src/Entity/Media.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\MediaRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use InvalidArgumentException;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. /**
  12.  * @ORM\Entity(repositoryClass=MediaRepository::class)
  13.  * @Vich\Uploadable
  14.  */
  15. class Media
  16. {
  17.     const UPLOAD_MAX_SIZE "500M";
  18.     const IMAGE_MIMES = [
  19.         "image/png",
  20.         "image/jpeg",
  21.         "image/svg+xml"
  22.     ];
  23.     const AUDIO_MIMES = [
  24.         "audio/x-wav",
  25.         "audio/mpeg",
  26.         "audio/mp4a-latm",
  27.     ];
  28.     const VIDEO_MIMES = [
  29.         "video/mp4",
  30.         "video/quicktime",
  31.     ];
  32.     const DOCUMENT_MIMES = [
  33.         "application/pdf",
  34.         "application/msword",
  35.         "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  36.         "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  37.         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  38.         "text/csv",
  39.         "application/vnd.ms-excel.sheet.macroEnabled.12",
  40.         "application/vnd.ms-excel",
  41.     ];
  42.     const UPLOAD_MIMES = [
  43.         "image/png",
  44.         "image/jpeg",
  45.         "audio/mpeg",
  46.         "audio/x-wav",
  47.         "audio/mp4a-latm",
  48.         "video/mp4",
  49.         "video/quicktime",
  50.         "application/pdf",
  51.         "application/msword",
  52.         "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  53.         "image/svg+xml",
  54.         "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  55.         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  56.         "text/csv",
  57.         "application/vnd.ms-excel.sheet.macroEnabled.12",
  58.         "application/vnd.ms-excel",
  59.     ];
  60.     const AUDIO_TYPE 0;
  61.     const VIDEO_TYPE 1;
  62.     const IMAGE_TYPE 2;
  63.     const DOCUMENT_TYPE 3;
  64.     const NONE_TYPE 4;
  65.     const TYPES = [
  66.         self::AUDIO_TYPE,
  67.         self::VIDEO_TYPE,
  68.         self::IMAGE_TYPE,
  69.         self::DOCUMENT_TYPE,
  70.         self::NONE_TYPE,
  71.     ];
  72.     /**
  73.      * @ORM\Id
  74.      * @ORM\GeneratedValue
  75.      * @ORM\Column(type="integer")
  76.      */
  77.     private $id;
  78.     /**
  79.      * @Vich\UploadableField(mapping="media", fileNameProperty="fileName")
  80.      */
  81.     #[Assert\File(
  82.         maxSizeself::UPLOAD_MAX_SIZE,
  83.         mimeTypesself::UPLOAD_MIMES,
  84.         mimeTypesMessage'Wrong file type {{ type }}, allowed types: {{ types }}',
  85.     )]
  86.     private ?File $file null;
  87.     /**
  88.      * @ORM\Column(type="string", length=255, nullable=true)
  89.      */
  90.     private $fileName;
  91.     /**
  92.      * @ORM\Column(type="smallint")
  93.      */
  94.     #[Assert\Choice(self::TYPES)]
  95.     private $type;
  96.     /**
  97.      * @ORM\Column(type="datetime_immutable", options={ "default": "CURRENT_TIMESTAMP" })
  98.      */
  99.     private $createdAt;
  100.     /**
  101.      * @ORM\Column(type="datetime_immutable", options={ "default": "CURRENT_TIMESTAMP" })
  102.      */
  103.     private $updatedAt;
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity=Contributor::class, inversedBy="media", cascade={"persist"})
  106.      */
  107.     private $contributor;
  108.     public function __construct()
  109.     {
  110.         $this->createdAt = new \DateTimeImmutable();
  111.         $this->updatedAt = new \DateTimeImmutable();
  112.     }
  113.     public function getId(): ?int
  114.     {
  115.         return $this->id;
  116.     }
  117.     public function getFile(): ?File
  118.     {
  119.         return $this->file;
  120.     }
  121.     public function setFile(?File $file): self
  122.     {
  123.         $this->file $file;
  124.         $this->updatedAt = new \DateTimeImmutable('now');
  125.         if ($file) {
  126.             $fileMime $file->getMimeType();
  127.             if (in_array($fileMimeself::IMAGE_MIMES)) {
  128.                 $this->type self::IMAGE_TYPE;
  129.             } elseif (in_array($fileMimeself::AUDIO_MIMES)) {
  130.                 $this->type self::AUDIO_TYPE;
  131.             } elseif (in_array($fileMimeself::VIDEO_MIMES)) {
  132.                 $this->type self::VIDEO_TYPE;
  133.             } elseif (in_array($fileMimeself::DOCUMENT_MIMES)) {
  134.                 $this->type self::DOCUMENT_TYPE;
  135.             } elseif (in_array($fileMimeself::AUDIO_MIMES)) {
  136.                 $this->type self::AUDIO_TYPE;
  137.             } else {
  138.                 throw new InvalidArgumentException('Wrong file type');
  139.             }
  140.         }
  141.         return $this;
  142.     }
  143.     public function getFileName(): ?string
  144.     {
  145.         return $this->fileName;
  146.     }
  147.     public function setFileName(?string $fileName): self
  148.     {
  149.         $this->fileName $fileName;
  150.         return $this;
  151.     }
  152.     public function getType(): ?int
  153.     {
  154.         return $this->type;
  155.     }
  156.     public function setType(int $type): self
  157.     {
  158.         $this->type $type;
  159.         return $this;
  160.     }
  161.     public function getCreatedAt(): ?\DateTimeImmutable
  162.     {
  163.         return $this->createdAt;
  164.     }
  165.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  166.     {
  167.         $this->createdAt $createdAt;
  168.         return $this;
  169.     }
  170.     public function getUpdatedAt(): ?\DateTimeImmutable
  171.     {
  172.         return $this->updatedAt;
  173.     }
  174.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  175.     {
  176.         $this->updatedAt $updatedAt;
  177.         return $this;
  178.     }
  179.     public function toString(): string
  180.     {
  181.         return '';
  182.     }
  183.     public function __toString()
  184.     {
  185.         return $this->fileName;
  186.     }
  187.     public function getContributor(): ?Contributor
  188.     {
  189.         return $this->contributor;
  190.     }
  191.     public function setContributor(?Contributor $contributor): self
  192.     {
  193.         $this->contributor $contributor;
  194.         return $this;
  195.     }
  196. }