ERP/ApprovalBundle/EventSubscriber/ApprovalCreatedSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ERP\ApprovalBundle\EventSubscriber;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use ERP\ApprovalBundle\Entity\Approval;
  6. use ERP\ApprovalBundle\Repository\ApprovalRepository;
  7. use Hashids\Hashids;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Component\Workflow\Registry;
  15. class ApprovalCreatedSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var Registry
  19.      */
  20.     private $registry;
  21.     /**
  22.      * @var MessageBusInterface
  23.      */
  24.     private $bus;
  25.     /**
  26.      * @var LoggerInterface
  27.      */
  28.     private $logger;
  29.     /**
  30.      * @var ApprovalRepository
  31.      */
  32.     private $approvalRepository;
  33.     private ParameterBagInterface  $parameterBag;
  34.     public function __construct(
  35.         Registry $registry,
  36.         MessageBusInterface $bus,
  37.         LoggerInterface $logger,
  38.         ApprovalRepository $approvalRepository,
  39.         ParameterBagInterface $parameterBag
  40.     ) {
  41.         $this->registry $registry;
  42.         $this->bus $bus;
  43.         $this->logger $logger;
  44.         $this->approvalRepository $approvalRepository;
  45.         $this->parameterBag $parameterBag;
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             KernelEvents::VIEW => ['persist'EventPriorities::POST_WRITE],
  51.         ];
  52.     }
  53.     public function persist(ViewEvent $event)
  54.     {
  55.         /** @var Approval $approval */
  56.         $approval $event->getControllerResult();
  57.         if (!$approval instanceof Approval) {
  58.             return;
  59.         }
  60.         // TMP skip for tests because php extension is missing
  61.         if ('test' !== $this->parameterBag->get('env')) {
  62.             $approvalHash = new Hashids(''Approval::HASH_MINIMUM_LENGTH);
  63.             $approval->setHash($approvalHash->encodeHex($approval->getId()));
  64.         }
  65.         $this->approvalRepository->store($approval);
  66.     }
  67. }