src/CasinoBundle/Entity/SlotScreenshot.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\CasinoBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\CmsBundle\Entity\PathTrait;
  5. use Symfony\Component\HttpFoundation\File\File;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8.  * SlotScreenshot
  9.  *
  10.  * @ORM\Table(
  11.  *     name="slot_screenshot",
  12.  *     indexes={
  13.  *          @ORM\Index(name="slot_screenshot_position_index", columns={"position"})
  14.  *     }
  15.  * )
  16.  * @ORM\Entity(
  17.  *     repositoryClass="App\CasinoBundle\Repository\SlotScreenshotRepository"
  18.  * )
  19.  * @ORM\HasLifecycleCallbacks()
  20.  * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="one_day")
  21.  */
  22. class SlotScreenshot
  23. {
  24.     use PathTrait;
  25.     const SERVER_PATH_FOLDER '/tmp';
  26.     /**
  27.      * @ORM\Id()
  28.      * @ORM\GeneratedValue()
  29.      * @ORM\Column(type="integer")
  30.      */
  31.     private ?int $id;
  32.     /**
  33.      * @var Slot
  34.      * @ORM\ManyToOne(targetEntity="Slot", inversedBy="slotScreenshots")
  35.      * @ORM\JoinColumn(name="slot_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
  36.      */
  37.     private Slot $slot;
  38.     /**
  39.      * @var ?integer
  40.      * @ORM\Column(name="position", type="integer", nullable=true)
  41.      */
  42.     private ?int $position;
  43.     /**
  44.      * @Assert\All({
  45.      *      @Assert\File(
  46.      *          mimeTypes={ "image/jpeg" }
  47.      *     )
  48.      * })
  49.      */
  50.     private array $files;
  51.     /**
  52.      * @var string
  53.      */
  54.     private string $filename;
  55.     /**
  56.      * @var ?string
  57.      */
  58.     private ?string $site null;
  59.     /**
  60.      * @return string
  61.      */
  62.     public function __toString(): string
  63.     {
  64.         return $this->path != null $this->path '';
  65.     }
  66.     /**
  67.      * @return int|null
  68.      */
  69.     public function getId(): ?int
  70.     {
  71.         return $this->id;
  72.     }
  73.     /**
  74.      * @return Slot|null
  75.      */
  76.     public function getSlot(): ?Slot
  77.     {
  78.         return $this->slot;
  79.     }
  80.     /**
  81.      * @param Slot $slot
  82.      * @return $this
  83.      */
  84.     public function setSlot(Slot $slot): self
  85.     {
  86.         $this->slot $slot;
  87.         return $this;
  88.     }
  89.     /**
  90.      * @return ?string
  91.      */
  92.     public function getSite(): ?string
  93.     {
  94.         return $this->site;
  95.     }
  96.     /**
  97.      * @param ?string $site
  98.      * @return $this
  99.      */
  100.     public function setSite(?string $site): self
  101.     {
  102.         $this->site $site;
  103.         return $this;
  104.     }
  105.     /**
  106.      * @return ?int
  107.      */
  108.     public function getPosition(): ?int
  109.     {
  110.         return $this->position ?? 0;
  111.     }
  112.     /**
  113.      * @param int|null $position
  114.      * @return $this
  115.      */
  116.     public function setPosition(?int $position): self
  117.     {
  118.         $this->position $position;
  119.         return $this;
  120.     }
  121.     /**
  122.      * @return array
  123.      */
  124.     public function getFiles(): array
  125.     {
  126.         return $this->files;
  127.     }
  128.     public function addFile(string $file): void
  129.     {
  130.         $this->files[] = $file;
  131.     }
  132.     /**
  133.      * @param array $files
  134.      * @return void
  135.      */
  136.     public function setFiles(array $files): void
  137.     {
  138.         $this->files $files;
  139.     }
  140.     /**
  141.      * @return void
  142.      */
  143.     public function upload(): void
  144.     {
  145.         foreach ($this->files as $file) {
  146.             $filename md5(uniqid()) . '.' $file->getClientOriginalExtension();
  147.             $file->move(self::SERVER_PATH_FOLDER$filename);
  148.             $this->addFile($filename);
  149.         }
  150.     }
  151. }