src/Hitso/Bundle/ContentBundle/EventListener/LikeableListener.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\ContentBundle\EventListener;
  4. use Hitso\Bundle\CommonBundle\Helper\Event\LikeableEvent;
  5. use Hitso\Bundle\ContentBundle\Entity\Content;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class LikeableListener implements EventSubscriberInterface
  8. {
  9.     public function onValidate(LikeableEvent $event): void
  10.     {
  11.         $entity $event->getEntity();
  12.         if (!$entity instanceof Content || !$event->isValid()) {
  13.             return;
  14.         }
  15.         if ($event->isValid() && !$this->canLikeIt($entity)) {
  16.             $event->setValid(false);
  17.             $event->stopPropagation();
  18.         }
  19.     }
  20.     private function canLikeIt(Content $content): bool
  21.     {
  22.         return true === $content->isRated() && true === $content->isPublished();
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             LikeableEvent::ON_VALIDATE_LIKEABLE_EVENT => 'onValidate',
  28.         ];
  29.     }
  30. }