custom/plugins/CogiLicenseManager/src/Subscriber/EventSubscriber.php line 189

Open in your IDE?
  1. <?php
  2. namespace Cogi\CogiLicenseManager\Subscriber;
  3. use Cogi\CogiLicenseManager\Service\LicenseManagerService;
  4. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
  7. use Shopware\Core\Checkout\Cart\Exception\OrderTransactionNotFoundException;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  17. use Shopware\Core\System\SystemConfig\SystemConfigService;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class EventSubscriber implements EventSubscriberInterface {
  20.     /**
  21.      * @var SalesChannelContext
  22.      */
  23.     protected $salesChannelContext;
  24.     /**
  25.      * @var EntityRepository $orderRepository
  26.      */
  27.     private $orderRepository;
  28.     /**
  29.      * @var EntityRepository $orderTransactionRepository
  30.      */
  31.     private $orderTransactionRepository;
  32.     /**
  33.      * @var EntityRepository $orderDeliveryRepository
  34.      */
  35.     private $orderDeliveryRepository;
  36.     /**
  37.      * @var EntityRepository
  38.      */
  39.     protected $licenseProductRepository;
  40.     /**
  41.      * @var EntityRepository
  42.      */
  43.     protected $licensePoolRepository;
  44.     /**
  45.      * @var EntityRepository
  46.      */
  47.     protected $licenseRepository;
  48.     /**
  49.      * @var EntityRepository
  50.      */
  51.     protected $licenseCustomerRepository;
  52.     /**
  53.      * @var EntityRepository
  54.      */
  55.     protected $productRepository;
  56.     /**
  57.      * @var LicenseManagerService
  58.      */
  59.     protected $licenseService;
  60.     /**
  61.      * @var array
  62.      */
  63.     protected $config;
  64.     /**
  65.      * @param EntityRepository $orderRepository
  66.      * @param EntityRepository $orderTransactionRepository
  67.      * @param EntityRepository $orderDeliveryRepository
  68.      * @param EntityRepository $licenseProductRepository
  69.      * @param EntityRepository $licensePoolRepository
  70.      * @param EntityRepository $licenseRepository
  71.      * @param EntityRepository $licenseCustomerRepository
  72.      * @param EntityRepository $productRepository
  73.      * @param LicenseManagerService $licenseService
  74.      * @param SystemConfigService $systemConfigService
  75.      */
  76.     public function __construct(EntityRepository $orderRepository,
  77.                                 EntityRepository $orderTransactionRepository,
  78.                                 EntityRepository $orderDeliveryRepository,
  79.                                 EntityRepository $licenseProductRepository,
  80.                                 EntityRepository $licensePoolRepository,
  81.                                 EntityRepository $licenseRepository,
  82.                                 EntityRepository $licenseCustomerRepository,
  83.                                 EntityRepository $productRepository,
  84.                                 LicenseManagerService $licenseService,
  85.                                 SystemConfigService $systemConfigService) {
  86.         $this->orderRepository $orderRepository;
  87.         $this->orderTransactionRepository $orderTransactionRepository;
  88.         $this->orderDeliveryRepository $orderDeliveryRepository;
  89.         $this->licenseProductRepository $licenseProductRepository;
  90.         $this->licensePoolRepository $licensePoolRepository;
  91.         $this->licenseRepository $licenseRepository;
  92.         $this->licenseCustomerRepository $licenseCustomerRepository;
  93.         $this->productRepository $productRepository;
  94.         $this->licenseService $licenseService;
  95.         $this->config $systemConfigService->getDomain('CogiLicenseManager.config');
  96.     }
  97.     public static function getSubscribedEvents(): array {
  98.         return [
  99.             SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved',
  100.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  101.             AfterLineItemAddedEvent::class => 'onAfterLineItemAdded',
  102.             'state_machine.order_transaction.state_changed' => 'onOrderTransactionStateChanged'
  103.         ];
  104.     }
  105.     /**
  106.      * @param CheckoutOrderPlacedEvent $event
  107.      */
  108.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event) {
  109.         $order $event->getOrder();
  110.         if ($order instanceof OrderEntity) {
  111.             /** @var OrderLineItemEntity $lineItem */
  112.             foreach ($order->getLineItems() as $lineItem){
  113.                 if(isset($lineItem->getPayload()['isLicense'])) {
  114.                     if ($lineItem->getPayload()['isLicense'] !== null) {
  115.                         if ($lineItem->getPayload()['isLicense']) {
  116.                             $this->licenseService->processLicenseOrder($order$lineItem$event->getContext());
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.     }
  123.     /**
  124.      * Checkt bei einer Ă„nderung des Zahlungsstatus ob ein Lizenz produkt dabei ist
  125.      * Bei einem Lizenz Produkt wird hier der Proccess der zusendung eines Keys gestartet
  126.      *
  127.      * @param StateMachineStateChangeEvent $event
  128.      * @throws InconsistentCriteriaIdsException
  129.      * @throws OrderTransactionNotFoundException
  130.      */
  131.     public function onOrderTransactionStateChanged(StateMachineStateChangeEvent $event) {
  132.         $orderTransactionId $event->getTransition()->getEntityId();
  133.         /** @var OrderTransactionEntity|null $orderTransaction */
  134.         $orderTransaction $this->orderTransactionRepository->search(new Criteria([$orderTransactionId]), $event->getContext())->first();
  135.         if ($orderTransaction === null) {
  136.             throw new OrderTransactionNotFoundException($orderTransactionId);
  137.         }
  138.         $orderId $orderTransaction->getOrderId();
  139.         $criteria = new Criteria([$orderId]);
  140.         $criteria->addAssociation('lineItems');
  141.         $criteria->addAssociation('orderCustomer');
  142.         /** @var OrderEntity|null $order */
  143.         $order $this->orderRepository->search($criteria$event->getContext())->get($orderId);
  144.         if ($order instanceof OrderEntity) {
  145.             $this->licenseService->checkState($order$event->getContext());
  146.         }
  147.     }
  148.     /**
  149.      * @param AfterLineItemAddedEvent $event
  150.      */
  151.     public function onAfterLineItemAdded(AfterLineItemAddedEvent $event) {
  152.     }
  153.     /**
  154.      * @param SalesChannelContextResolvedEvent $event
  155.      */
  156.     public function onSalesChannelContextResolved(SalesChannelContextResolvedEvent $event): void {
  157.         $this->salesChannelContext $event->getSalesChannelContext();
  158.     }
  159. }