vendor/bluue/prestashop-connector-bundle/src/EventSubscriber/EditStateForOrderSubscriber.php line 62

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Thomas HERISSON (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\PrestashopConnectorBundle\EventSubscriber;
  9. use App\Services\Configuration;
  10. use Bluue\ApiCmsBundle\Repository\MappingRepository;
  11. use Bluue\SalesBundle\Entity\Order;
  12. use Bluue\SalesBundle\Event\EditStateForOrderEvent;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Bluue\PrestashopConnectorBundle\Message\UpdateStateForOrderToPrestaMessage;
  16. class EditStateForOrderSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var MessageBusInterface $messageBus
  20.      */
  21.     protected MessageBusInterface $messageBus;
  22.     /**
  23.      * @var MappingRepository
  24.      */
  25.     protected MappingRepository $mappingRepo;
  26.     /**
  27.      * @var Configuration
  28.      */
  29.     protected Configuration $configuration;
  30.     public function __construct(
  31.         MessageBusInterface $messageBus,
  32.         MappingRepository $mappingRepo,
  33.         Configuration $configuration
  34.     ) {
  35.         $this->messageBus $messageBus;
  36.         $this->mappingRepo $mappingRepo;
  37.         $this->configuration $configuration;
  38.     }
  39.     /**
  40.      * @return string[]
  41.      */
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             EditStateForOrderEvent::POST_SUBMIT => 'postSubmit'
  46.         ];
  47.     }
  48.     /**
  49.      * @param EditStateForOrderEvent $event
  50.      * @return void
  51.      */
  52.     public function postSubmit(EditStateForOrderEvent $event): void
  53.     {
  54.         $order $event->getOrder();
  55.         $context $order->getContext();
  56.         $this->messageBus->dispatch(new UpdateStateForOrderToPrestaMessage(
  57.             $order->getId()->toRfc4122(),
  58.             $order->getOrderState()->getId()->toRfc4122()
  59.         ));
  60.         $generateInvoice $this->configuration->get(
  61.             'prestashop_connector_bundle__invoices_generated_in_bluue',
  62.             $context
  63.         );
  64.         $invoiceLimitDate $this->configuration->get(
  65.             'prestashop_connector_bundle__limit_invoice_date',
  66.             $context
  67.         );
  68.         $mappingOrder $this->mappingRepo->findMapping(
  69.             $order->getId(),
  70.             'prestashop',
  71.             Order::class,
  72.             null,
  73.             $context->getId()
  74.         );
  75.         if (
  76.             $mappingOrder
  77.             && (
  78.                 !$generateInvoice
  79.                 || !$invoiceLimitDate
  80.                 || $order->getCreatedAt()->format('Y-m-d') < $invoiceLimitDate
  81.             )
  82.         ) {
  83.             $event->setCancelInvoice(true);
  84.         }
  85.     }
  86. }