custom/plugins/EcocodeTrustpilot/src/Frontend/Event/OrderPlacedSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ecocode\Trustpilot\Frontend\Event;
  4. use Ecocode\Trustpilot\Model\Message\InitiateInvitationMessage;
  5. use Ecocode\Trustpilot\Service\TrustpilotConfigurationService;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\Framework\Context;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Contracts\EventDispatcher\Event;
  13. class OrderPlacedSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var TrustpilotConfigurationService
  17.      */
  18.     protected $configurationService;
  19.     /**
  20.      * @var MessageBusInterface
  21.      */
  22.     private $messageBus;
  23.     public function __construct(MessageBusInterface $messageBusTrustpilotConfigurationService $configurationService)
  24.     {
  25.         $this->messageBus           $messageBus;
  26.         $this->configurationService $configurationService;
  27.     }
  28.     /** @codeCoverageIgnore
  29.      * @return array<string,string>
  30.      */
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             CheckoutOrderPlacedEvent::class            => 'orderEvent',
  35.             'state_enter.order_delivery.state.shipped' => 'orderEvent',
  36.             'state_enter.order.state.completed'        => 'orderEvent'
  37.         ];
  38.     }
  39.     /**
  40.      * @param CheckoutOrderPlacedEvent|OrderStateMachineStateChangeEvent $event
  41.      */
  42.     public function orderEvent(Event $event): void
  43.     {
  44.         $salesChannelId $event->getSalesChannelId();
  45.         if (!$this->configurationService->isConfigured()) {
  46.             return;
  47.         }
  48.         if (!$this->configurationService->isInvitationSendingActive($salesChannelId)) {
  49.             return;
  50.         }
  51.         if ($event->getName() !== $this->configurationService->getSubscribedInvitationEvent($salesChannelId)) {
  52.             return;
  53.         }
  54.         $this->createOrderMessage($event->getOrder(), $event->getContext());
  55.     }
  56.     private function createOrderMessage(OrderEntity $orderContext $context): void
  57.     {
  58.         $evaluationInvitationMessage = new InitiateInvitationMessage();
  59.         $evaluationInvitationMessage
  60.             ->setOrderIds([$order->getId()])
  61.             ->withContext($context);
  62.         $this->messageBus->dispatch($evaluationInvitationMessage);
  63.     }
  64. }