<?php
declare(strict_types=1);
namespace ERP\ApprovalBundle\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use ERP\ApprovalBundle\Entity\Approval;
use ERP\ApprovalBundle\Repository\ApprovalRepository;
use Hashids\Hashids;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Registry;
class ApprovalCreatedSubscriber implements EventSubscriberInterface
{
/**
* @var Registry
*/
private $registry;
/**
* @var MessageBusInterface
*/
private $bus;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ApprovalRepository
*/
private $approvalRepository;
private ParameterBagInterface $parameterBag;
public function __construct(
Registry $registry,
MessageBusInterface $bus,
LoggerInterface $logger,
ApprovalRepository $approvalRepository,
ParameterBagInterface $parameterBag
) {
$this->registry = $registry;
$this->bus = $bus;
$this->logger = $logger;
$this->approvalRepository = $approvalRepository;
$this->parameterBag = $parameterBag;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['persist', EventPriorities::POST_WRITE],
];
}
public function persist(ViewEvent $event)
{
/** @var Approval $approval */
$approval = $event->getControllerResult();
if (!$approval instanceof Approval) {
return;
}
// TMP skip for tests because php extension is missing
if ('test' !== $this->parameterBag->get('env')) {
$approvalHash = new Hashids('', Approval::HASH_MINIMUM_LENGTH);
$approval->setHash($approvalHash->encodeHex($approval->getId()));
}
$this->approvalRepository->store($approval);
}
}