custom/plugins/SwagPayPal/src/Checkout/Order/Shipping/ShippingSubscriber.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\PayPal\Checkout\Order\Shipping;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryDefinition;
  10. use Shopware\Core\Checkout\Order\OrderEvents;
  11. use Shopware\Core\Defaults;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  16. use Swag\PayPal\Checkout\Order\Shipping\Service\ShippingService;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * @internal
  20.  */
  21. class ShippingSubscriber implements EventSubscriberInterface
  22. {
  23.     private ShippingService $shippingService;
  24.     private LoggerInterface $logger;
  25.     public function __construct(
  26.         ShippingService $shippingService,
  27.         LoggerInterface $logger
  28.     ) {
  29.         $this->shippingService $shippingService;
  30.         $this->logger $logger;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             PreWriteValidationEvent::class => 'triggerChangeSet',
  36.             OrderEvents::ORDER_DELIVERY_WRITTEN_EVENT => 'onOrderDeliveryWritten',
  37.         ];
  38.     }
  39.     public function triggerChangeSet(PreWriteValidationEvent $event): void
  40.     {
  41.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  42.             return;
  43.         }
  44.         foreach ($event->getCommands() as $command) {
  45.             if (!$command instanceof ChangeSetAware) {
  46.                 continue;
  47.             }
  48.             if ($command->getDefinition()->getEntityName() !== OrderDeliveryDefinition::ENTITY_NAME) {
  49.                 continue;
  50.             }
  51.             if (!isset($command->getPayload()['tracking_codes'])) {
  52.                 continue;
  53.             }
  54.             $command->requestChangeSet();
  55.         }
  56.     }
  57.     public function onOrderDeliveryWritten(EntityWrittenEvent $event): void
  58.     {
  59.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  60.             return;
  61.         }
  62.         foreach ($event->getWriteResults() as $writeResult) {
  63.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_DELETE) {
  64.                 continue;
  65.             }
  66.             $before = [];
  67.             $changeSet $writeResult->getChangeSet();
  68.             if ($changeSet !== null && !$changeSet->hasChanged('tracking_codes')) {
  69.                 continue;
  70.             }
  71.             if ($changeSet !== null) {
  72.                 $codesBefore $changeSet->getBefore('tracking_codes') ?? [];
  73.                 $before = !\is_array($codesBefore) ? \json_decode($codesBefore) : [];
  74.             }
  75.             $orderDeliveryId $writeResult->getPrimaryKey();
  76.             $after $writeResult->getProperty('trackingCodes') ?? [];
  77.             if (!\is_string($orderDeliveryId) || !\is_array($after) || !\is_array($before)) {
  78.                 continue;
  79.             }
  80.             try {
  81.                 $this->shippingService->updateTrackingCodes($orderDeliveryId$after$before$event->getContext());
  82.             } catch (\Throwable $e) {
  83.                 $this->logger->warning('Could not update tracking codes', [
  84.                     'exception' => $e,
  85.                     'trace' => $e->getTraceAsString(),
  86.                 ]);
  87.             }
  88.         }
  89.     }
  90. }