custom/static-plugins/GlobusSW6/src/Subscriber/ArticleDocuments/HandleArticleDocuments.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\ArticleDocuments;
  3. use GlobusSW6\Service\ArticleDocumentsService;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use GlobusSW6\Service\IaneoBusinessExtension;
  8. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  9. use Shopware\Core\Content\Product\ProductCollection;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class HandleArticleDocuments implements EventSubscriberInterface
  12. {
  13.     private ArticleDocumentsService $articleDocumentsService;
  14.     private LoggerInterface $logger;
  15.     private IaneoBusinessExtension $IaneoBusinessExtension;
  16.     private RequestStack $requestStack;
  17.     public const WHITELIST_ROUTES = [
  18.         'frontend.detail.page',
  19.         'store-api.product.detail',
  20.     ];
  21.     public function __construct(
  22.         ArticleDocumentsService $articleDocumentsService,
  23.         LoggerInterface $logger,
  24.         IaneoBusinessExtension $IaneoBusinessExtension,
  25.         RequestStack $requestStack
  26.     ) {
  27.         $this->articleDocumentsService $articleDocumentsService;
  28.         $this->logger $logger;
  29.         $this->IaneoBusinessExtension $IaneoBusinessExtension;
  30.         $this->requestStack $requestStack;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35. //            ProductPageLoadedEvent::class => 'onProductPageLoadedEvent'
  36.             'product.search.result.loaded' => 'onProductLoaded'
  37.         ];
  38.     }
  39.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  40.     {
  41.         // do not fire event in backend context
  42.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  43.             if ($event->getContext()->getSource()->isAdmin()) {
  44.                 return;
  45.             }
  46.         }
  47.         if(!$this->shouldAttachDocuments()) {
  48.             return;
  49.         }
  50.         /** @var ProductCollection $products */
  51.         $products $event->getResult()->getEntities();
  52.         if($products === null){
  53.             return;
  54.         }
  55.         foreach ($products as $product) {
  56.             try {
  57.                 $articleDocuments $this->articleDocumentsService->getProductDocumentRelations($product->getId(), $event->getContext());
  58.                 if($articleDocuments->has('documents')) {
  59.                     $this->IaneoBusinessExtension->addSingleAttributeToProduct($product$articleDocuments->get('documents'), 'articleDocuments');
  60.                 }
  61.             } catch(\Exception $exception) {
  62.                 $this->logger->error(__CLASS__ ":" __FUNCTION__ ":" __LINE__ ":\$exception->getMessage(): " $exception->getMessage());
  63.             }
  64.         }
  65.     }
  66.     private function shouldAttachDocuments(): bool
  67.     {
  68.         $currentRequest $this->requestStack->getCurrentRequest();
  69.         if ($currentRequest) {
  70.             $requestRoute $currentRequest->attributes?->get('_route') ?? '';
  71.             foreach (self::WHITELIST_ROUTES as $WHITELIST_ROUTE) {
  72.                 if (str_contains($requestRoute$WHITELIST_ROUTE)) {
  73.                     return true;
  74.                 }
  75.             }
  76.         }
  77.         return false;
  78.     }
  79. }