src/V4/Module/DolibarrApi/EventSubscriber/EntityEventSubscriber.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\V4\Module\DolibarrApi\EventSubscriber;
  3. use App\DataPersister\AbstractDataPersister;
  4. use App\Export\Builder\ExportBuilderInterface;
  5. use App\Export\Builder\ExporterInterface;
  6. use App\Model\Exception\ModuleNotFoundException;
  7. use App\Security\SecurityConfig;
  8. use App\V4\Event\PostPersistEvent;
  9. use App\V4\EventSubscriber\AbstractSubscriber;
  10. use App\V4\Model\Contact\Contact;
  11. use App\V4\Model\Prospect\Prospect;
  12. use App\V4\Model\Quote\Quote;
  13. use App\V4\Module\DolibarrApi\Controller\QuoteDataPersisterAction;
  14. use App\V4\Module\DolibarrApi\Exception\NotFoundException;
  15. use App\V4\Module\DolibarrApi\Service\ConfigurationService;
  16. use App\V4\Module\DolibarrApi\Service\SendDataService;
  17. use App\V4\Validator\CustomAction\Builder\CustomActionValidatorBuilder;
  18. use App\V4\Validator\CustomActionValidator;
  19. use ReflectionException;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  23. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  24. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  25. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  26. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  27. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  28. class EntityEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var SendDataService
  32.      */
  33.     private $sendDataService;
  34.     /**
  35.      * @var ConfigurationService
  36.      */
  37.     private $configurationService;
  38.     /**
  39.      * @var CustomActionValidatorBuilder
  40.      */
  41.     private $customActionValidatorBuilder;
  42.     /**
  43.      * @var ExportBuilderInterface
  44.      */
  45.     private $exportBuilder;
  46.     /**
  47.      * @var Security
  48.      */
  49.     private $security;
  50.     public function __construct(
  51.         SendDataService $sendDataService,
  52.         ConfigurationService $configurationService,
  53.         CustomActionValidatorBuilder $customActionValidatorBuilder,
  54.         ExportBuilderInterface $exportBuilder,
  55.         Security $security
  56.     ) {
  57.         $this->sendDataService $sendDataService;
  58.         $this->configurationService $configurationService;
  59.         $this->customActionValidatorBuilder $customActionValidatorBuilder;
  60.         $this->exportBuilder $exportBuilder;
  61.         $this->security $security;
  62.     }
  63.     public static function getSubscribedEvents(): array
  64.     {
  65.         return [
  66.             PostPersistEvent::NAME => 'onPostPersist',
  67.         ];
  68.     }
  69.     public function supports(PostPersistEvent $event): bool
  70.     {
  71.         return  $this->security->isGranted(SecurityConfig::MODULE_DOLIBARR_API)
  72.             && (
  73.                 $event->getAfter() instanceof Prospect
  74.                 || $event->getAfter() instanceof Contact
  75.                 || $event->getAfter() instanceof Quote
  76.             )
  77.             && $this->supportsConfiguration($event)
  78.         ;
  79.     }
  80.     /**
  81.      * @param PostPersistEvent $event
  82.      *
  83.      * @throws ExceptionInterface
  84.      * @throws ModuleNotFoundException
  85.      * @throws NotFoundException
  86.      * @throws ReflectionException
  87.      * @throws ClientExceptionInterface
  88.      * @throws DecodingExceptionInterface
  89.      * @throws RedirectionExceptionInterface
  90.      * @throws ServerExceptionInterface
  91.      * @throws TransportExceptionInterface
  92.      */
  93.     public function onPostPersist(PostPersistEvent $event)
  94.     {
  95.         $context $event->getContext();
  96.         if (!empty($context[AbstractDataPersister::CONTEXT_ORIGIN]) &&
  97.             QuoteDataPersisterAction::FROM_DOLIBARR_PERSIST === $context[AbstractDataPersister::CONTEXT_ORIGIN]
  98.         ) {
  99.             return;
  100.         }
  101.         $entity $event->getAfter();
  102.         if (!$this->supports($event) || $this->isIdAlreadyManaged($event->getAfter()->getId(), get_class($this))) {
  103.             return;
  104.         }
  105.         if ($entity instanceof Prospect) {
  106.             /* @var Prospect $entity */
  107.             $this->sendDataService->sendProspectToService(
  108.                 get_class($entity),
  109.                 $entity
  110.             );
  111.         }
  112.         if ($entity instanceof Contact) {
  113.             /* @var Contact $entity */
  114.             $this->sendDataService->sendProspectContactToService(
  115.                 get_class($entity),
  116.                 $entity
  117.             );
  118.         }
  119.         if ($entity instanceof Quote) {
  120.             $dolibarrStatus $entity->getStatus()->getIsWon() ? : ($entity->getStatus()->getIsLost() ? null);
  121.             if (null !== $dolibarrStatus) {
  122.                 /* @var Quote $entity */
  123.                 $this->sendDataService->sendQuoteToService(
  124.                     get_class($entity),
  125.                     $entity,
  126.                     ['status' => $dolibarrStatus]
  127.                 );
  128.             }
  129.         }
  130.     }
  131.     private function supportsConfiguration(PostPersistEvent $event): bool
  132.     {
  133.         $configuration $this->configurationService->getEventListenersByEntity(get_class($event->getAfter()));
  134.         if (!array_key_exists('onPostPersist'$configuration)) {
  135.             return false;
  136.         }
  137.         if (empty($configuration['onPostPersist'])) {
  138.             return true;
  139.         }
  140.         /** @var Prospect|Quote $object */
  141.         $object $event->getAfter();
  142.         foreach ($configuration['onPostPersist'][CustomActionValidator::CUSTOM_ACTION_CONDITIONS] as $condition) {
  143.             $value $this->exportBuilder
  144.                 ->getExporter($object$condition[CustomActionValidator::CUSTOM_ACTION_CONDITIONS_KEY], ExporterInterface::EXPORT_TYPE_LISTING)
  145.                 ->convert($object$condition[CustomActionValidator::CUSTOM_ACTION_CONDITIONS_KEY]);
  146.             if (isset($condition[CustomActionValidator::CUSTOM_ACTION_CONDITIONS_KEY])
  147.                 && !$this->customActionValidatorBuilder->getValidator($value$condition)->isValid($value$condition)) {
  148.                 return false;
  149.             }
  150.         }
  151.         return true;
  152.     }
  153. }