<?php
namespace App\AtechNet\CoreBundle\Entity;
use App\AtechNet\CoreBundle\Helper\CloudflareImage;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\PostRemove;
use Doctrine\ORM\Mapping\Table as Table;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Table(name="photos")
* @Entity
* @HasLifecycleCallbacks
*/
class Photo extends AbstractPhoto
{
public static $supported_mime_type = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
/**
* @var Album
*
* @Assert\NotNull
*
* @ManyToOne(targetEntity="Album", inversedBy="photos")
* @JoinColumn(name="album_id", referencedColumnName="id")
*/
private $album;
/**
* @var string
*
* @Column(name="cloudflare_id", type="string", length=256, nullable=true)
*/
private $cloudflareId;
/**
* @Column(name="context_array", type="array")
*/
private $contexts;
public function __construct()
{
parent::__construct();
$this->contexts = ['thumb'];
}
/**
* @param string $context
*
* @return Photo
*/
public function addContext($context)
{
if (array_key_exists($context, self::$available_context)) {
$this->contexts[] = $context;
} else {
throw new HttpException(printf("Invalid context name: %s", $context));
}
return $this;
}
/**
* @return array
*/
public function getContexts()
{
return $this->contexts;
}
public function setContexts($contexts)
{
$this->contexts = $contexts;
}
public function clearContexts()
{
$this->contexts = [];
}
public function reGenerateContextImages()
{
$this->postUploadProcess();
}
protected function postUploadProcess()
{
// Disable Image Generation
// foreach ($this->contexts as $context) {
// $this->generateImage($context);
// }
}
/**
* @PostRemove()
*/
public function deleteFile()
{
parent::deleteFile();
}
/**
* @Assert\IsTrue(message = "Not supported file type")
*
* @return bool
*/
public function isValid()
{
$file = $this->getFile();
if (!is_null($file)) {
$mimeType = $file->getMimeType();
return in_array(strtolower($mimeType), self::$supported_mime_type);
} else {
return true;
}
}
/**
* @return Album
*/
public function getAlbum()
{
return $this->album;
}
/**
* @param Album $album
*/
public function setAlbum($album)
{
$this->album = $album;
$album->addPhoto($this);
}
/**
* @return string|null
*/
public function getCloudflareId(): ?string
{
return $this->cloudflareId;
}
/**
* @param string|null $cloudflareId
*/
public function setCloudflareId(?string $cloudflareId): void
{
$this->cloudflareId = $cloudflareId;
}
/**
* @param string $variant
* @return string
*/
public function getCdnUrl(string $variant): string
{
if (!is_null($this->cloudflareId)) {
return 'https://imagedelivery.net/' . CloudflareImage::ACCOUNT_HASH . '/' . $this->cloudflareId . '/' . $variant;
}
return 'https://www.atechcomp.eu/centralis/web/' . $this->getContextWebPath('eshop');
}
/**
* @param int $width
* @param int $height
* @param string $extension
* @return string
*/
public function getInlineUrl(int $width, int $height, string $extension = 'jpeg'): string
{
return 'https://www.furbify.sk/cdn/image/product/' . $this->getId() . $this->getSalt() . '-' . $width . '-' . $height . '.' . $extension;
}
/**
* @param Product $product
* @param string $context
* @return string
*/
public function getCdnPath(Product $product, $context)
{
return strtolower('p/' . str_replace([' ', ',', '"'], '', $product->getType()->getTitle()) . '/' . $product->getCode() . '/' . $context . '/' . str_replace(['(', ')', '+', ' ', '_', 'Ü', ','], '-', $this->getOriginalName()));
}
/**
* @param Product $product
* @param $context
* @return string
*/
public function getFullCdnPath(Product $product, $context)
{
return 'https://furbify.ams3.cdn.digitaloceanspaces.com/' . $this->getCdnPath($product, $context);
}
}