src/Domain/Entity/Comment.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Domain\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\Delete;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\GetCollection;
  7. use ApiPlatform\Metadata\Link;
  8. use ApiPlatform\Metadata\Post;
  9. use ApiPlatform\Metadata\Put;
  10. use App\Application\ApiResources\State\Processor\Comment\CommentDeleteProcessor;
  11. use App\Application\ApiResources\State\Processor\Comment\CommentInputProcessor;
  12. use App\Application\ApiResources\State\Processor\Comment\CommentsValidateProcessor;
  13. use App\Application\ApiResources\State\Processor\Comment\CommentUpdateProcessor;
  14. use App\Application\ApiResources\State\Processor\Comment\CommentValidateProcessor;
  15. use App\Application\ApiResources\State\Provider\GetUncertifiedCommentProvider;
  16. use App\Application\Controller\Api\Comment\GetLastCommunityAction;
  17. use App\Application\Controller\Api\Comment\GetWithRevisionAction;
  18. use App\Application\Dto\Comment\CommentInput;
  19. use App\Application\Dto\Comment\CommentsValidateInput;
  20. use App\Domain\Entity\Behavior\Identifiable;
  21. use App\Domain\Entity\Translation\CommentTranslation;
  22. use DateTime;
  23. use Doctrine\Common\Collections\ArrayCollection;
  24. use Doctrine\Common\Collections\Collection;
  25. use Gedmo\SoftDeleteable\Traits\SoftDeleteable;
  26. use Gedmo\Timestampable\Traits\Timestampable;
  27. use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
  28. use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
  29. /**
  30.  * Class Comment
  31.  *
  32.  * @package App\Domain\Entity
  33.  */
  34. #[ApiResource(
  35.     operations: [
  36.         new Delete(processorCommentDeleteProcessor::class),
  37.         new GetCollection(uriTemplate'/comments/with_revision'controllerGetWithRevisionAction::class),
  38.         new GetCollection(uriTemplate'/comments/last_community'controllerGetLastCommunityAction::class),
  39.         new GetCollection(
  40.             filters: [
  41.                 'comment.search_filter',
  42.                 'comment.exists_filter',
  43.                 'soft_delete_filter',
  44.                 'comment.order_filter',
  45.                 'comment.null_filter',
  46.             ],
  47.         ),
  48.         new GetCollection(
  49.             uriTemplate'/comments/uncertified_list',
  50.             providerGetUncertifiedCommentProvider::class
  51.         ),
  52.         new Get(),
  53.         new Post(
  54.             normalizationContext: ['groups' => ['translations']],
  55.             inputCommentInput::class,
  56.             processorCommentInputProcessor::class
  57.         ),
  58.         new Put(normalizationContext: ['groups' => ['translations']], processorCommentUpdateProcessor::class),
  59.         new Put(uriTemplate'/comments/{uuid}/validate'processorCommentValidateProcessor::class),
  60.         new Post(
  61.             uriTemplate'/comments/validate',
  62.             inputCommentsValidateInput::class,
  63.             processorCommentsValidateProcessor::class,
  64.             deserializetrue,
  65.             inputFormats: ['json' => ['application/json''application/ld+json']],
  66.             outputFormats: ['json' => ['application/json''application/ld+json']],
  67.         ),
  68.     ],
  69.     normalizationContext: ['groups' => ['comment_read']],
  70.     denormalizationContext: ['groups' => ['comment_write']],
  71.     filters: ['translation.groups'],
  72.     order: ['createdAt' => 'DESC'],
  73. )]
  74. #[ApiResource(
  75.     uriTemplate'/people/{uuid}/comments',
  76.     operations: [
  77.         new GetCollection(normalizationContext: ['groups' => 'comment_read']),
  78.     ],
  79.     uriVariables: [
  80.         'uuid' => new Link(toProperty'author'fromClassPerson::class),
  81.     ]
  82. )]
  83. abstract class Comment extends AbstractTranslatable
  84. {
  85.     use Identifiable;
  86.     use SoftDeleteable;
  87.     use Timestampable;
  88.     public const COMMENT_ACCEPTED 'accepted';
  89.     public const COMMENT_NEW 'new';
  90.     public const COMMENT_REFUSED 'refused';
  91.     private Collection $children;
  92.     public ?string $remoteId null;
  93.     /**
  94.      * @param Person        $author
  95.      * @param Locale|null   $authorLocale
  96.      * @param string        $status
  97.      * @param int           $notation
  98.      * @param Comment|null  $parent
  99.      * @param DateTime|null $zhistUpdate
  100.      */
  101.     public function __construct(
  102.         public Person $author,
  103.         public Locale null $authorLocale,
  104.         public string $status self::COMMENT_NEW,
  105.         public int $notation 0,
  106.         public Comment null $parent null,
  107.         public ?DateTime $zhistUpdate null,
  108.         public ?DateTime $lastUserUpdate null,
  109.     ) {
  110.         parent::__construct();
  111.         $this->children = new ArrayCollection();
  112.     }
  113.     public function createTranslation(): TranslationInterface
  114.     {
  115.         return new CommentTranslation();
  116.     }
  117.     public function getTranslationType($locale null): CommentTranslation
  118.     {
  119.         return $this->getTranslation($locale);
  120.     }
  121.     public function getValue(): ?string
  122.     {
  123.         return $this->getTranslationType()->getValue();
  124.     }
  125.     public function setValue(?string $value): void
  126.     {
  127.         $this->getTranslationType()->setValue($value);
  128.     }
  129.     /**
  130.      * @return Collection
  131.      */
  132.     public function getChildren(): Collection
  133.     {
  134.         return $this->children->filter(fn(Comment $child) => $child->status === self::COMMENT_ACCEPTED && $child->deletedAt === null);
  135.     }
  136.     /**
  137.      * @param Collection $children
  138.      */
  139.     public function setChildren(Collection $children): void
  140.     {
  141.         $this->children $children;
  142.     }
  143.     /**
  144.      * @param Comment $child
  145.      *
  146.      * @return void
  147.      */
  148.     public function addChild(Comment $child): void
  149.     {
  150.         if ($this->children->contains($child)) {
  151.             return;
  152.         }
  153.         $this->children->add($child);
  154.     }
  155.     /**
  156.      * @param Comment $child
  157.      *
  158.      * @return void
  159.      */
  160.     public function removeChild(Comment $child): void
  161.     {
  162.         if (!$this->children->contains($child)) {
  163.             return;
  164.         }
  165.         $this->children->removeElement($child);
  166.     }
  167.     public function getDefaultValue(): string
  168.     {
  169.         /** @var CommentTranslation $translation */
  170.         $translation $this->getTranslation($this->authorLocale->alpha2);
  171.         return $translation->getValue();
  172.     }
  173.     public function setLastUserUpdate(): void
  174.     {
  175.         $this->lastUserUpdate = new DateTime();
  176.     }
  177. }