<?php
/**
* Created by PhpStorm.
* User: takabayashi
* Date: 2019/09/19
* Time: 21:18
*/
declare(strict_types=1);
namespace App\EventListener\Workflow\Task\QrCode;
use App\Entity\QrCode;
use App\Service\QrCode\UsedUpChecker\UsedUpCheckerResolverInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;
class QrCodeUseUpListener implements EventSubscriberInterface
{
/**
* @var UsedUpCheckerResolverInterface
*/
private $usedUpCheckerResolver;
/**
* QrCodeUseUpListener constructor.
* @param UsedUpCheckerResolverInterface $usedUpCheckerResolver
*/
public function __construct(UsedUpCheckerResolverInterface $usedUpCheckerResolver)
{
$this->usedUpCheckerResolver = $usedUpCheckerResolver;
}
public function guardUseUp(GuardEvent $event): void
{
$qrCode = $event->getSubject();
if (!$qrCode instanceof QrCode) {
throw new \LogicException('subject must be '. QrCode::class);
}
if (!$this->usedUpCheckerResolver->resolve($qrCode)->isUsedUp($qrCode)) {
$event->addTransitionBlocker(new TransitionBlocker('まだQrCodeを使い切っていません。', '0'));
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
'workflow.qrcode_states.guard.use_up' => ['guardUseUp'],
];
}
}