custom/plugins/MadMyWerb/src/Subscriber/ProductSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Mad\MyWerb\Subscriber;
  4. use League\Flysystem\FilesystemInterface;
  5. use Mad\MyWerb\Message\MyWerbOrderPlaced;
  6. use Mad\MyWerb\Service\MyWerbChecker;
  7. use Mad\MyWerb\Service\MyWerbFileLocator;
  8. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  9. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  10. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  11. use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
  12. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\CountResult;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\Struct\ArrayEntity;
  18. use Shopware\Core\Framework\Struct\ArrayStruct;
  19. use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
  20. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
  21. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  22. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  23. use Shopware\Core\System\SystemConfig\SystemConfigService;
  24. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
  25. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  26. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  27. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  28. use Shopware\Storefront\Page\Product\ProductPageLoader;
  29. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  30. use Shopware\Core\Content\Product\ProductEvents;
  31. use Symfony\Component\HttpFoundation\ParameterBag;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\HttpFoundation\RequestStack;
  34. use Symfony\Component\HttpFoundation\Response;
  35. use Symfony\Component\Messenger\MessageBusInterface;
  36. use Twig\Environment;
  37. use UsingRefs\Product;
  38. use Shopware\Core\Framework\Context;
  39. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  40. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  41. class ProductSubscriber implements EventSubscriberInterface
  42. {
  43.     public function __construct(
  44.         private MyWerbChecker $myWerbChecker,
  45.         private RequestStack $requestStack,
  46.         private FilesystemInterface $owncloud,
  47.         private FilesystemInterface $filesystemTemp,
  48.         private EntityRepositoryInterface $mailTemplateRepository,
  49.         private EntityRepositoryInterface $salesChannelRepository,
  50.         private AbstractSalesChannelContextFactory $salesChannelContextFactory,
  51.         private Environment $twig,
  52.         private SystemConfigService $systemConfigService,
  53.         private MessageBusInterface $messageBus,
  54.         private MyWerbFileLocator $myWerbFileLocator
  55.     ) {
  56.     }
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  60.         return [
  61.             ProductPageLoadedEvent::class => 'addMyWerbCheck',
  62.             ProductEvents::PRODUCT_LOADED_EVENT => "onProductLoaded",
  63.             BeforeLineItemAddedEvent::class => 'onLineItemAdded',
  64.             //CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPage',
  65.             #CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPage',
  66.             #CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinish',
  67.             CheckoutOrderPlacedEvent::class => [
  68.                 ['handleMyWerbOrder'PHP_INT_MIN 100]
  69.             ],
  70.         ];
  71.     }
  72.     public function handleMyWerbOrder(CheckoutOrderPlacedEvent $event): void
  73.     {
  74.         $order $event->getOrder();
  75.         $salesChannelId $event->getSalesChannelId();
  76.         $context $event->getContext();
  77.         $message MyWerbOrderPlaced::create($order$salesChannelId$context);
  78.         foreach ($order->getLineItems() as $orderLineItem) {
  79.             $payload $orderLineItem->getPayload();
  80.             if (\is_array($payload) && isset($payload['mywerb'])) {
  81.                 $this->messageBus->dispatch($message);
  82.                 return;
  83.             }
  84.         }
  85.     }
  86.     public function onLineItemAdded(BeforeLineItemAddedEvent $event)
  87.     {
  88.         $mywerbId 0;
  89.         $request $this->requestStack->getCurrentRequest()->get('lineItems');
  90.         if ($request) {
  91.             foreach ($request as $key => $item) {
  92.                 if (isset($item['mywerb'])) {
  93.                     $mywerbId str_replace(' ''_'$item['mywerb']);
  94.                 }
  95.             }
  96.             if ($mywerbId) {
  97.                 $mylineitems $event->getCart()->getLineItems();
  98.                 $lineitems $event->getLineItem();
  99.                 $lineitems->setPayloadValue("mywerb"$mywerbId);
  100.             }
  101.         }
  102.     }
  103.     public function onProductLoaded(EntityLoadedEvent $event)
  104.     {
  105.         $entities $event->getEntities();
  106.         /** @var SalesChannelProductEntity $entity */
  107.         foreach ($entities as $entity) {
  108.             $number $entity->getProductNumber();
  109.             $name $entity->getName();
  110.             if ($this->myWerbChecker->isMyWerb($entity)) {
  111.                 $data = new ArrayStruct(['myWerb' => true]);
  112.                 $entity->addExtension('myWerb'$data);
  113.             }
  114.         }
  115.     }
  116.     public function addMyWerbCheck(ProductPageLoadedEvent $event)
  117.     {
  118.         $number $event->getPage()->getProduct()->getProductNumber();
  119.         $name $event->getPage()->getProduct()->getName();
  120.         if ($this->myWerbChecker->isMyWerb($event->getPage()->getProduct())) {
  121.             $filePath $this->myWerbFileLocator->getProductDownloadPath($number'pdf'$event->getSalesChannelContext()->getSalesChannelId());
  122.             if ($this->owncloud->has($filePath)) {
  123.                 $download = new ArrayEntity(['Download' => '1']);
  124.                 $event->getPage()->addExtension('Download'$download);
  125.             }
  126.             $data = new ArrayEntity(['myWerb' => '1']);
  127.             $event->getPage()->addExtension('myWerb'$data);
  128.         }
  129.     }
  130. }