custom/plugins/rapi1Connector/src/Subscribers/UpdatedCustomerOrderEntitySubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rapidmail\Shopware\Subscribers;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Checkout\Order\OrderEvents;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. class UpdatedCustomerOrderEntitySubscriber extends UpdatedCustomerRelatedEntitySubscriber
  14. {
  15.     public const SUBSCRIBED_TO_ENTITY_NAME 'order';
  16.     private EntityRepositoryInterface $orderRepository;
  17.     public function __construct(
  18.         EntityRepositoryInterface $customerRepository,
  19.         EntityRepositoryInterface $orderRepository,
  20.         LoggerInterface $logger
  21.     ) {
  22.         parent::__construct($customerRepository$logger);
  23.         $this->orderRepository $orderRepository;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             OrderEvents::ORDER_WRITTEN_EVENT => 'onWrittenOrder',
  29.         ];
  30.     }
  31.     public function onWrittenOrder(EntityWrittenEvent $event): void
  32.     {
  33.         $context $event->getContext();
  34.         $writeResult $event->getWriteResults()[0] ?? null;
  35.         if (!($writeResult instanceof EntityWriteResult)) {
  36.             return;
  37.         }
  38.         $payload $writeResult->getPayload();
  39.         if ($this->isAllowed($event$payload)) {
  40.             $customerId $this->getCustomerId($payload['id'], $context);
  41.             if ($customerId) {
  42.                 $this->updateCustomer($customerId$context);
  43.             }
  44.         }
  45.     }
  46.     protected function isAllowed(EntityWrittenEvent $event, ?array $data): bool
  47.     {
  48.         return (
  49.             $event->getEntityName() === self::SUBSCRIBED_TO_ENTITY_NAME &&
  50.             is_array($data) &&
  51.             !empty($data['id'])
  52.         );
  53.     }
  54.     private function getCustomerId(string $orderIdContext $context): ?string
  55.     {
  56.         $criteria = new Criteria([$orderId]);
  57.         $criteria->addAssociation('orderCustomer');
  58.         $order $this->orderRepository
  59.             ->search($criteria$context)
  60.             ->first();
  61.         if ($order instanceof OrderEntity) {
  62.             $orderCustomer $order->getOrderCustomer();
  63.             if ($orderCustomer instanceof OrderCustomerEntity) {
  64.                 return $orderCustomer->getCustomerId();
  65.             }
  66.         }
  67.         return null;
  68.     }
  69. }