src/AtechNet/CoreBundle/Entity/Photo.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\AtechNet\CoreBundle\Entity;
  3. use App\AtechNet\CoreBundle\Helper\CloudflareImage;
  4. use Doctrine\ORM\Mapping\Column;
  5. use Doctrine\ORM\Mapping\Entity;
  6. use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
  7. use Doctrine\ORM\Mapping\JoinColumn;
  8. use Doctrine\ORM\Mapping\ManyToOne;
  9. use Doctrine\ORM\Mapping\PostRemove;
  10. use Doctrine\ORM\Mapping\Table as Table;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14.  * @Table(name="photos")
  15.  * @Entity
  16.  * @HasLifecycleCallbacks
  17.  */
  18. class Photo extends AbstractPhoto
  19. {
  20.     public static $supported_mime_type = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
  21.     /**
  22.      * @var Album
  23.      *
  24.      * @Assert\NotNull
  25.      *
  26.      * @ManyToOne(targetEntity="Album", inversedBy="photos")
  27.      * @JoinColumn(name="album_id", referencedColumnName="id")
  28.      */
  29.     private $album;
  30.     /**
  31.      * @var string
  32.      *
  33.      * @Column(name="cloudflare_id", type="string", length=256, nullable=true)
  34.      */
  35.     private $cloudflareId;
  36.     /**
  37.      * @Column(name="context_array", type="array")
  38.      */
  39.     private $contexts;
  40.     public function __construct()
  41.     {
  42.         parent::__construct();
  43.         $this->contexts = ['thumb'];
  44.     }
  45.     /**
  46.      * @param string $context
  47.      *
  48.      * @return Photo
  49.      */
  50.     public function addContext($context)
  51.     {
  52.         if (array_key_exists($context, self::$available_context)) {
  53.             $this->contexts[] = $context;
  54.         } else {
  55.             throw new HttpException(printf("Invalid context name: %s", $context));
  56.         }
  57.         return $this;
  58.     }
  59.     /**
  60.      * @return array
  61.      */
  62.     public function getContexts()
  63.     {
  64.         return $this->contexts;
  65.     }
  66.     public function setContexts($contexts)
  67.     {
  68.         $this->contexts = $contexts;
  69.     }
  70.     public function clearContexts()
  71.     {
  72.         $this->contexts = [];
  73.     }
  74.     public function reGenerateContextImages()
  75.     {
  76.         $this->postUploadProcess();
  77.     }
  78.     protected function postUploadProcess()
  79.     {
  80.         // Disable Image Generation
  81.         // foreach ($this->contexts as $context) {
  82.         //     $this->generateImage($context);
  83.         // }
  84.     }
  85.     /**
  86.      * @PostRemove()
  87.      */
  88.     public function deleteFile()
  89.     {
  90.         parent::deleteFile();
  91.     }
  92.     /**
  93.      * @Assert\IsTrue(message = "Not supported file type")
  94.      *
  95.      * @return bool
  96.      */
  97.     public function isValid()
  98.     {
  99.         $file = $this->getFile();
  100.         if (!is_null($file)) {
  101.             $mimeType = $file->getMimeType();
  102.             return in_array(strtolower($mimeType), self::$supported_mime_type);
  103.         } else {
  104.             return true;
  105.         }
  106.     }
  107.     /**
  108.      * @return Album
  109.      */
  110.     public function getAlbum()
  111.     {
  112.         return $this->album;
  113.     }
  114.     /**
  115.      * @param Album $album
  116.      */
  117.     public function setAlbum($album)
  118.     {
  119.         $this->album = $album;
  120.         $album->addPhoto($this);
  121.     }
  122.     /**
  123.      * @return string|null
  124.      */
  125.     public function getCloudflareId(): ?string
  126.     {
  127.         return $this->cloudflareId;
  128.     }
  129.     /**
  130.      * @param string|null $cloudflareId
  131.      */
  132.     public function setCloudflareId(?string $cloudflareId): void
  133.     {
  134.         $this->cloudflareId = $cloudflareId;
  135.     }
  136.     /**
  137.      * @param string $variant
  138.      * @return string
  139.      */
  140.     public function getCdnUrl(string $variant): string
  141.     {
  142.         if (!is_null($this->cloudflareId)) {
  143.             return 'https://imagedelivery.net/' . CloudflareImage::ACCOUNT_HASH . '/' . $this->cloudflareId . '/' . $variant;
  144.         }
  145.         return 'https://www.atechcomp.eu/centralis/web/' . $this->getContextWebPath('eshop');
  146.     }
  147.     /**
  148.      * @param int $width
  149.      * @param int $height
  150.      * @param string $extension
  151.      * @return string
  152.      */
  153.     public function getInlineUrl(int $width, int $height, string $extension = 'jpeg'): string
  154.     {
  155.         return 'https://www.furbify.sk/cdn/image/product/' . $this->getId() . $this->getSalt() . '-' . $width . '-' . $height . '.' . $extension;
  156.     }
  157.     /**
  158.      * @param Product $product
  159.      * @param string $context
  160.      * @return string
  161.      */
  162.     public function getCdnPath(Product $product, $context)
  163.     {
  164.         return strtolower('p/' . str_replace([' ', ',', '"'], '', $product->getType()->getTitle()) . '/' . $product->getCode() . '/' . $context . '/' . str_replace(['(', ')', '+', ' ', '_', 'Ü', ','], '-', $this->getOriginalName()));
  165.     }
  166.     /**
  167.      * @param Product $product
  168.      * @param $context
  169.      * @return string
  170.      */
  171.     public function getFullCdnPath(Product $product, $context)
  172.     {
  173.         return 'https://furbify.ams3.cdn.digitaloceanspaces.com/' . $this->getCdnPath($product, $context);
  174.     }
  175. }