custom/static-plugins/LnbCustomFields/src/Subscriber/SubscriptionStateSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Lnb\Shopware6\LnbCustomFields\Subscriber;
  4. use Lnb\Shopware6\LnbSubscription\Entity\SubscriptionOrder\SubscriptionOrderEntity;
  5. use Lnb\Shopware6\LnbSubscription\Event\SubscriptionBeforeRenewEvent;
  6. use Lnb\Shopware6\LnbSubscription\Event\SubscriptionCancelledEvent;
  7. use Lnb\Shopware6\LnbSubscription\Event\SubscriptionCreatedEvent;
  8. use Lnb\Shopware6\LnbSubscription\Installation\Installer\StateMachineInstaller;
  9. use Shopware\Core\Checkout\Order\OrderEntity;
  10. use Shopware\Core\Checkout\Order\OrderEvents;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class SubscriptionStateSubscriber implements EventSubscriberInterface
  21. {
  22.     protected ContainerInterface $container;
  23.     public function __construct(
  24.         ContainerInterface $container
  25.     ) {
  26.         $this->container $container;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             SubscriptionCreatedEvent::class => ['onSubscriptionCreated'],
  32.             SubscriptionCancelledEvent::class => ['onSubscriptionCancelled'],
  33.             OrderEvents::ORDER_WRITTEN_EVENT => [['onOrderCreated'0]],
  34.         ];
  35.     }
  36.     public function onSubscriptionCreated(SubscriptionCreatedEvent $event)
  37.     {
  38.         $context $event->getContext();
  39.         $subscriptionId $event->getSubscriptionId();
  40.         $subscription $this->getSubscription($subscriptionId$context);
  41.         $this->updateCustomerActiveSubscriptionsCount($subscription->getCustomerId(), $context);
  42.     }
  43.     public function onSubscriptionCancelled(SubscriptionCancelledEvent $event)
  44.     {
  45.         $context $event->getContext();
  46.         $subscriptionId $event->getLnbSubscriptionOrder()->getId();
  47.         $subscription $this->getSubscription($subscriptionId$context);
  48.         $this->updateCustomerActiveSubscriptionsCount($subscription->getCustomerId(), $context);
  49.         $this->updateAllSubscriptionChildren($subscriptionId$context);
  50.     }
  51.     public function onOrderCreated(EntityWrittenEvent $event)
  52.     {
  53.         $context $event->getContext();
  54.         foreach ($event->getWriteResults() as $writeResult) {
  55.             // return if the operation is not insert
  56.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_INSERT) {
  57.                 return;
  58.             }
  59.             $orderId $writeResult->getPrimaryKey();
  60.             $order $this->getOrder($context$orderId);
  61.             foreach ($order->getLineItems() as $lineItem) {
  62.                 $product $lineItem->getProduct();
  63.                 if ($product) { // it can be discount...
  64.                     $payload $lineItem->getPayload();
  65.                     if (!empty($payload['LnbSubscription']))
  66.                     {
  67.                         $this->setOrderCustomField($order->getId(), 'lnb_custom_fields_parent_subscription_canceled'false$context);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     private function getSubscription(string $subscriptionIdContext $context): SubscriptionOrderEntity
  74.     {
  75.         $criteria = new Criteria([$subscriptionId]);
  76.         $subscriptionOrderRepository $this->container->get('lnb_subscription_order.repository');
  77.         return $subscriptionOrderRepository->search($criteria$context)->first();
  78.     }
  79.     private function updateCustomerActiveSubscriptionsCount(string $customerIdContext $context): void
  80.     {
  81.         $criteria = new Criteria();
  82.         $criteria
  83.             ->addFilter(new EqualsFilter('customerId'$customerId))
  84.             ->addFilter(new NotFilter('AND', [new EqualsFilter('stateId'StateMachineInstaller::STATE_CANCELLED_ID)]));
  85.         $subscriptionOrderRepository $this->container->get('lnb_subscription_order.repository');
  86.         $activeSubscriptionCount $subscriptionOrderRepository->search($criteria$context)->count();
  87.         $customerRepository $this->container->get('customer.repository');
  88.         $customerRepository->update([
  89.             [
  90.                 'id' => $customerId,
  91.                 'customFields' => [
  92.                     'lnb_custom_fields_active_subscription_count' => $activeSubscriptionCount
  93.                 ],
  94.             ]
  95.         ], $context);
  96.     }
  97.     private function updateAllSubscriptionChildren(string $subscriptionIdContext $context): void
  98.     {
  99.         $criteria = new Criteria();
  100.         $criteria
  101.             ->addAssociation('lineItems')
  102.             ->addFilter(new EqualsFilter('lineItems.customFields.LnbSubscription.lnbSubscriptionOrderId'$subscriptionId));
  103.         $orderRepository $this->container->get('order.repository');
  104.         $orders $orderRepository->search($criteria$context)->getElements();
  105.         foreach ($orders as $order) {
  106.             $this->setOrderCustomField($order->getId(), 'lnb_custom_fields_parent_subscription_canceled'true$context);
  107.         }
  108.     }
  109.     private function setOrderCustomField(string $orderIdstring $fieldNamemixed $fieldValue ,Context $context)
  110.     {
  111.         $orderRepository $this->container->get('order.repository');
  112.         $orderRepository->update([
  113.             [
  114.                 'id' => $orderId,
  115.                 'customFields' => [
  116.                     $fieldName => $fieldValue
  117.                 ],
  118.             ]
  119.         ], $context);
  120.     }
  121.     private function getOrder(Context $contextstring $orderId): OrderEntity
  122.     {
  123.         /** @var EntityRepository $orderRepository */
  124.         $orderRepository $this->container->get('order.repository');
  125.         $criteria = new Criteria([$orderId]);
  126.         $criteria->addAssociation('lineItems');
  127.         $criteria->addAssociation('lineItems.product');
  128.         $criteria->addAssociation('orderCustomer.customer');
  129.         return $orderRepository->search($criteria$context)->first();
  130.     }
  131. }