src/Subscriber/Content/ContentRankingSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Content;
  3. use App\Entity\ContentRanking;
  4. use App\Event\Content\CommentCreatedEvent;
  5. use App\Event\Content\CommentUpdatedEvent;
  6. use App\Service\Content\CommentService;
  7. use App\Service\Content\RankingService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ContentRankingSubscriber implements EventSubscriberInterface
  10. {
  11.     protected RankingService $service;
  12.     protected CommentService $commentService;
  13.     /**
  14.      * CommentSubscriber constructor.
  15.      */
  16.     public function __construct(RankingService $serviceCommentService $commentService)
  17.     {
  18.         $this->service $service;
  19.         $this->commentService $commentService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CommentCreatedEvent::class => [
  25.                 ['onCommentCreatedUpdateContentRankingVotes'0],
  26.             ],
  27.             CommentUpdatedEvent::class => [
  28.                 ['onCommentUpdatedUpdateContentRankingVotes'0],
  29.             ],
  30.         ];
  31.     }
  32.     /**
  33.      * @throws \Doctrine\ORM\ORMException
  34.      * @throws \Doctrine\ORM\OptimisticLockException
  35.      */
  36.     public function onCommentCreatedUpdateContentRankingVotes(CommentCreatedEvent $event)
  37.     {
  38.         $this->calculateAndStoreRanking($event);
  39.     }
  40.     /**
  41.      * @param CommentCreatedEvent $event
  42.      *
  43.      * @throws \Doctrine\ORM\ORMException
  44.      * @throws \Doctrine\ORM\OptimisticLockException
  45.      */
  46.     public function onCommentUpdatedUpdateContentRankingVotes(CommentUpdatedEvent $event)
  47.     {
  48.         $this->calculateAndStoreRanking($event);
  49.     }
  50.     /**
  51.      * @throws \Doctrine\ORM\ORMException
  52.      * @throws \Doctrine\ORM\OptimisticLockException
  53.      */
  54.     protected function calculateAndStoreRanking(CommentCreatedEvent|CommentUpdatedEvent $event)
  55.     {
  56.         $content $event->getComment()->getContent();
  57.         if (!$ranking $content->getRanking()) {
  58.             $ranking $this->service->createRankingByContent($content);
  59.         }
  60.         $sums $this->commentService->getSumsForContent($content);
  61.         $ranking->setCommentCount($sums['comment_count']);
  62.         $ranking->setVotes((int) $sums['vote_count']);
  63.         if ($ranking->getVotes() > 0) {
  64.             $ranking->setVoteAverage($sums['vote_sum'] / $ranking->getVotes());
  65.         }
  66.         $ranking->setRank($this->calculateRankInteger($ranking));
  67.         $this->service->storeRanking($ranking);
  68.     }
  69.     protected function calculateRankInteger(ContentRanking $entity): int
  70.     {
  71.         $rank 0;
  72.         if ($entity->getVoteAverage()) {
  73.             $rank += $entity->getVoteAverage() * 100;
  74.         }
  75.         if ($entity->getVotes()) {
  76.             $rank += $entity->getVotes() * 20;
  77.         }
  78.         if ($entity->getCommentCount()) {
  79.             $rank += $entity->getCommentCount() * 20;
  80.         }
  81.         if ($entity->getSellCount()) {
  82.             $rank += $entity->getSellCount() * 10;
  83.         }
  84.         // do not consider for now (business logic)
  85.         if (false && $entity->getLikeCount()) {
  86.             $rank += $entity->getLikeCount() * 10;
  87.         }
  88.         if ($entity->getRankModifier()) {
  89.             $rank $entity $entity->getRankModifier();
  90.         }
  91.         return $rank;
  92.     }
  93. }