src/CasinoBundle/Entity/Logo.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\CasinoBundle\Entity;
  3. use App\CmsBundle\Entity\TimeStampedTrait;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\HttpFoundation\File\UploadedFile;
  6. use DateTime;
  7. /**
  8.  * Logo
  9.  *
  10.  * @ORM\Table(name="logo")
  11.  * @ORM\Entity
  12.  * @ORM\HasLifecycleCallbacks()
  13.  */
  14. class Logo
  15. {
  16.     use TimeStampedTrait;
  17.     const WEBPATH '/uploads/logos/';
  18.     const SERVER_PATH_TO_IMAGE_FOLDER __DIR__ '/../../../public/' self::WEBPATH;
  19.     /**
  20.      * @var int
  21.      *
  22.      * @ORM\Column(name="id", type="integer", nullable=false)
  23.      * @ORM\Id
  24.      * @ORM\GeneratedValue(strategy="IDENTITY")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @var string
  29.      *
  30.      * @ORM\Column(name="w_size", type="integer", nullable=true)
  31.      */
  32.     private $widthSize;
  33.     /**
  34.      * @var string
  35.      *
  36.      * @ORM\Column(name="h_size", type="integer", nullable=true)
  37.      */
  38.     private $heightSize;
  39.     /**
  40.      * @ORM\ManyToOne(targetEntity="App\CasinoBundle\Entity\Casino", inversedBy="logos", cascade={"persist"})
  41.      * @ORM\JoinColumn(nullable=false)
  42.      */
  43.     private $casino;
  44.     /**
  45.      * @ORM\Column(type="string", length=255, nullable=true)
  46.      */
  47.     private $photo;
  48.     /**
  49.      * Unmapped property to handle file uploads
  50.      */
  51.     private $file;
  52.     /**
  53.      * @var bool|null
  54.      *
  55.      * @ORM\Column(name="for_review", type="boolean", nullable=true)
  56.      */
  57.     private $review;
  58.     /**
  59.      * @var bool|null
  60.      *
  61.      * @ORM\Column(name="for_catalog", type="boolean", nullable=true)
  62.      */
  63.     private $catalog;
  64.     public function __toString(): string
  65.     {
  66.         return $this->photo;
  67.     }
  68.     public function __construct()
  69.     {
  70.         $this->setCreatedValues();
  71.     }
  72.     public function getId(): int
  73.     {
  74.         return $this->id;
  75.     }
  76.     public function setId(int $id): void
  77.     {
  78.         $this->id $id;
  79.     }
  80.     public function getSize(): ?string
  81.     {
  82.         return $this->widthSize 'x' $this->heightSize;
  83.     }
  84.     public function setWidthSize(int $size): self
  85.     {
  86.         $this->widthSize $size;
  87.         return $this;
  88.     }
  89.     public function setHeightSize(int $size): self
  90.     {
  91.         $this->heightSize $size;
  92.         return $this;
  93.     }
  94.     public function getWidthSize(): ?int
  95.     {
  96.         return $this->widthSize;
  97.     }
  98.     public function getHeightSize(): ?int
  99.     {
  100.         return $this->heightSize;
  101.     }
  102.     public function getCasino(): ?Casino
  103.     {
  104.         return $this->casino;
  105.     }
  106.     public function setCasino(?Casino $casino): self
  107.     {
  108.         $this->casino $casino;
  109.         return $this;
  110.     }
  111.     public function getPhoto(): ?string
  112.     {
  113.         return $this->photo;
  114.     }
  115.     public function setPhoto(?string $photo): self
  116.     {
  117.         $this->photo $photo;
  118.         return $this;
  119.     }
  120.     /**
  121.      * @param UploadedFile $file
  122.      */
  123.     public function setFile(UploadedFile $file null)
  124.     {
  125.         $this->file $file;
  126.     }
  127.     /**
  128.      * @return UploadedFile
  129.      */
  130.     public function getFile()
  131.     {
  132.         return $this->file;
  133.     }
  134.     /**
  135.      * Manages the copying of the file to the relevant place on the server
  136.      * @ORM\PrePersist()
  137.      * @ORM\PreUpdate()
  138.      */
  139.     public function upload()
  140.     {
  141.         // the file property can be empty if the field is not required
  142.         if (null === $this->getFile()) {
  143.             return;
  144.         }
  145.         // we use the original file name here but you should
  146.         // sanitize it at least to avoid any security issues
  147.         // move takes the target directory and target filename as params
  148.         $filename $this->getFile()->getClientOriginalName();
  149.         $ext explode('.'$filename);
  150.         $newFilename = (new DateTime())->getTimestamp() . '.' end($ext);
  151.         $this->getFile()->move(
  152.             self::SERVER_PATH_TO_IMAGE_FOLDER,
  153.             $newFilename
  154.         );
  155.         // set the path property to the filename where you've saved the file
  156.         $this->photo self::WEBPATH $newFilename;
  157.         // clean up the file property as you won't need it anymore
  158.         $this->setFile(null);
  159.     }
  160.     /**
  161.      * Lifecycle callback to upload the file to the server.
  162.      */
  163.     public function lifecycleFileUpload()
  164.     {
  165.         $this->upload();
  166.     }
  167.     /**
  168.      * Updates the hash value to force the preUpdate and postUpdate events to fire.
  169.      */
  170.     public function refreshUpdated()
  171.     {
  172.         $this->setUpdated(new \DateTime());
  173.     }
  174.     public function setReview(?bool $value): self
  175.     {
  176.         $this->review $value;
  177.         return $this;
  178.     }
  179.     public function getReview(?bool $value): ?bool
  180.     {
  181.         return $this->review;
  182.     }
  183.     public function setCatalog(?bool $value): self
  184.     {
  185.         $this->catalog $value;
  186.         return $this;
  187.     }
  188.     public function getCatalog(?bool $value): ?bool
  189.     {
  190.         return $this->catalog;
  191.     }
  192. }