src/Domain/Entity/PointOfInterest.php line 196

Open in your IDE?
  1. <?php
  2. namespace App\Domain\Entity;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Delete;
  6. use ApiPlatform\Metadata\Get;
  7. use ApiPlatform\Metadata\GetCollection;
  8. use ApiPlatform\Metadata\Post;
  9. use ApiPlatform\Metadata\Put;
  10. use App\Admin\Controller\Api\DeleteAndNotifyPointOfInterestAction;
  11. use App\Application\ApiResources\State\Processor\PointOfInterest\PointOfInterestCreateProcessor;
  12. use App\Application\ApiResources\State\Processor\PointOfInterest\PointOfInterestDeleteProcessor;
  13. use App\Application\ApiResources\State\Processor\PointOfInterest\PointOfInterestUpdateProcessor;
  14. use App\Application\Controller\Api\PointOfInterest\CountCommentAction;
  15. use App\Application\Controller\Api\PointOfInterest\GetJSONRevisionsLogAction;
  16. use App\Application\Controller\Api\PointOfInterest\GetMainRevisionAction;
  17. use App\Application\Controller\Api\PointOfInterest\GetMyPointOfInterestAction;
  18. use App\Application\Controller\Api\PointOfInterest\PostTransferCommentAction;
  19. use App\Application\Controller\Api\PointOfInterest\PostTransferPictureAction;
  20. use App\Application\Controller\Api\PointOfInterest\PostTransferPicturesAction;
  21. use App\Application\Controller\Api\PointOfInterest\SetFavoriteAction;
  22. use App\Application\Dto\PersonPointOfInterest\FavoriteInput;
  23. use App\Application\Dto\PointOfInterest\PointOfInterestInput;
  24. use App\Domain\Entity\Behavior\Identifiable;
  25. use App\Domain\Entity\PointOfInterest\Picture;
  26. use App\Domain\Entity\PointOfInterest\Revision;
  27. use DateTime;
  28. use Doctrine\Common\Collections\ArrayCollection;
  29. use Doctrine\Common\Collections\Collection;
  30. use Doctrine\ORM\Mapping\OrderBy;
  31. use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
  32. use Gedmo\Timestampable\Traits\Timestampable;
  33. /**
  34.  * Class PointOfInterest
  35.  *
  36.  * @package App\Domain\Entity
  37.  */
  38. #[
  39.     ApiResource(
  40.         operations: [
  41.             new Delete(security"is_granted('ROLE_ADMIN')"processorPointOfInterestDeleteProcessor::class),
  42.             new Delete(
  43.                 uriTemplate'/point_of_interests/{uuid}/delete_notify',
  44.                 controllerDeleteAndNotifyPointOfInterestAction::class,
  45.                 security"is_granted('ROLE_ADMIN')"
  46.             ),
  47.             new GetCollection(
  48.                 uriTemplate'/point_of_interests/mine',
  49.                 controllerGetMyPointOfInterestAction::class,
  50.                 openapiContext: [
  51.                     'summary' => 'Get current user\'s point of interests',
  52.                     'parameters' => [
  53.                         [
  54.                             'name' => 'deletedAt',
  55.                             'in' => 'query',
  56.                             'schema' => [
  57.                                 'type' => 'string',
  58.                                 'format' => 'date-time',
  59.                             ],
  60.                             'description' => 'Date and time of deletion. If set, only deleted point of interests will be returned.',
  61.                         ],
  62.                     ],
  63.                     'responses' => [
  64.                         '200' => [
  65.                             'description' => 'Point of interests list',
  66.                             'content' => [
  67.                                 'application/json' => [
  68.                                     'schema' => [
  69.                                         'type' => 'object',
  70.                                         'properties' => [
  71.                                             '@context' => [
  72.                                                 'type' => 'string',
  73.                                             ],
  74.                                             '@id' => [
  75.                                                 'type' => 'string',
  76.                                             ],
  77.                                             '@type' => [
  78.                                                 'type' => 'string',
  79.                                             ],
  80.                                             'hydra:member' => [
  81.                                                 'type' => 'array',
  82.                                                 'items' => [
  83.                                                     'type' => 'object',
  84.                                                     'properties' => [
  85.                                                         '@type' => [
  86.                                                             'type' => 'string',
  87.                                                         ],
  88.                                                         '@id' => [
  89.                                                             'type' => 'string',
  90.                                                         ],
  91.                                                         'city' => [
  92.                                                             'type' => 'string',
  93.                                                         ],
  94.                                                         'mainPicture' => [
  95.                                                             'type' => 'string',
  96.                                                         ],
  97.                                                         'title' => [
  98.                                                             'type' => 'string',
  99.                                                         ],
  100.                                                         'typeCode' => [
  101.                                                             'type' => 'string',
  102.                                                         ],
  103.                                                         'uuid' => [
  104.                                                             'type' => 'string',
  105.                                                         ],
  106.                                                         'zipCode' => [
  107.                                                             'type' => 'string',
  108.                                                         ],
  109.                                                     ],
  110.                                                 ],
  111.                                             ],
  112.                                         ],
  113.                                         'example' => [
  114.                                             '@context' => 'string',
  115.                                             '@id' => 'string',
  116.                                             '@type' => 'string',
  117.                                             'hydra:member' => [
  118.                                                 [
  119.                                                     '@type' => 'string',
  120.                                                     '@id' => 'string',
  121.                                                     'city' => 'string',
  122.                                                     'mainPicture' => 'string',
  123.                                                     'title' => 'string',
  124.                                                     'typeCode' => 'string',
  125.                                                     'uuid' => 'string',
  126.                                                     'zipCode' => 'string',
  127.                                                 ],
  128.                                             ],
  129.                                         ],
  130.                                     ],
  131.                                 ],
  132.                             ],
  133.                         ],
  134.                     ],
  135.                 ],
  136.                 name'get_my_points_of_interest',
  137.             ),
  138.             new GetCollection(
  139.                 uriTemplate'/point_of_interests/{uuid}/count_comments',
  140.                 controllerCountCommentAction::class,
  141.                 name'get_total_comments'
  142.             ),
  143.             new Get(),
  144.             new Get(
  145.                 uriTemplate'/point_of_interests/{uuid}/main_revision',
  146.                 controllerGetMainRevisionAction::class,
  147.                 name'get_main_revision'
  148.             ),
  149.             new Get(
  150.                 uriTemplate'/point_of_interests/{uuid}/revision_log',
  151.                 controllerGetJSONRevisionsLogAction::class,
  152.                 name'get_revision_json_log'
  153.             ),
  154.             new Post(
  155.                 inputFormats: ['multipart'],
  156.                 inputPointOfInterestInput::class,
  157.                 processorPointOfInterestCreateProcessor::class
  158.             ),
  159.             new Post(
  160.                 uriTemplate'/point_of_interests/{uuid}/transfer_comments',
  161.                 controllerPostTransferCommentAction::class,
  162.                 security"is_granted('ROLE_ADMIN')",
  163.                 name'get_transfer_comment',
  164.             ),
  165.             new Post(
  166.                 uriTemplate'/point_of_interests/{uuid}/favorite',
  167.                 controllerSetFavoriteAction::class,
  168.                 denormalizationContext: ['groups' => 'favorite_write'],
  169.                 security"is_granted('ROLE_USER')",
  170.                 inputFavoriteInput::class,
  171.             ),
  172.             new Post(
  173.                 uriTemplate'/point_of_interests/{uuid}/transfer_pictures',
  174.                 controllerPostTransferPicturesAction::class,
  175.                 security"is_granted('ROLE_ADMIN')",
  176.                 name'get_transfer_pictures'
  177.             ),
  178.             new Post(
  179.                 uriTemplate'/point_of_interests/{uuid}/transfer_picture',
  180.                 controllerPostTransferPictureAction::class,
  181.                 security"is_granted('ROLE_ADMIN')",
  182.                 name'get_transfer_picture'
  183.             ),
  184.             new Put(
  185.                 security"is_granted('ROLE_ADMIN')",
  186.                 processorPointOfInterestUpdateProcessor::class
  187.             ),
  188.         ],
  189.         normalizationContext: ['groups' => ['point_of_interest_read']],
  190.         denormalizationContext: ['groups' => ['point_of_interest_write']],
  191.     )
  192. ]
  193. class PointOfInterest
  194. {
  195.     use Identifiable;
  196.     use SoftDeleteable;
  197.     use Timestampable;
  198.     public const OFFER_TYPE_DISCOVERY 'discovery';
  199.     public const AMOUNT_DISCOVERY 99.0;
  200.     public const VAT_AMOUNT_DISCOVERY 118.8;
  201.     public Collection $awards;
  202.     public Collection $comments;
  203.     #[OrderBy(['position' => 'asc'])]
  204.     public Collection $pictures;
  205.     public ?string $remoteId null;
  206.     public ?string $oldId null;
  207.     public Collection $revisions;
  208.     public function __construct(
  209.         public ?Person $author null,
  210.         public ?Person $referent null,
  211.         public ?Media $mainPicture null,
  212.         #[ApiProperty(openapiContext: ['nullable' => false])]
  213.         public float $averageNotation 0,
  214.         public ?DateTime $zhistUpdate null,
  215.         public bool $draft true,
  216.         public ?string $offerType null,
  217.         public ?DateTime $offerEndAt null,
  218.         // For Input Dto only, not doctrine mapped
  219.         public ?Revision $revision null,
  220.         public ?bool $isFavorite false,
  221.         public ?string $slug null,
  222.     ) {
  223.         $this->comments = new ArrayCollection();
  224.         $this->pictures = new ArrayCollection();
  225.         $this->awards = new ArrayCollection();
  226.         $this->revisions = new ArrayCollection();
  227.     }
  228.     /**
  229.      * @param Picture $pointOfInterestPicture
  230.      *
  231.      * @return void
  232.      */
  233.     public function addPointOfInterestPicture(Picture $pointOfInterestPicture): void
  234.     {
  235.         $this->pictures->add($pointOfInterestPicture);
  236.     }
  237.     /**
  238.      * @param Picture $pointOfInterestPicture
  239.      *
  240.      * @return void
  241.      */
  242.     public function removePointOfInterestPicture(Picture $pointOfInterestPicture): void
  243.     {
  244.         $this->pictures->removeElement($pointOfInterestPicture);
  245.     }
  246.     public function removeAward(Award $award): void
  247.     {
  248.         if (!$this->awards->contains($award)) {
  249.             return;
  250.         }
  251.         $this->awards->removeElement($award);
  252.         $award->removePointOfInterest($this);
  253.     }
  254.     public function addAward(Award $award): void
  255.     {
  256.         if ($this->awards->contains($award)) {
  257.             return;
  258.         }
  259.         $this->awards->add($award);
  260.         $award->addPointOfInterest($this);
  261.     }
  262.     /**
  263.      * @return Collection
  264.      */
  265.     public function getAwards(): Collection
  266.     {
  267.         return $this->awards;
  268.     }
  269.     /**
  270.      * @param Collection $awards
  271.      */
  272.     public function setAwards(Collection $awards): void
  273.     {
  274.         $this->awards $awards;
  275.     }
  276.     public function getPictures(): array
  277.     {
  278.         $pictures $this->pictures->filter(
  279.             fn(Picture $picture) => ($picture->type !== Picture::PICTURE_UNCERTIFIED),
  280.         )->getValues();
  281.         usort(
  282.             $pictures,
  283.             fn(Picture $aPicture $b) => $a->getPosition() <=> $b->getPosition(),
  284.         );
  285.         return $pictures;
  286.     }
  287.     /**
  288.      * @param Collection $pictures
  289.      */
  290.     public function setPictures(Collection $pictures): void
  291.     {
  292.         $this->pictures $pictures;
  293.     }
  294.     public function removePicture(Picture $picture): void
  295.     {
  296.         if (!$this->pictures->contains($picture)) {
  297.             return;
  298.         }
  299.         $this->pictures->removeElement($picture);
  300.     }
  301.     public function addPicture(Picture $picture): void
  302.     {
  303.         if ($this->pictures->contains($picture)) {
  304.             return;
  305.         }
  306.         $this->pictures->add($picture);
  307.     }
  308.     /**
  309.      * @return void
  310.      */
  311.     public function removePictures(): void
  312.     {
  313.         $this->pictures->clear();
  314.     }
  315.     public function setPicturePosition(Picture $pictureint $position): void
  316.     {
  317.         $picture->setPosition($position);
  318.     }
  319.     public function isPro(): bool
  320.     {
  321.         return $this->offerType !== null && $this->offerEndAt !== null && $this->offerEndAt > new DateTime();
  322.     }
  323. }