<?php
declare(strict_types=1);
namespace Hitso\Bundle\ContentBundle\EventListener;
use Hitso\Bundle\CommonBundle\Helper\Event\LikeableEvent;
use Hitso\Bundle\ContentBundle\Entity\Content;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LikeableListener implements EventSubscriberInterface
{
public function onValidate(LikeableEvent $event): void
{
$entity = $event->getEntity();
if (!$entity instanceof Content || !$event->isValid()) {
return;
}
if ($event->isValid() && !$this->canLikeIt($entity)) {
$event->setValid(false);
$event->stopPropagation();
}
}
private function canLikeIt(Content $content): bool
{
return true === $content->isRated() && true === $content->isPublished();
}
public static function getSubscribedEvents()
{
return [
LikeableEvent::ON_VALIDATE_LIKEABLE_EVENT => 'onValidate',
];
}
}