<?php/** * Created by PhpStorm. * User: bertrand * Date: 29/03/2019 * Time: 13:53 */namespace App\Listener;use App\Entity\Enum\ProspectOnStoreCreationModeEnum;use App\Entity\Freelancer;use App\Entity\ProspectOnStore;use App\Entity\ProspectQualification;use App\Repository\AcquisitionModeRepository;use App\Service\DeliveryService;use App\Service\ProspectOnStoreQualificationService;use App\Service\ProspectService;use Psr\Log\LoggerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Workflow\Event\Event;class ProspectOnStoreQualificationWorkflowListener implements EventSubscriberInterface{ private LoggerInterface $logger; private DeliveryService $deliveryService; private ProspectService $prospectService; private AcquisitionModeRepository $acquisitionModeRepository; /** * ProspectQualificationWorkflowListener constructor. * @param DeliveryService $deliveryService * @param ProspectService $prospectService * @param ProspectOnStoreQualificationService $prospectOnStoreQualificationService * @param AcquisitionModeRepository $acquisitionModeRepository * @param LoggerInterface $logger */ public function __construct( DeliveryService $deliveryService, ProspectService $prospectService, ProspectOnStoreQualificationService $prospectOnStoreQualificationService, AcquisitionModeRepository $acquisitionModeRepository, LoggerInterface $logger) { $this->logger = $logger; $this->deliveryService = $deliveryService; $this->prospectService = $prospectService; $this->acquisitionModeRepository = $acquisitionModeRepository; } public static function getSubscribedEvents(): array { return [ 'workflow.prospectOnStoreQualification.completed.validate' => 'onValidate', 'workflow.prospectOnStoreQualification.completed.reject' => 'onReject', 'workflow.prospectOnStoreQualification.completed.unreach' => 'onUnreach', ]; } public function onValidate(Event $event) { $this->logger->info('ProspectOnStoreQualificationWorflowListener onValidate'); $prospectOnStoreQualification = $event->getSubject(); $prospectOnStore = $prospectOnStoreQualification->getProspectOnStore(); $this->logger->info('ProspectOnStoreQualificationWorflowListener.onValidate POS: ' . $prospectOnStore->getId()); $prospectOnStoreQualification->setEndedAt(new \DateTime()); //set validation date $prospectOnStoreQualification->setValidatedAt(new \DateTime()); //deliver prospect if ($prospectOnStore->isWaitingQualification()) { $this->logger->info('POS was waiting qualification->deliver'); $this->logger->info('deliver this POS'); $this->deliveryService->deliver($prospectOnStore); } } public function onReject(Event $event) { $prospectOnStoreQualification = $event->getSubject(); $prospectOnStore = $prospectOnStoreQualification->getProspectOnStore(); $this->logger->info('ProspectOnStoreQualificationWorflowListener.onReject POS: ' . $prospectOnStore->getId()); $prospectOnStoreQualification->setEndedAt(new \DateTime()); } /** * Manage prospect unreachable * @param Event $event */ public function onUnReach(Event $event) { $prospectOnStoreQualification = $event->getSubject(); $prospectOnStore = $prospectOnStoreQualification->getProspectOnStore(); $this->logger->info('ProspectOnStoreQualificationWorflowListener.onUnReach POS: ' . $prospectOnStore->getId()); $prospectOnStoreQualification->setEndedAt(new \DateTime()); $this->redeliverOnCondition($prospectOnStore); } private function redeliverOnCondition(ProspectOnStore $prospectOnStore) { $this->logger->info('redeliverOnCondition: '.$prospectOnStore->getId()); //AcquisitionMode ' . $prospectOnStore->getAcquisitionMode() . ' : ' . $prospectOnStore->getAcquisitionMode()->isRedeliverOnUnReach()); $this->logger->info('searching with mode='.$prospectOnStore->getAcquisitionMode().' and base='.$prospectOnStore->getAcquisitionBase()); $acquisitionMode = $this->acquisitionModeRepository->findAcquisitionModeOfPos($prospectOnStore); $this->logger->info($acquisitionMode?'acquisitionMode id='.$acquisitionMode->getId():' none find'); if ($acquisitionMode && $acquisitionMode->isRedeliverOnUnReach()) { $this->logger->info('acquisitionMode is isRedeliverOnUnReach'); $this->prospectService->deliverProspectOnStoreBelongingToADifferentPartner($prospectOnStore->getProspect(), ProspectOnStoreCreationModeEnum::UNREACH); }else{ $this->logger->info('acquisitionMode prevent from delivering'); } }}