<?php
namespace Cogi\CogiLicenseManager\Subscriber;
use Cogi\CogiLicenseManager\Service\LicenseManagerService;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Exception\OrderTransactionNotFoundException;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EventSubscriber implements EventSubscriberInterface {
/**
* @var SalesChannelContext
*/
protected $salesChannelContext;
/**
* @var EntityRepository $orderRepository
*/
private $orderRepository;
/**
* @var EntityRepository $orderTransactionRepository
*/
private $orderTransactionRepository;
/**
* @var EntityRepository $orderDeliveryRepository
*/
private $orderDeliveryRepository;
/**
* @var EntityRepository
*/
protected $licenseProductRepository;
/**
* @var EntityRepository
*/
protected $licensePoolRepository;
/**
* @var EntityRepository
*/
protected $licenseRepository;
/**
* @var EntityRepository
*/
protected $licenseCustomerRepository;
/**
* @var EntityRepository
*/
protected $productRepository;
/**
* @var LicenseManagerService
*/
protected $licenseService;
/**
* @var array
*/
protected $config;
/**
* @param EntityRepository $orderRepository
* @param EntityRepository $orderTransactionRepository
* @param EntityRepository $orderDeliveryRepository
* @param EntityRepository $licenseProductRepository
* @param EntityRepository $licensePoolRepository
* @param EntityRepository $licenseRepository
* @param EntityRepository $licenseCustomerRepository
* @param EntityRepository $productRepository
* @param LicenseManagerService $licenseService
* @param SystemConfigService $systemConfigService
*/
public function __construct(EntityRepository $orderRepository,
EntityRepository $orderTransactionRepository,
EntityRepository $orderDeliveryRepository,
EntityRepository $licenseProductRepository,
EntityRepository $licensePoolRepository,
EntityRepository $licenseRepository,
EntityRepository $licenseCustomerRepository,
EntityRepository $productRepository,
LicenseManagerService $licenseService,
SystemConfigService $systemConfigService) {
$this->orderRepository = $orderRepository;
$this->orderTransactionRepository = $orderTransactionRepository;
$this->orderDeliveryRepository = $orderDeliveryRepository;
$this->licenseProductRepository = $licenseProductRepository;
$this->licensePoolRepository = $licensePoolRepository;
$this->licenseRepository = $licenseRepository;
$this->licenseCustomerRepository = $licenseCustomerRepository;
$this->productRepository = $productRepository;
$this->licenseService = $licenseService;
$this->config = $systemConfigService->getDomain('CogiLicenseManager.config');
}
public static function getSubscribedEvents(): array {
return [
SalesChannelContextResolvedEvent::class => 'onSalesChannelContextResolved',
CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
AfterLineItemAddedEvent::class => 'onAfterLineItemAdded',
'state_machine.order_transaction.state_changed' => 'onOrderTransactionStateChanged'
];
}
/**
* @param CheckoutOrderPlacedEvent $event
*/
public function onOrderPlaced(CheckoutOrderPlacedEvent $event) {
$order = $event->getOrder();
if ($order instanceof OrderEntity) {
/** @var OrderLineItemEntity $lineItem */
foreach ($order->getLineItems() as $lineItem){
if(isset($lineItem->getPayload()['isLicense'])) {
if ($lineItem->getPayload()['isLicense'] !== null) {
if ($lineItem->getPayload()['isLicense']) {
$this->licenseService->processLicenseOrder($order, $lineItem, $event->getContext());
}
}
}
}
}
}
/**
* Checkt bei einer Ă„nderung des Zahlungsstatus ob ein Lizenz produkt dabei ist
* Bei einem Lizenz Produkt wird hier der Proccess der zusendung eines Keys gestartet
*
* @param StateMachineStateChangeEvent $event
* @throws InconsistentCriteriaIdsException
* @throws OrderTransactionNotFoundException
*/
public function onOrderTransactionStateChanged(StateMachineStateChangeEvent $event) {
$orderTransactionId = $event->getTransition()->getEntityId();
/** @var OrderTransactionEntity|null $orderTransaction */
$orderTransaction = $this->orderTransactionRepository->search(new Criteria([$orderTransactionId]), $event->getContext())->first();
if ($orderTransaction === null) {
throw new OrderTransactionNotFoundException($orderTransactionId);
}
$orderId = $orderTransaction->getOrderId();
$criteria = new Criteria([$orderId]);
$criteria->addAssociation('lineItems');
$criteria->addAssociation('orderCustomer');
/** @var OrderEntity|null $order */
$order = $this->orderRepository->search($criteria, $event->getContext())->get($orderId);
if ($order instanceof OrderEntity) {
$this->licenseService->checkState($order, $event->getContext());
}
}
/**
* @param AfterLineItemAddedEvent $event
*/
public function onAfterLineItemAdded(AfterLineItemAddedEvent $event) {
}
/**
* @param SalesChannelContextResolvedEvent $event
*/
public function onSalesChannelContextResolved(SalesChannelContextResolvedEvent $event): void {
$this->salesChannelContext = $event->getSalesChannelContext();
}
}