<?php
/**
* digitvision
*
* @category digitvision
* @package Shopware\Plugins\DvsnReturnShipment
* @copyright (c) 2020 digitvision
*/
namespace Dvsn\ReturnShipment\Subscriber\Core\Checkout\Order;
use Dvsn\ReturnShipment\Core\Checkout\Order\Aggregate\ReturnShipment\ReturnShipmentStruct;
use Dvsn\ReturnShipment\Core\Checkout\ReturnShipment\ReturnShipmentEntity;
use Dvsn\ReturnShipment\Service\DeadlineValidatorServiceInterface;
use Dvsn\ReturnShipment\Service\Order\ValidatorServiceInterface;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
class OrderEventsSubscriber implements EventSubscriberInterface
{
/**
* ...
*
* @var EntityRepository
*/
protected $returnShipmentRepository;
/**
* ...
*
* @var EntityRepository
*/
protected $returnShipmentLineItemRepository;
/**
* ...
*
* @var SystemConfigService
*/
protected $systemConfigService;
/**
* ...
*
* @var ValidatorServiceInterface
*/
protected $orderValidatorService;
/**
* ...
*
* @var DeadlineValidatorServiceInterface
*/
protected $deadlineValidatorService;
/**
* ...
*
* @param EntityRepository $returnShipmentRepository
* @param EntityRepository $returnShipmentLineItemRepository
* @param SystemConfigService $systemConfigService
* @param ValidatorServiceInterface $orderValidatorService
* @param DeadlineValidatorServiceInterface $deadlineValidatorService
*/
public function __construct(EntityRepository $returnShipmentRepository, EntityRepository $returnShipmentLineItemRepository, SystemConfigService $systemConfigService, ValidatorServiceInterface $orderValidatorService, DeadlineValidatorServiceInterface $deadlineValidatorService)
{
// set params
$this->returnShipmentRepository = $returnShipmentRepository;
$this->returnShipmentLineItemRepository = $returnShipmentLineItemRepository;
$this->systemConfigService = $systemConfigService;
$this->orderValidatorService = $orderValidatorService;
$this->deadlineValidatorService = $deadlineValidatorService;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return [
OrderEvents::ORDER_LOADED_EVENT => 'onOrdersLoaded'
];
}
/**
* ...
*
* @param EntityLoadedEvent $event
*/
public function onOrdersLoaded(EntityLoadedEvent $event): void
{
/** @var OrderEntity $orderEntity */
foreach ($event->getEntities() as $orderEntity) {
// try to find a return shipment for this order
$criteria = (new Criteria())
->addFilter(new EqualsFilter('orderId', $orderEntity->getId()))
->setLimit(1);
/** @var ReturnShipmentEntity $returnShipment */
$returnShipment = $this->returnShipmentRepository->search(
$criteria,
$event->getContext()
)->first();
// create a struct to extend the order
$struct = new ReturnShipmentStruct();
$struct->setStatus(($returnShipment instanceof ReturnShipmentEntity));
$struct->setId(($returnShipment instanceof ReturnShipmentEntity) ? $returnShipment->getId() : null);
$struct->setIsValid($this->isValid(
$orderEntity,
$returnShipment,
$event->getContext(),
$this->systemConfigService->get('DvsnReturnShipment.config', $orderEntity->getSalesChannelId())
));
// add to order
$orderEntity->addExtension(
'dvsn_return_shipment',
$struct
);
}
}
/**
* ...
*
* @param OrderEntity $orderEntity
* @param ReturnShipmentEntity $returnShipment
* @param Context $context
* @param array $config
*
* @return bool
*/
private function isValid(OrderEntity $orderEntity, ?ReturnShipmentEntity $returnShipment, Context $context, array $config): bool
{
if ($returnShipment instanceof ReturnShipmentEntity) {
return
$this->orderValidatorService->isValid($orderEntity) &&
$this->deadlineValidatorService->isValid($orderEntity->getId(), [
'status' => (bool) $this->systemConfigService->get('DvsnReturnShipment.config.deadlineStatus', $orderEntity->getSalesChannelId()),
'type' => (string) $this->systemConfigService->get('DvsnReturnShipment.config.deadlineType', $orderEntity->getSalesChannelId()),
'daysDate' => (string) trim($this->systemConfigService->get('DvsnReturnShipment.config.deadlineDaysDate', $orderEntity->getSalesChannelId())),
'orderDeliveryStatus' => (string) $this->systemConfigService->get('DvsnReturnShipment.config.deadlineOrderDeliveryStatus', $orderEntity->getSalesChannelId())
], $context);
}
return $this->orderValidatorService->isValid($orderEntity);
}
}