src/Entity/StoreImage.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\StoreImageRepository;
  4. use App\Utils\Formatter;
  5. use DateTime;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. /**
  11.  * @ORM\Entity(repositoryClass=StoreImageRepository::class)
  12.  * @Vich\Uploadable
  13.  */
  14. class StoreImage
  15. {
  16.     /**
  17.      * @ORM\Id()
  18.      * @ORM\GeneratedValue()
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="string", length=255)
  24.      * @var string
  25.      */
  26.     private $imagePath;
  27.     /**
  28.      * @Vich\UploadableField(mapping="store_images", fileNameProperty="imagePath")
  29.      * @Assert\File(
  30.      *     maxSize="3000K",
  31.      *     mimeTypes = {
  32.      *          "image/png",
  33.      *          "image/jpeg",
  34.      *          "image/jpg",
  35.      *          "image/webp"
  36.      *      }
  37.      *     )
  38.      * @var File
  39.      */
  40.     private $imageFile;
  41.     /**
  42.      * @ORM\Column(type="datetime", nullable=true)
  43.      * @var \DateTime
  44.      */
  45.     private $updatedAt;
  46.     /**
  47.      * @ORM\ManyToOne(targetEntity=StorePage::class, inversedBy="storeImages")
  48.      * @ORM\JoinColumn(nullable=false)
  49.      */
  50.     private $storePage;
  51.     public function getId(): ?int
  52.     {
  53.         return $this->id;
  54.     }
  55.     public function getStorePage(): ?StorePage
  56.     {
  57.         return $this->storePage;
  58.     }
  59.     public function setStorePage(?StorePage $storePage): self
  60.     {
  61.         $this->storePage $storePage;
  62.         return $this;
  63.     }
  64.     public function setImageFile(File $image null)
  65.     {
  66.         $this->imageFile $image;
  67.         // It is required that at least one field changes if you are using Doctrine,
  68.         // otherwise the event listeners won't be called and the file is lost
  69.         if ($image) {
  70.             $this->updatedAt = new \DateTime('now');
  71.         }
  72.     }
  73.     public function getImageFile()
  74.     {
  75.         return $this->imageFile;
  76.     }
  77.     public function setImagePath($imagePath)
  78.     {
  79.         $this->imagePath $imagePath;
  80.     }
  81.     public function getImagePath()
  82.     {
  83.         return $this->imagePath;
  84.     }
  85.     public function getUpdatedAt(): ?\DateTimeInterface
  86.     {
  87.         return $this->updatedAt;
  88.     }
  89.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  90.     {
  91.         $this->updatedAt $updatedAt;
  92.         return $this;
  93.     }
  94.     /**
  95.      * generate a unique name for any images based on
  96.      * - id of store
  97.      * - name of store
  98.      * - city
  99.      * - original file name
  100.      * - time
  101.      * @return string
  102.      */
  103.     public function generateSeoFriendlyFileName() {
  104.         $storeId str_pad($this->getStorePage()->getStore()->getId(), 5'0'STR_PAD_LEFT);
  105.         return strtolower($storeId.'_'.$this -> getStorePage()->getStore()->getSlug().'_'.md5(time().$this->getImageFile()->getFilename()));
  106.     }
  107. }