src/Entity/Media.php line 93

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use App\Controller\CreateMediaAction;
  7. use App\Trait\TimestampableEntity;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Symfony\Component\Serializer\Annotation\Groups;
  11. use Symfony\Component\Serializer\Annotation\MaxDepth;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  14. /**
  15.  * A media object, such as an image, video, or audio object embedded in a web page or a downloadable dataset i.e. DataDownload. Note that a creative work may have many media objects associated with it on the same web page. For example, a page about a single song (MusicRecording) may have a music video (VideoObject), and a high and low bandwidth audio stream (2 AudioObject's).
  16.  *
  17.  * @see https://schema.org/MediaObject
  18.  *
  19.  * @author Jordi Fernandes Alves <jfadev@gmail.com>
  20.  */
  21. #[ORM\Entity]
  22. #[ApiResource(
  23.     iri'Media',
  24.     itemOperations: [
  25.         'get' => [
  26.             // 'security' => "is_granted('ROLE_USER')",
  27.             'normalization_context' => [
  28.                 'groups' => 'media:item:get',
  29.                 'enable_max_depth' => true
  30.             ]
  31.         ],
  32.         'put' => [
  33.             // 'security' => "is_granted('ROLE_USER')",
  34.             'normalization_context' => [
  35.                 'groups' => 'media:item:put',
  36.                 'enable_max_depth' => true
  37.             ],
  38.             'denormalization_context' => [
  39.                 'groups' => 'media:item:put',
  40.                 'enable_max_depth' => true
  41.             ]
  42.         ],
  43.         'delete' => [
  44.             // 'security' => "is_granted('ROLE_USER')",
  45.         ]
  46.     ],
  47.     collectionOperations: [
  48.         'get' => [
  49.             // 'security' => "is_granted('ROLE_USER')",
  50.             'normalization_context' => [
  51.                 'groups' => ['media:collection:get''createdAt'],
  52.                 'enable_max_depth' => true
  53.             ]
  54.         ],
  55.         'post' => [
  56.             // 'security' => "is_granted('ROLE_USER')",
  57.             'normalization_context' => [
  58.                 'groups' => 'media:collection:post',
  59.                 'enable_max_depth' => true
  60.             ],
  61.             'denormalization_context' => [
  62.                 'groups' => 'media:collection:post',
  63.                 'enable_max_depth' => true
  64.             ],
  65.             'controller' => CreateMediaAction::class,
  66.             'deserialize' => false,
  67.             'validation_groups' => ['Default''create'],
  68.             'openapi_context' => [
  69.                 'requestBody' => [
  70.                     'content' => [
  71.                         'multipart/form-data' => [
  72.                             'schema' => [
  73.                                 'type' => 'object',
  74.                                 'properties' => [
  75.                                     'file' => [
  76.                                         'type' => 'string',
  77.                                         'format' => 'binary'
  78.                                     ]
  79.                                 ]
  80.                             ]
  81.                         ]
  82.                     ]
  83.                 ]
  84.             ],
  85.         ],
  86.     ]
  87. )]
  88. #[Vich\Uploadable]
  89. class Media
  90. {
  91.     use TimestampableEntity;
  92.     
  93.     #[ORM\Id]
  94.     #[ORM\GeneratedValue(strategy'AUTO')]
  95.     #[ORM\Column(type'integer')]
  96.     #[Groups([
  97.         'media:collection:post',
  98.         'media:item:get',
  99.         'media:item:put',
  100.         'endorsement:collection:post',
  101.         'endorsement:collection:get',
  102.         'endorsement:item:get',
  103.         'endorsement:item:put',
  104.         'policy_upload:collection:post',
  105.         'policy_upload:collection:get',
  106.         'policy_upload:item:get',
  107.         'policy_upload:item:put',
  108.         'demand_upload:collection:post',
  109.         'demand_upload:collection:get',
  110.         'demand_upload:item:get',
  111.         'demand_upload:item:put',
  112.         'insured_upload:collection:post',
  113.         'insured_upload:collection:get',
  114.         'insured_upload:item:get',
  115.         'insured_upload:item:put',
  116.         'insured_insurer_global_ccg:collection:post',
  117.         'insured_insurer_global_ccg:collection:get',
  118.         'insured_insurer_global_ccg:item:get',
  119.         'insured_insurer_global_ccg:item:put',
  120.         'user:collection:get',
  121.         'user:collection:post',
  122.         'user:item:get',
  123.         'user:item:put',
  124.     ])]
  125.     private ?int $id null;
  126.     /**
  127.      * The name of the item.
  128.      *
  129.      * @see https://schema.org/name
  130.      */
  131.     #[ORM\Column(type'string'nullablefalse)]
  132.     #[ApiProperty(iri'https://schema.org/name')]
  133.     #[Groups([
  134.         'media:collection:post',
  135.         'media:item:get',
  136.         'media:item:put',
  137.         'endorsement:collection:get',
  138.         'endorsement:item:get',
  139.         'policy_upload:collection:get',
  140.         'policy_upload:item:get',
  141.         'demand_upload:collection:get',
  142.         'demand_upload:item:get',
  143.         'insured_upload:collection:get',
  144.         'insured_upload:item:get',
  145.         'insured_insurer_global_ccg:collection:get',
  146.         'insured_insurer_global_ccg:item:get',
  147.         'user:collection:get',
  148.         'user:item:get',
  149.         'insured:item:get',
  150.         'policy:item:get',
  151.         'demand:item:get',
  152.     ])]
  153.     private ?string $name '';
  154.     /**
  155.      * Actual bytes of the media object, for example the image file or video file.
  156.      *
  157.      * @see https://schema.org/contentUrl
  158.      */
  159.     #[ApiProperty(iri'https://schema.org/contentUrl')]
  160.     #[Groups([
  161.         'media:collection:post',
  162.         'media:item:get',
  163.         'endorsement:collection:get',
  164.         'endorsement:item:get',
  165.         'policy_upload:collection:get',
  166.         'policy_upload:item:get',
  167.         'demand_upload:collection:get',
  168.         'demand_upload:item:get',
  169.         'insured_upload:collection:get',
  170.         'insured_upload:item:get',
  171.         'insured_insurer_global_ccg:collection:get',
  172.         'insured_insurer_global_ccg:item:get',
  173.         'user:collection:get',
  174.         'user:item:get'
  175.     ])]
  176.     private ?string $contentUrl null;
  177.     /**
  178.      * File size in (mega/kilo) bytes.
  179.      *
  180.      * @see https://schema.org/contentSize
  181.      */
  182.     #[ORM\Column(type'text'nullabletrue)]
  183.     #[ApiProperty(iri'https://schema.org/contentSize')]
  184.     #[Assert\Type('string')]
  185.     #[Groups([
  186.         'media:collection:post',
  187.         'media:item:get',
  188.         'media:item:put',
  189.         'endorsement:collection:get',
  190.         'endorsement:item:get',
  191.         'policy_upload:collection:get',
  192.         'policy_upload:item:get',
  193.         'demand_upload:collection:get',
  194.         'demand_upload:item:get',
  195.         'insured_upload:collection:get',
  196.         'insured_upload:item:get',
  197.         'insured_insurer_global_ccg:collection:get',
  198.         'insured_insurer_global_ccg:item:get',
  199.         'user:collection:get',
  200.         'user:item:get'
  201.     ])]
  202.     private ?string $contentSize null;
  203.     /**
  204.      * Media type typically expressed using a MIME format (see \[IANA site\](http://www.iana.org/assignments/media-types/media-types.xhtml) and \[MDN reference\](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics\_of\_HTTP/MIME\_types)) e.g. application/zip for a SoftwareApplication binary, audio/mpeg for .mp3 etc.). In cases where a \[\[CreativeWork\]\] has several media type representations, \[\[encoding\]\] can be used to indicate each \[\[MediaObject\]\] alongside particular \[\[encodingFormat\]\] information. Unregistered or niche encoding and file formats can be indicated instead via the most appropriate URL, e.g. defining Web page or a Wikipedia/Wikidata entry.
  205.      *
  206.      * @see https://schema.org/encodingFormat
  207.      */
  208.     #[ORM\Column(type'text'nullabletrue)]
  209.     #[ApiProperty(iri'https://schema.org/encodingFormat')]
  210.     #[Assert\Type('string')]
  211.     #[Groups([
  212.         'media:collection:post',
  213.         'media:item:get',
  214.         'media:item:put',
  215.         'endorsement:collection:get',
  216.         'endorsement:item:get',
  217.         'policy_upload:collection:get',
  218.         'policy_upload:item:get',
  219.         'demand_upload:collection:get',
  220.         'demand_upload:item:get',
  221.         'insured_upload:collection:get',
  222.         'insured_upload:item:get',
  223.         'insured_insurer_global_ccg:collection:get',
  224.         'insured_insurer_global_ccg:item:get',
  225.         'user:collection:get',
  226.         'user:item:get'
  227.     ])]
  228.     private ?string $encodingFormat null;
  229.     /**
  230.      * Date when this media object was uploaded to this site.
  231.      *
  232.      * @see https://schema.org/uploadDate
  233.      */
  234.     #[ORM\Column(type'date'nullabletrue)]
  235.     #[ApiProperty(iri'https://schema.org/uploadDate')]
  236.     #[Assert\Type(\DateTimeInterface::class)]
  237.     #[Groups([
  238.         'media:collection:post',
  239.         'media:item:get',
  240.         'media:item:put',
  241.     ])]
  242.     private ?\DateTimeInterface $uploadDate null;
  243.     /**
  244.      * Genre of the creative work, broadcast channel or group.
  245.      *
  246.      * @see https://schema.org/genre
  247.      */
  248.     #[ORM\Column(type'string'nullabletrue)]
  249.     #[ApiProperty(iri'https://schema.org/genre')]
  250.     #[Groups([
  251.         'media:collection:post',
  252.         'media:item:get',
  253.         'media:item:put',
  254.         'endorsement:collection:get',
  255.         'endorsement:item:get',
  256.         'policy_upload:collection:get',
  257.         'policy_upload:item:get',
  258.         'demand_upload:collection:get',
  259.         'demand_upload:item:get',
  260.         'insured_upload:collection:get',
  261.         'insured_upload:item:get',
  262.         'insured_insurer_global_ccg:collection:get',
  263.         'insured_insurer_global_ccg:item:get',
  264.         'user:collection:get',
  265.         'user:item:get'
  266.     ])]
  267.     private ?string $genre null;
  268.     #[Assert\NotNull(groups: ['create'])]
  269.     #[Vich\UploadableField(
  270.         mapping'medias'
  271.         fileNameProperty'filePath'
  272.         size'contentSize'
  273.         mimeType'encodingFormat'
  274.         originalName'name'
  275.     )]
  276.     private ?File $file null;
  277.     #[ORM\Column(type'string'length255nullabletrue)]
  278.     #[Groups([
  279.         'media:collection:post',
  280.         'media:item:get',
  281.         'media:item:put',
  282.     ])]
  283.     private ?string $filePath null;
  284.     #[ORM\ManyToOne(targetEntityInsured::class, inversedBy'uploads')]
  285.     private $insured;
  286.     #[ORM\ManyToOne(targetEntityDemand::class, inversedBy'uploads')]
  287.     private $demand;
  288.     #[ORM\OneToOne(mappedBy'upload'targetEntityPolicyUpload::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  289.     private $policyUpload;
  290.     #[ORM\OneToOne(mappedBy'upload'targetEntityDemandUpload::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  291.     private $demandUpload;
  292.     
  293.     #[ORM\OneToOne(mappedBy'upload'targetEntityEndorsement::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  294.     private $endorsement;
  295.     #[ORM\OneToOne(mappedBy'upload'targetEntityInsuredUpload::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  296.     private $insuredUpload;
  297.     #[ORM\OneToOne(mappedBy'upload'targetEntityInsuredInsurerGlobalCcg::class, cascade: ['persist''remove'], orphanRemovaltrue)]
  298.     private $insuredInsurerGlobalCcg;
  299.     public function getId(): ?int
  300.     {
  301.         return $this->id;
  302.     }
  303.     // public function setContentUrl(?string $contentUrl): void
  304.     // {
  305.     //     $this->contentUrl = $contentUrl;
  306.     // }
  307.     public function getContentUrl(): ?string
  308.     {
  309.         return $this->genre === 'avatar' '/api/avatar/' $this->id '/api/download/' $this->id;
  310.     }
  311.     public function setContentSize(?string $contentSize): void
  312.     {
  313.         $this->contentSize $contentSize;
  314.     }
  315.     public function getContentSize(): ?string
  316.     {
  317.         return $this->contentSize;
  318.     }
  319.     public function setEncodingFormat(?string $encodingFormat): void
  320.     {
  321.         $this->encodingFormat $encodingFormat;
  322.     }
  323.     public function getEncodingFormat(): ?string
  324.     {
  325.         return $this->encodingFormat;
  326.     }
  327.     public function setUploadDate(?\DateTimeInterface $uploadDate): void
  328.     {
  329.         $this->uploadDate $uploadDate;
  330.     }
  331.     public function getUploadDate(): ?\DateTimeInterface
  332.     {
  333.         return $this->uploadDate;
  334.     }
  335.     public function setGenre(?string $genre): void
  336.     {
  337.         $this->genre $genre;
  338.     }
  339.     public function getGenre(): ?string
  340.     {
  341.         return $this->genre;
  342.     }
  343.     public function getInsured(): ?Insured
  344.     {
  345.         return $this->insured;
  346.     }
  347.     public function setInsured(?Insured $insured): self
  348.     {
  349.         $this->insured $insured;
  350.         return $this;
  351.     }
  352.     public function getDemand(): ?Demand
  353.     {
  354.         return $this->demand;
  355.     }
  356.     public function setDemand(?Demand $demand): self
  357.     {
  358.         $this->demand $demand;
  359.         return $this;
  360.     }
  361.     // public function getPolicy(): ?Policy
  362.     // {
  363.     //     return $this->policy;
  364.     // }
  365.     // public function setPolicy(?Policy $policy): self
  366.     // {
  367.     //     $this->policy = $policy;
  368.     //     return $this;
  369.     // }
  370.     public function getName(): ?string
  371.     {
  372.         return $this->name;
  373.     }
  374.     public function setName(?string $name): self
  375.     {
  376.         $this->name $name;
  377.         return $this;
  378.     }
  379.     public function getEndorsement(): ?Endorsement
  380.     {
  381.         return $this->endorsement;
  382.     }
  383.     public function setEndorsement(?Endorsement $endorsement): self
  384.     {
  385.         // unset the owning side of the relation if necessary
  386.         if (null === $endorsement && null !== $this->endorsement) {
  387.             $this->endorsement->setUpload(null);
  388.         }
  389.         // set the owning side of the relation if necessary
  390.         if (null !== $endorsement && $endorsement->getUpload() !== $this) {
  391.             $endorsement->setUpload($this);
  392.         }
  393.         $this->endorsement $endorsement;
  394.         return $this;
  395.     }
  396.     public function getPolicyUpload(): ?PolicyUpload
  397.     {
  398.         return $this->policyUpload;
  399.     }
  400.     public function setPolicyUpload(?PolicyUpload $policyUpload): self
  401.     {
  402.         // unset the owning side of the relation if necessary
  403.         if (null === $policyUpload && null !== $this->policyUpload) {
  404.             $this->policyUpload->setUpload(null);
  405.         }
  406.         // set the owning side of the relation if necessary
  407.         if (null !== $policyUpload && $policyUpload->getUpload() !== $this) {
  408.             $policyUpload->setUpload($this);
  409.         }
  410.         $this->policyUpload $policyUpload;
  411.         return $this;
  412.     }
  413.     public function getDemandUpload(): ?DemandUpload
  414.     {
  415.         return $this->demandUpload;
  416.     }
  417.     public function setDemandUpload(?DemandUpload $demandUpload): self
  418.     {
  419.         // unset the owning side of the relation if necessary
  420.         if (null === $demandUpload && null !== $this->demandUpload) {
  421.             $this->PolicyUpload->setUpload(null);
  422.         }
  423.         // set the owning side of the relation if necessary
  424.         if (null !== $demandUpload && $demandUpload->getUpload() !== $this) {
  425.             $demandUpload->setUpload($this);
  426.         }
  427.         $this->demandUpload $demandUpload;
  428.         return $this;
  429.     }
  430.     public function getInsuredUpload(): ?InsuredUpload
  431.     {
  432.         return $this->insuredUpload;
  433.     }
  434.     public function setInsuredUpload(?InsuredUpload $insuredUpload): self
  435.     {
  436.         // unset the owning side of the relation if necessary
  437.         if (null === $insuredUpload && null !== $this->insuredUpload) {
  438.             $this->insuredUpload->setUpload(null);
  439.         }
  440.         // set the owning side of the relation if necessary
  441.         if (null !== $insuredUpload && $insuredUpload->getUpload() !== $this) {
  442.             $insuredUpload->setUpload($this);
  443.         }
  444.         $this->insuredUpload $insuredUpload;
  445.         return $this;
  446.     }
  447.     public function getInsuredInsurerGlobalCcg(): ?InsuredInsurerGlobalCcg
  448.     {
  449.         return $this->insuredInsurerGlobalCcg;
  450.     }
  451.     public function setInsuredInsurerGlobalCcg(?InsuredInsurerGlobalCcg $insuredInsurerGlobalCcg): self
  452.     {
  453.         // unset the owning side of the relation if necessary
  454.         if (null === $insuredInsurerGlobalCcg && null !== $this->insuredInsurerGlobalCcg) {
  455.             $this->insuredInsurerGlobalCcg->setUpload(null);
  456.         }
  457.         // set the owning side of the relation if necessary
  458.         if (null !== $insuredInsurerGlobalCcg && $insuredInsurerGlobalCcg->getUpload() !== $this) {
  459.             $insuredInsurerGlobalCcg->setUpload($this);
  460.         }
  461.         $this->insuredInsurerGlobalCcg $insuredInsurerGlobalCcg;
  462.         return $this;
  463.     }
  464.     public function getFile(): ?File
  465.     {
  466.         return $this->file;
  467.     }
  468.     public function setFile(?File $file): self
  469.     {
  470.         $this->file $file;
  471.         if (null !== $file) {
  472.             $this->uploadDate = new \DateTimeImmutable();
  473.         }
  474.         return $this;
  475.     }
  476.     public function getFilePath(): ?string
  477.     {
  478.         return $this->filePath;
  479.     }
  480.     public function setFilePath(?string $filePath): self
  481.     {
  482.         $this->filePath $filePath;
  483.         return $this;
  484.     }
  485. }