custom/plugins/DvsnReturnShipment/src/Subscriber/Core/Checkout/Order/OrderEventsSubscriber.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * digitvision
  4.  *
  5.  * @category  digitvision
  6.  * @package   Shopware\Plugins\DvsnReturnShipment
  7.  * @copyright (c) 2020 digitvision
  8.  */
  9. namespace Dvsn\ReturnShipment\Subscriber\Core\Checkout\Order;
  10. use Dvsn\ReturnShipment\Core\Checkout\Order\Aggregate\ReturnShipment\ReturnShipmentStruct;
  11. use Dvsn\ReturnShipment\Core\Checkout\ReturnShipment\ReturnShipmentEntity;
  12. use Dvsn\ReturnShipment\Service\DeadlineValidatorServiceInterface;
  13. use Dvsn\ReturnShipment\Service\Order\ValidatorServiceInterface;
  14. use Shopware\Core\Checkout\Order\OrderEntity;
  15. use Shopware\Core\Checkout\Order\OrderEvents;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  20. use Shopware\Core\System\SystemConfig\SystemConfigService;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  23. class OrderEventsSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * ...
  27.      *
  28.      * @var EntityRepository
  29.      */
  30.     protected $returnShipmentRepository;
  31.     /**
  32.      * ...
  33.      *
  34.      * @var EntityRepository
  35.      */
  36.     protected $returnShipmentLineItemRepository;
  37.     /**
  38.      * ...
  39.      *
  40.      * @var SystemConfigService
  41.      */
  42.     protected $systemConfigService;
  43.     /**
  44.      * ...
  45.      *
  46.      * @var ValidatorServiceInterface
  47.      */
  48.     protected $orderValidatorService;
  49.     /**
  50.      * ...
  51.      *
  52.      * @var DeadlineValidatorServiceInterface
  53.      */
  54.     protected $deadlineValidatorService;
  55.     /**
  56.      * ...
  57.      *
  58.      * @param EntityRepository $returnShipmentRepository
  59.      * @param EntityRepository $returnShipmentLineItemRepository
  60.      * @param SystemConfigService $systemConfigService
  61.      * @param ValidatorServiceInterface $orderValidatorService
  62.      * @param DeadlineValidatorServiceInterface $deadlineValidatorService
  63.      */
  64.     public function __construct(EntityRepository $returnShipmentRepositoryEntityRepository $returnShipmentLineItemRepositorySystemConfigService $systemConfigServiceValidatorServiceInterface $orderValidatorServiceDeadlineValidatorServiceInterface $deadlineValidatorService)
  65.     {
  66.         // set params
  67.         $this->returnShipmentRepository $returnShipmentRepository;
  68.         $this->returnShipmentLineItemRepository $returnShipmentLineItemRepository;
  69.         $this->systemConfigService $systemConfigService;
  70.         $this->orderValidatorService $orderValidatorService;
  71.         $this->deadlineValidatorService $deadlineValidatorService;
  72.     }
  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     public static function getSubscribedEvents(): array
  77.     {
  78.         return [
  79.             OrderEvents::ORDER_LOADED_EVENT => 'onOrdersLoaded'
  80.         ];
  81.     }
  82.     /**
  83.      * ...
  84.      *
  85.      * @param EntityLoadedEvent $event
  86.      */
  87.     public function onOrdersLoaded(EntityLoadedEvent $event): void
  88.     {
  89.         /** @var OrderEntity $orderEntity */
  90.         foreach ($event->getEntities() as $orderEntity) {
  91.             // try to find a return shipment for this order
  92.             $criteria = (new Criteria())
  93.                 ->addFilter(new EqualsFilter('orderId'$orderEntity->getId()))
  94.                 ->setLimit(1);
  95.             /** @var ReturnShipmentEntity $returnShipment */
  96.             $returnShipment $this->returnShipmentRepository->search(
  97.                 $criteria,
  98.                 $event->getContext()
  99.             )->first();
  100.             // create a struct to extend the order
  101.             $struct = new ReturnShipmentStruct();
  102.             $struct->setStatus(($returnShipment instanceof ReturnShipmentEntity));
  103.             $struct->setId(($returnShipment instanceof ReturnShipmentEntity) ? $returnShipment->getId() : null);
  104.             $struct->setIsValid($this->isValid(
  105.                 $orderEntity,
  106.                 $returnShipment,
  107.                 $event->getContext(),
  108.                 $this->systemConfigService->get('DvsnReturnShipment.config'$orderEntity->getSalesChannelId())
  109.             ));
  110.             // add to order
  111.             $orderEntity->addExtension(
  112.                 'dvsn_return_shipment',
  113.                 $struct
  114.             );
  115.         }
  116.     }
  117.     /**
  118.      * ...
  119.      *
  120.      * @param OrderEntity $orderEntity
  121.      * @param ReturnShipmentEntity $returnShipment
  122.      * @param Context $context
  123.      * @param array $config
  124.      *
  125.      * @return bool
  126.      */
  127.     private function isValid(OrderEntity $orderEntity, ?ReturnShipmentEntity $returnShipmentContext $context, array $config): bool
  128.     {
  129.         if ($returnShipment instanceof ReturnShipmentEntity) {
  130.             return
  131.                 $this->orderValidatorService->isValid($orderEntity) &&
  132.                 $this->deadlineValidatorService->isValid($orderEntity->getId(), [
  133.                     'status' => (bool) $this->systemConfigService->get('DvsnReturnShipment.config.deadlineStatus'$orderEntity->getSalesChannelId()),
  134.                     'type' => (string) $this->systemConfigService->get('DvsnReturnShipment.config.deadlineType'$orderEntity->getSalesChannelId()),
  135.                     'daysDate' => (string) trim($this->systemConfigService->get('DvsnReturnShipment.config.deadlineDaysDate'$orderEntity->getSalesChannelId())),
  136.                     'orderDeliveryStatus' => (string) $this->systemConfigService->get('DvsnReturnShipment.config.deadlineOrderDeliveryStatus'$orderEntity->getSalesChannelId())
  137.                 ], $context);
  138.         }
  139.         return $this->orderValidatorService->isValid($orderEntity);
  140.     }
  141. }