<?php
namespace App\Entity;
use App\Repository\StoreImageRepository;
use App\Utils\Formatter;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=StoreImageRepository::class)
* @Vich\Uploadable
*/
class StoreImage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $imagePath;
/**
* @Vich\UploadableField(mapping="store_images", fileNameProperty="imagePath")
* @Assert\File(
* maxSize="3000K",
* mimeTypes = {
* "image/png",
* "image/jpeg",
* "image/jpg",
* "image/webp"
* }
* )
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="datetime", nullable=true)
* @var \DateTime
*/
private $updatedAt;
/**
* @ORM\ManyToOne(targetEntity=StorePage::class, inversedBy="storeImages")
* @ORM\JoinColumn(nullable=false)
*/
private $storePage;
public function getId(): ?int
{
return $this->id;
}
public function getStorePage(): ?StorePage
{
return $this->storePage;
}
public function setStorePage(?StorePage $storePage): self
{
$this->storePage = $storePage;
return $this;
}
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($image) {
$this->updatedAt = new \DateTime('now');
}
}
public function getImageFile()
{
return $this->imageFile;
}
public function setImagePath($imagePath)
{
$this->imagePath = $imagePath;
}
public function getImagePath()
{
return $this->imagePath;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* generate a unique name for any images based on
* - id of store
* - name of store
* - city
* - original file name
* - time
* @return string
*/
public function generateSeoFriendlyFileName() {
$storeId = str_pad($this->getStorePage()->getStore()->getId(), 5, '0', STR_PAD_LEFT);
return strtolower($storeId.'_'.$this -> getStorePage()->getStore()->getSlug().'_'.md5(time().$this->getImageFile()->getFilename()));
}
}