custom/plugins/DreiscSeoPro/src/Subscriber/Installment/RobotsTag/RobotsTagSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Subscriber\Installment\RobotsTag;
  3. use DreiscSeoPro\Core\CustomSetting\CustomSettingLoader;
  4. use DreiscSeoPro\Core\RobotsTag\RobotsTagFetcher;
  5. use DreiscSeoPro\Core\RobotsTag\RobotsTagFetcherStruct;
  6. use Shopware\Core\Content\Category\CategoryDefinition;
  7. use Shopware\Core\Content\Product\ProductDefinition;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Storefront\Page\Navigation\NavigationPage;
  10. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  11. use Shopware\Storefront\Page\Page;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Shopware\Storefront\Page\Product\ProductPage;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class RobotsTagSubscriber implements EventSubscriberInterface
  17. {
  18.     public const DREISC_SEO_INSTALLMENT_ROBOTS_TAG_DATA 'dreiscSeoInstallmentRobotsTagData';
  19.     /**
  20.      * @var RobotsTagFetcher
  21.      */
  22.     private $robotsTagFetcher;
  23.     /**
  24.      * @var CustomSettingLoader
  25.      */
  26.     private $customSettingLoader;
  27.     /**
  28.      * @param RobotsTagFetcher $robotsTagFetcher
  29.      * @param CustomSettingLoader $customSettingLoader
  30.      */
  31.     public function __construct(RobotsTagFetcher $robotsTagFetcherCustomSettingLoader $customSettingLoader)
  32.     {
  33.         $this->robotsTagFetcher $robotsTagFetcher;
  34.         $this->customSettingLoader $customSettingLoader;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             ProductPageLoadedEvent::class => 'addInstallment',
  40.             NavigationPageLoadedEvent::class => 'addInstallment',
  41.         ];
  42.     }
  43.     /**
  44.      * @param PageLoadedEvent $event
  45.      * @throws InconsistentCriteriaIdsException
  46.      */
  47.     public function addInstallment(PageLoadedEvent $event)
  48.     {
  49.         /** @var Page|ProductPage|NavigationPage $page */
  50.         $page $event->getPage();
  51.         $salesChannelEntity $event->getSalesChannelContext()->getSalesChannel();
  52.         $languageId $event->getContext()->getLanguageId();
  53.         $robotsTag null;
  54.         /** Load the custom settings */
  55.         $customSettings $this->customSettingLoader->load($salesChannelEntity->getId(), true);
  56.         /** Fetch the params of the request */
  57.         $requestParams null;
  58.         if (null !== $event->getRequest()->getQueryString()) {
  59.             parse_str($event->getRequest()->getQueryString(), $requestParams);
  60.         }
  61.         if ($page instanceof ProductPage) {
  62.             $robotsTag $this->robotsTagFetcher->fetch(
  63.                 new RobotsTagFetcherStruct(
  64.                     $customSettings,
  65.                     ProductDefinition::ENTITY_NAME,
  66.                     $page->getProduct()->getId(),
  67.                     $languageId,
  68.                     $requestParams
  69.                 )
  70.             );
  71.         } elseif ($page instanceof NavigationPage) {
  72.             /** Try to fetch the active category */
  73.             $categoryId $event->getRequest()->attributes->get('navigationId');
  74.             /** Fetch the home category id */
  75.             if(
  76.                 empty($categoryId) &&
  77.                 'frontend.home.page' === $event->getRequest()->attributes->get('_route') &&
  78.                 null !== $page->getHeader() &&
  79.                 null !== $page->getHeader()->getNavigation()
  80.             ) {
  81.                 $activeCategory $page->getHeader()->getNavigation()->getActive();
  82.                 if (null !== $activeCategory) {
  83.                     $categoryId $activeCategory->getId();
  84.                 }
  85.             }
  86.             /** Abort, if category is null */
  87.             if (null === $categoryId) {
  88.                 return;
  89.             }
  90.             $robotsTag $this->robotsTagFetcher->fetch(
  91.                 new RobotsTagFetcherStruct(
  92.                     $customSettings,
  93.                     CategoryDefinition::ENTITY_NAME,
  94.                     $categoryId,
  95.                     $languageId,
  96.                     $requestParams
  97.                 )
  98.             );
  99.         }
  100.         if (null !== $robotsTag) {
  101.             $page->addExtension(
  102.                 self::DREISC_SEO_INSTALLMENT_ROBOTS_TAG_DATA,
  103.                 new RobotsTagStruct($robotsTag)
  104.             );
  105.         }
  106.     }
  107. }