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.     /**
  109.      * @ORM\ManyToOne(targetEntity=Instance::class)
  110.      */
  111.     #[Assert\NotNull(message'L’organisation est obligatoire.')]
  112.     private $instance;
  113.     public function __construct()
  114.     {
  115.         // Only set dates if they are not already set (when creating new entity, not loading from DB)
  116.         if (!isset($this->createdAt)) {
  117.             $this->createdAt = new \DateTimeImmutable();
  118.         }
  119.         if (!isset($this->updatedAt)) {
  120.             $this->updatedAt = new \DateTimeImmutable();
  121.         }
  122.     }
  123.     public function getId(): ?int
  124.     {
  125.         return $this->id;
  126.     }
  127.     public function getFile(): ?File
  128.     {
  129.         return $this->file;
  130.     }
  131.     public function setFile(?File $file): self
  132.     {
  133.         $this->file $file;
  134.         $this->updatedAt = new \DateTimeImmutable('now');
  135.         if ($file) {
  136.             $fileMime $file->getMimeType();
  137.             if (in_array($fileMimeself::IMAGE_MIMES)) {
  138.                 $this->type self::IMAGE_TYPE;
  139.             } elseif (in_array($fileMimeself::AUDIO_MIMES)) {
  140.                 $this->type self::AUDIO_TYPE;
  141.             } elseif (in_array($fileMimeself::VIDEO_MIMES)) {
  142.                 $this->type self::VIDEO_TYPE;
  143.             } elseif (in_array($fileMimeself::DOCUMENT_MIMES)) {
  144.                 $this->type self::DOCUMENT_TYPE;
  145.             } elseif (in_array($fileMimeself::AUDIO_MIMES)) {
  146.                 $this->type self::AUDIO_TYPE;
  147.             } else {
  148.                 throw new InvalidArgumentException('Wrong file type');
  149.             }
  150.         }
  151.         return $this;
  152.     }
  153.     public function getFileName(): ?string
  154.     {
  155.         return $this->fileName;
  156.     }
  157.     public function setFileName(?string $fileName): self
  158.     {
  159.         $this->fileName $fileName;
  160.         return $this;
  161.     }
  162.     public function getType(): ?int
  163.     {
  164.         return $this->type;
  165.     }
  166.     public function setType(int $type): self
  167.     {
  168.         $this->type $type;
  169.         return $this;
  170.     }
  171.     public function getCreatedAt(): ?\DateTimeImmutable
  172.     {
  173.         return $this->createdAt;
  174.     }
  175.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  176.     {
  177.         $this->createdAt $createdAt;
  178.         return $this;
  179.     }
  180.     public function getUpdatedAt(): ?\DateTimeImmutable
  181.     {
  182.         return $this->updatedAt;
  183.     }
  184.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): self
  185.     {
  186.         $this->updatedAt $updatedAt;
  187.         return $this;
  188.     }
  189.     public function toString(): string
  190.     {
  191.         return '';
  192.     }
  193.     public function __toString()
  194.     {
  195.         return $this->fileName;
  196.     }
  197.     public function getContributor(): ?Contributor
  198.     {
  199.         return $this->contributor;
  200.     }
  201.     public function setContributor(?Contributor $contributor): self
  202.     {
  203.         $this->contributor $contributor;
  204.         return $this;
  205.     }
  206.     public function getInstance(): ?Instance
  207.     {
  208.         return $this->instance;
  209.     }
  210.     public function setInstance(?Instance $instance): self
  211.     {
  212.         $this->instance $instance;
  213.         return $this;
  214.     }
  215. }