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

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. class ShippingSubscriber implements EventSubscriberInterface
  19. {
  20.     private ShippingService $shippingService;
  21.     private LoggerInterface $logger;
  22.     public function __construct(
  23.         ShippingService $shippingService,
  24.         LoggerInterface $logger
  25.     ) {
  26.         $this->shippingService $shippingService;
  27.         $this->logger $logger;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             PreWriteValidationEvent::class => 'triggerChangeSet',
  33.             OrderEvents::ORDER_DELIVERY_WRITTEN_EVENT => 'onOrderDeliveryWritten',
  34.         ];
  35.     }
  36.     public function triggerChangeSet(PreWriteValidationEvent $event): void
  37.     {
  38.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  39.             return;
  40.         }
  41.         foreach ($event->getCommands() as $command) {
  42.             if (!$command instanceof ChangeSetAware) {
  43.                 continue;
  44.             }
  45.             if ($command->getDefinition()->getEntityName() !== OrderDeliveryDefinition::ENTITY_NAME) {
  46.                 continue;
  47.             }
  48.             if (!isset($command->getPayload()['tracking_codes'])) {
  49.                 continue;
  50.             }
  51.             $command->requestChangeSet();
  52.         }
  53.     }
  54.     public function onOrderDeliveryWritten(EntityWrittenEvent $event): void
  55.     {
  56.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  57.             return;
  58.         }
  59.         foreach ($event->getWriteResults() as $writeResult) {
  60.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_DELETE) {
  61.                 continue;
  62.             }
  63.             $before = [];
  64.             $changeSet $writeResult->getChangeSet();
  65.             if ($changeSet !== null && !$changeSet->hasChanged('tracking_codes')) {
  66.                 continue;
  67.             }
  68.             if ($changeSet !== null) {
  69.                 $codesBefore $changeSet->getBefore('tracking_codes') ?? [];
  70.                 $before = !\is_array($codesBefore) ? \json_decode($codesBefore) : [];
  71.             }
  72.             $orderDeliveryId $writeResult->getPrimaryKey();
  73.             $after $writeResult->getProperty('trackingCodes') ?? [];
  74.             if (!\is_string($orderDeliveryId) || !\is_array($after) || !\is_array($before)) {
  75.                 continue;
  76.             }
  77.             try {
  78.                 $this->shippingService->updateTrackingCodes($orderDeliveryId$after$before$event->getContext());
  79.             } catch (\Throwable $e) {
  80.                 $this->logger->warning('Could not update tracking codes', [
  81.                     'exception' => $e,
  82.                     'trace' => $e->getTraceAsString(),
  83.                 ]);
  84.             }
  85.         }
  86.     }
  87. }