src/CasinoBundle/Entity/CasinoScreenshot.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\CasinoBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\CasinoBundle\Enum\ScreenshotDeviceTypeEnum;
  5. use App\CmsBundle\Entity\PathTrait;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8.  * CasinoScreenshot
  9.  *
  10.  * @ORM\Table(
  11.  *     name="casino_screenshot",
  12.  *     indexes={
  13.  *          @ORM\Index(name="casino_screenshot_index", columns={"casino_id"}),
  14.  *          @ORM\Index(name="casino_screenshot_position_index", columns={"position"}),
  15.  *          @ORM\Index(name="casino_screenshot_type_index", columns={"type"})
  16.  *     }
  17.  * )
  18.  * @ORM\Entity(
  19.  *     repositoryClass="App\CasinoBundle\Repository\CasinoScreenshotRepository"
  20.  * )
  21.  * @ORM\HasLifecycleCallbacks()
  22.  * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="one_day")
  23.  */
  24. class CasinoScreenshot
  25. {
  26.     use PathTrait;
  27.     const SERVER_PATH_FOLDER '/tmp';
  28.     /**
  29.      * @var integer
  30.      *
  31.      * @ORM\Id
  32.      * @ORM\Column(name="id", type="bigint", nullable=false)
  33.      * @ORM\GeneratedValue(strategy="AUTO")
  34.      */
  35.     private int $id;
  36.     /**
  37.      * @var Casino
  38.      * @ORM\ManyToOne(targetEntity="Casino", inversedBy="casinoScreenshots")
  39.      * @ORM\JoinColumn(name="casino_id", referencedColumnName="id", nullable=false)
  40.      */
  41.     private Casino $casino;
  42.     /**
  43.      * @var ?integer
  44.      * @ORM\Column(name="position", type="integer", nullable=true)
  45.      */
  46.     private ?int $position;
  47.     /**
  48.      * @var integer
  49.      * @ORM\Column(name="type", type="integer", nullable=false)
  50.      */
  51.     private int $type;
  52.     /**
  53.      * @Assert\All({
  54.      *      @Assert\File(
  55.      *          mimeTypes={ "image/jpeg" }
  56.      *     )
  57.      * })
  58.      */
  59.     private array $files;
  60.     /**
  61.      * @var string
  62.      */
  63.     private string $filename;
  64.     /**
  65.      * @var ?string
  66.      */
  67.     private ?string $site null;
  68.     /**
  69.      * @return string
  70.      */
  71.     public function __toString(): string
  72.     {
  73.         return $this->path != null $this->path '';
  74.     }
  75.     /**
  76.      * @return int|null
  77.      */
  78.     public function getId(): ?int
  79.     {
  80.         return $this->id;
  81.     }
  82.     /**
  83.      * @param int $id
  84.      * @return void
  85.      */
  86.     public function setId(int $id): void
  87.     {
  88.         $this->id $id;
  89.     }
  90.     /**
  91.      * @return Casino|null
  92.      */
  93.     public function getCasino(): ?Casino
  94.     {
  95.         return $this->casino;
  96.     }
  97.     /**
  98.      * @param Casino $casino
  99.      * @return $this
  100.      */
  101.     public function setCasino(Casino $casino): self
  102.     {
  103.         $this->casino $casino;
  104.         return $this;
  105.     }
  106.     /**
  107.      * @return ?int
  108.      */
  109.     public function getPosition(): ?int
  110.     {
  111.         return $this->position ?? 0;
  112.     }
  113.     /**
  114.      * @param int|null $position
  115.      * @return $this
  116.      */
  117.     public function setPosition(?int $position): self
  118.     {
  119.         $this->position $position;
  120.         return $this;
  121.     }
  122.     /**
  123.      * @param int $type
  124.      * @return $this
  125.      */
  126.     public function setType(int $type): self
  127.     {
  128.         ScreenshotDeviceTypeEnum::assertExists($type);
  129.         $this->type $type;
  130.         return $this;
  131.     }
  132.     /**
  133.      * @return int
  134.      */
  135.     public function getType(): int
  136.     {
  137.         return $this->type;
  138.     }
  139.     /**
  140.      * @return array
  141.      */
  142.     public function getFiles(): array
  143.     {
  144.         return $this->files;
  145.     }
  146.     public function addFile(string $file): void
  147.     {
  148.         $this->files[] = $file;
  149.     }
  150.     /**
  151.      * @param array $files
  152.      * @return void
  153.      */
  154.     public function setFiles(array $files): void
  155.     {
  156.         $this->files $files;
  157.     }
  158.     /**
  159.      * @return void
  160.      */
  161.     public function upload(): void
  162.     {
  163.         foreach ($this->files as $file) {
  164.             $filename md5(uniqid()) . '.' $file->getClientOriginalExtension();
  165.             $file->move(self::SERVER_PATH_FOLDER$filename);
  166.             $this->addFile($filename);
  167.         }
  168.     }
  169.     /**
  170.      * @return ?string
  171.      */
  172.     public function getSite(): ?string
  173.     {
  174.         return $this->site;
  175.     }
  176.     /**
  177.      * @param ?string $site
  178.      * @return $this
  179.      */
  180.     public function setSite(?string $site): self
  181.     {
  182.         $this->site $site;
  183.         return $this;
  184.     }
  185. }