src/EventListener/Workflow/Task/QrCodeUsage/QrCodeUsageCheckListener.php line 70

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: takabayashi
  5.  * Date: 2019/09/19
  6.  * Time: 20:48
  7.  */
  8. declare(strict_types=1);
  9. namespace App\EventListener\Workflow\Task\QrCodeUsage;
  10. use App\DBAL\Types\QrCodeStatusType;
  11. use App\Entity\QrCodeUsage;
  12. use App\Service\QrCode\DeviceValidator\DeviceValidatorResolverInterface;
  13. use App\Service\QrCode\UsableValidator\UsableValidatorResolverInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Workflow\Event\Event;
  16. use Symfony\Component\Workflow\Event\GuardEvent;
  17. use Symfony\Component\Workflow\TransitionBlocker;
  18. class QrCodeUsageCheckListener implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var DeviceValidatorResolverInterface
  22.      */
  23.     private $deviceValidatorResolver;
  24.     /**
  25.      * @var UsableValidatorResolverInterface
  26.      */
  27.     private $usableValidatorResolver;
  28.     /**
  29.      * QrCodeUsageUseCase constructor.
  30.      * @param DeviceValidatorResolverInterface $deviceValidatorResolver
  31.      * @param UsableValidatorResolverInterface $usableValidatorResolver
  32.      */
  33.     public function __construct(
  34.         DeviceValidatorResolverInterface $deviceValidatorResolver,
  35.         UsableValidatorResolverInterface $usableValidatorResolver
  36.     ) {
  37.         $this->deviceValidatorResolver $deviceValidatorResolver;
  38.         $this->usableValidatorResolver $usableValidatorResolver;
  39.     }
  40.     public function guardCheck(GuardEvent $event): void
  41.     {
  42.         /** @var QrCodeUsage */
  43.         $qrCodeUsage $event->getSubject();
  44.         if (!$qrCodeUsage instanceof QrCodeUsage) {
  45.             throw new \LogicException('Subject must be '.QrCodeUsage::class);
  46.         }
  47.         $qrCode $qrCodeUsage->getQrCode();
  48.         if (!$qrCode) {
  49.             throw new \LogicException('QrCode must not be null');
  50.         }
  51.         if (!in_array($qrCode->getStatus(), [QrCodeStatusType::CREATEDQrCodeStatusType::USING])) {
  52.             $event->addTransitionBlocker(new TransitionBlocker('このQRコードは利用できません。''0'));
  53.         } else if (!$this->deviceValidatorResolver->resolve($qrCode)->validate($qrCodeUsage)) {
  54.             $event->addTransitionBlocker(new TransitionBlocker('このデバイスからは利用できないQRコードです。''0'));
  55.         } else if (!$this->usableValidatorResolver->resolve($qrCode)->validate($qrCodeUsage)) {
  56.             $event->addTransitionBlocker(new TransitionBlocker('このQRコードは利用できません。''0'));
  57.         }
  58.     }
  59.     public function onEnteredChecked(Event $event): void
  60.     {
  61.         /** @var QrCodeUsage */
  62.         $qrCodeUsage $event->getSubject();
  63.         if (!$qrCodeUsage instanceof QrCodeUsage) {
  64.             throw new \LogicException('Subject must be '.QrCodeUsage::class);
  65.         }
  66.         $qrCode $qrCodeUsage->getQrCode();
  67.         if (!$qrCode) {
  68.             throw new \LogicException('QrCode must not be null');
  69.         }
  70.         $qrCode->addQrCodeUsage($qrCodeUsage);
  71.     }
  72.     /**
  73.      * Returns an array of event names this subscriber wants to listen to.
  74.      *
  75.      * The array keys are event names and the value can be:
  76.      *
  77.      *  * The method name to call (priority defaults to 0)
  78.      *  * An array composed of the method name to call and the priority
  79.      *  * An array of arrays composed of the method names to call and respective
  80.      *    priorities, or 0 if unset
  81.      *
  82.      * For instance:
  83.      *
  84.      *  * ['eventName' => 'methodName']
  85.      *  * ['eventName' => ['methodName', $priority]]
  86.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  87.      *
  88.      * @return array The event names to listen to
  89.      */
  90.     public static function getSubscribedEvents()
  91.     {
  92.         return [
  93.             'workflow.qrcode_usage_states.guard.check' => ['guardCheck'],
  94.             'workflow.qrcode_usage_states.entered.checked' => ['onEnteredChecked'],
  95.         ];
  96.     }
  97. }