vendor/bitbag/shipping-export-plugin/src/EventListener/PlacingShipmentForGatewayEventListener.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusShippingExportPlugin\EventListener;
  9. use BitBag\SyliusShippingExportPlugin\Entity\ShippingExportInterface;
  10. use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface;
  11. use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface;
  12. use BitBag\SyliusShippingExportPlugin\Repository\ShippingGatewayRepositoryInterface;
  13. use Sylius\Component\Core\Model\OrderInterface;
  14. use Sylius\Component\Core\Model\ShipmentInterface;
  15. use Sylius\Component\Resource\Factory\FactoryInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Webmozart\Assert\Assert;
  18. final class PlacingShipmentForGatewayEventListener
  19. {
  20.     /** @var ShippingGatewayRepositoryInterface */
  21.     private $shippingGatewayRepository;
  22.     /** @var ShippingExportRepositoryInterface */
  23.     private $shippingExportRepository;
  24.     /** @var FactoryInterface */
  25.     private $shippingExportFactory;
  26.     public function __construct(
  27.         ShippingGatewayRepositoryInterface $shippingGatewayRepository,
  28.         ShippingExportRepositoryInterface $shippingExportRepository,
  29.         FactoryInterface $shippingExportFactory
  30.     ) {
  31.         $this->shippingGatewayRepository $shippingGatewayRepository;
  32.         $this->shippingExportRepository $shippingExportRepository;
  33.         $this->shippingExportFactory $shippingExportFactory;
  34.     }
  35.     public function prepareShippingExport(GenericEvent $event): void
  36.     {
  37.         $order $event->getSubject();
  38.         Assert::isInstanceOf($orderOrderInterface::class);
  39.         if (=== count($order->getShipments())) {
  40.             return;
  41.         }
  42.         /** @var ShipmentInterface $shipment */
  43.         foreach ($order->getShipments() as $shipment) {
  44.             $shippingMethod $shipment->getMethod();
  45.             Assert::notNull($shippingMethod);
  46.             $shippingGateway $this->shippingGatewayRepository->findOneByShippingMethod($shippingMethod);
  47.             if ($shippingGateway instanceof ShippingGatewayInterface) {
  48.                 /** @var ShippingExportInterface $shippingExport */
  49.                 $shippingExport $this->shippingExportFactory->createNew();
  50.                 $shippingExport->setShippingGateway($shippingGateway);
  51.                 $shippingExport->setShipment($shipment);
  52.                 $this->shippingExportRepository->add($shippingExport);
  53.             }
  54.         }
  55.     }
  56. }