<?php
/**
* This file is part of the CosaVostra, TrackPay, Symfony package.
*
* (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\EventSubscriber\Procedure;
use App\Entity\ProcedureStep;
use App\Event\EmptyInvoiceContactEvent;
use App\Event\ProcedureDispatchEvent;
use App\Events;
use App\Manager\ProcedureManager;
use App\Manager\ProcedureStepManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProcedureDispatchSubscriber implements EventSubscriberInterface
{
/**
* @var ProcedureStepManager
*/
protected $procedureStepManager;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var ProcedureManager
*/
protected $procedureManager;
/**
* ProcedureDispatchSubscriber constructor.
*
* @param ProcedureStepManager $procedureStepManager
* @param EventDispatcherInterface $eventDispatcher
* @param ProcedureManager $procedureManager
*/
public function __construct(ProcedureStepManager $procedureStepManager, EventDispatcherInterface $eventDispatcher, ProcedureManager $procedureManager)
{
$this->procedureStepManager = $procedureStepManager;
$this->eventDispatcher = $eventDispatcher;
$this->procedureManager = $procedureManager;
}
/**
* @param ProcedureDispatchEvent $event
*/
public function onProcedureDispatch(ProcedureDispatchEvent $event): void
{
/** @var ProcedureStep $procedureStep */
$procedureStep = $event->getPendingStep();
if (null === $procedureStep || $procedureStep->getStatus() !== ProcedureStep::STATUS_STEP_PENDING) {
return;
}
$this->procedureStepManager->send($procedureStep, $event);
}
public function onProcedureBeforeDispatch(ProcedureDispatchEvent $event): void
{
$procedure = $event->getProcedure();
$invoice = $procedure->getInvoice();
if (null !== $category = $procedure->getWorkflow()->getCategory()) {
if ($category->getName() === 'category.no_treat_title') {
return;
}
}
// Check if the invoice customer contacts is empty
if (!$invoice->containsAtLeastOneCustomerContact()) {
// Dispatch empty invoice contact event (send notification email)
$this->eventDispatcher->dispatch(new EmptyInvoiceContactEvent($invoice));
// We need to pause procedure here.
$this->procedureManager->pause($procedure, 1);
$event->setAllowed(false);
}
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
Events::PROCEDURE_ON_DISPATCH => 'onProcedureDispatch',
Events::PROCEDURE_BEFORE_DISPATCH => 'onProcedureBeforeDispatch'
];
}
}