<?php
/**
* Created by PhpStorm.
* User: takabayashi
* Date: 2019/09/19
* Time: 20:48
*/
declare(strict_types=1);
namespace App\EventListener\Workflow\Task\QrCodeUsage;
use App\DBAL\Types\QrCodeStatusType;
use App\Entity\QrCodeUsage;
use App\Service\QrCode\DeviceValidator\DeviceValidatorResolverInterface;
use App\Service\QrCode\UsableValidator\UsableValidatorResolverInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;
class QrCodeUsageCheckListener implements EventSubscriberInterface
{
/**
* @var DeviceValidatorResolverInterface
*/
private $deviceValidatorResolver;
/**
* @var UsableValidatorResolverInterface
*/
private $usableValidatorResolver;
/**
* QrCodeUsageUseCase constructor.
* @param DeviceValidatorResolverInterface $deviceValidatorResolver
* @param UsableValidatorResolverInterface $usableValidatorResolver
*/
public function __construct(
DeviceValidatorResolverInterface $deviceValidatorResolver,
UsableValidatorResolverInterface $usableValidatorResolver
) {
$this->deviceValidatorResolver = $deviceValidatorResolver;
$this->usableValidatorResolver = $usableValidatorResolver;
}
public function guardCheck(GuardEvent $event): void
{
/** @var QrCodeUsage */
$qrCodeUsage = $event->getSubject();
if (!$qrCodeUsage instanceof QrCodeUsage) {
throw new \LogicException('Subject must be '.QrCodeUsage::class);
}
$qrCode = $qrCodeUsage->getQrCode();
if (!$qrCode) {
throw new \LogicException('QrCode must not be null');
}
if (!in_array($qrCode->getStatus(), [QrCodeStatusType::CREATED, QrCodeStatusType::USING])) {
$event->addTransitionBlocker(new TransitionBlocker('このQRコードは利用できません。', '0'));
} else if (!$this->deviceValidatorResolver->resolve($qrCode)->validate($qrCodeUsage)) {
$event->addTransitionBlocker(new TransitionBlocker('このデバイスからは利用できないQRコードです。', '0'));
} else if (!$this->usableValidatorResolver->resolve($qrCode)->validate($qrCodeUsage)) {
$event->addTransitionBlocker(new TransitionBlocker('このQRコードは利用できません。', '0'));
}
}
public function onEnteredChecked(Event $event): void
{
/** @var QrCodeUsage */
$qrCodeUsage = $event->getSubject();
if (!$qrCodeUsage instanceof QrCodeUsage) {
throw new \LogicException('Subject must be '.QrCodeUsage::class);
}
$qrCode = $qrCodeUsage->getQrCode();
if (!$qrCode) {
throw new \LogicException('QrCode must not be null');
}
$qrCode->addQrCodeUsage($qrCodeUsage);
}
/**
* 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_usage_states.guard.check' => ['guardCheck'],
'workflow.qrcode_usage_states.entered.checked' => ['onEnteredChecked'],
];
}
}