custom/plugins/DreiscSeoPro/src/Subscriber/Installment/RichSnippet/RichSnippetSubscriber.php line 89

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Subscriber\Installment\RichSnippet;
  3. use DreiscSeoPro\Core\Breadcrumb\PlainBreadcrumbCalculator;
  4. use DreiscSeoPro\Core\CustomSetting\CustomSettingLoader;
  5. use DreiscSeoPro\Core\RichSnippet\Breadcrumb\BreadcrumbRichSnippetLdBuilderInterface;
  6. use DreiscSeoPro\Core\RichSnippet\Breadcrumb\BreadcrumbRichSnippetLdBuilderStruct;
  7. use DreiscSeoPro\Core\RichSnippet\LocalBusiness\LocalBusinessRichSnippetLdBuilderInterface;
  8. use DreiscSeoPro\Core\RichSnippet\LocalBusiness\LocalBusinessRichSnippetLdBuilderStruct;
  9. use DreiscSeoPro\Core\RichSnippet\Logo\LogoRichSnippetLdBuilderInterface;
  10. use DreiscSeoPro\Core\RichSnippet\Logo\LogoRichSnippetLdBuilderStruct;
  11. use DreiscSeoPro\Core\RichSnippet\Product\ProductRichSnippetLdBuilderInterface;
  12. use DreiscSeoPro\Core\RichSnippet\Product\ProductRichSnippetLdBuilderStruct;
  13. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  15. use Shopware\Core\SalesChannelRequest;
  16. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  17. use Shopware\Storefront\Page\Page;
  18. use Shopware\Storefront\Page\PageLoadedEvent;
  19. use Shopware\Storefront\Page\Product\ProductPage;
  20. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class RichSnippetSubscriber implements EventSubscriberInterface
  23. {
  24.     public const DREISC_SEO_INSTALLMENT_RICH_SNIPPET_DATA 'dreiscSeoInstallmentRichSnippetData';
  25.     /**
  26.      * @var ProductRichSnippetLdBuilderInterface
  27.      */
  28.     private $productRichSnippetLdBuilder;
  29.     /**
  30.      * @var BreadcrumbRichSnippetLdBuilderInterface
  31.      */
  32.     private $breadcrumbRichSnippetLdBuilder;
  33.     /**
  34.      * @var LogoRichSnippetLdBuilderInterface
  35.      */
  36.     private $logoRichSnippetLdBuilder;
  37.     /**
  38.      * @var PlainBreadcrumbCalculator
  39.      */
  40.     private $plainBreadcrumbCalculator;
  41.     /**
  42.      * @var CustomSettingLoader
  43.      */
  44.     private $customSettingLoader;
  45.     /**
  46.      * @var LocalBusinessRichSnippetLdBuilderInterface
  47.      */
  48.     protected $localBusinessRichSnippetLdBuilder;
  49.     /**
  50.      * @param ProductRichSnippetLdBuilderInterface $productRichSnippetLdBuilder
  51.      * @param BreadcrumbRichSnippetLdBuilderInterface $breadcrumbRichSnippetLdBuilder
  52.      * @param LogoRichSnippetLdBuilderInterface $logoRichSnippetLdBuilder
  53.      * @param PlainBreadcrumbCalculator $plainBreadcrumbCalculator
  54.      * @param CustomSettingLoader $customSettingLoader
  55.      * @param LocalBusinessRichSnippetLdBuilderInterface $localBusinessRichSnippetLdBuilder
  56.      */
  57.     public function __construct(ProductRichSnippetLdBuilderInterface $productRichSnippetLdBuilderBreadcrumbRichSnippetLdBuilderInterface $breadcrumbRichSnippetLdBuilderLogoRichSnippetLdBuilderInterface $logoRichSnippetLdBuilderPlainBreadcrumbCalculator $plainBreadcrumbCalculatorCustomSettingLoader $customSettingLoaderLocalBusinessRichSnippetLdBuilderInterface $localBusinessRichSnippetLdBuilder)
  58.     {
  59.         $this->productRichSnippetLdBuilder $productRichSnippetLdBuilder;
  60.         $this->breadcrumbRichSnippetLdBuilder $breadcrumbRichSnippetLdBuilder;
  61.         $this->logoRichSnippetLdBuilder $logoRichSnippetLdBuilder;
  62.         $this->plainBreadcrumbCalculator $plainBreadcrumbCalculator;
  63.         $this->customSettingLoader $customSettingLoader;
  64.         $this->localBusinessRichSnippetLdBuilder $localBusinessRichSnippetLdBuilder;
  65.     }
  66.     public static function getSubscribedEvents()
  67.     {
  68.         return [
  69.             ProductPageLoadedEvent::class => 'addInstallment',
  70.             NavigationPageLoadedEvent::class => 'addInstallment',
  71.         ];
  72.     }
  73.     /**
  74.      * @param PageLoadedEvent $event
  75.      * @throws InconsistentCriteriaIdsException
  76.      */
  77.     public function addInstallment(PageLoadedEvent $event)
  78.     {
  79.         $ld = [];
  80.         /** @var Page|ProductPage $page */
  81.         $page $event->getPage();
  82.         /** @var SalesChannelProductEntity $productEntity */
  83.         $productEntity null;
  84.         $salesChannelEntity $event->getSalesChannelContext()->getSalesChannel();
  85.         $salesChannelDomainId $event->getRequest()->attributes->get(SalesChannelRequest::ATTRIBUTE_DOMAIN_ID);
  86.         $plainBreadcrumb null;
  87.         /** Load the custom settings */
  88.         $customSettings $this->customSettingLoader->load($salesChannelEntity->getId(), true);
  89.         /** Abort, if json ld support was not activated */
  90.         if (false === $customSettings->getRichSnippets()->getGeneral()->isActive()) {
  91.             return;
  92.         }
  93.         /** Try to fetch the navigation tree and the active category */
  94.         if (null !== $page->getHeader() && null !== $page->getHeader()->getNavigation()) {
  95.             $navigationTree $page->getHeader()->getNavigation()->getTree();
  96.             $category $page->getHeader()->getNavigation()->getActive();
  97.         } else {
  98.             $navigationTree null;
  99.             $category null;
  100.         }
  101.         /** Product page */
  102.         if ($page instanceof ProductPage) {
  103.             /** Calculate product ld data */
  104.             $productEntity $page->getProduct();
  105.             /*
  106.              *  TODO data appears to come from here (productEntity->manufacturer)
  107.              */
  108.             $currencyEntity $event->getSalesChannelContext()->getCurrency();
  109.             $reviewLoaderResult $event->getPage()->getReviews();
  110.             /** Build the product ld data */
  111.             $ld[] = $this->productRichSnippetLdBuilder->build(
  112.                 new ProductRichSnippetLdBuilderStruct(
  113.                     $customSettings,
  114.                     $productEntity,
  115.                     $salesChannelEntity,
  116.                     $currencyEntity,
  117.                     $reviewLoaderResult,
  118.                     $salesChannelDomainId
  119.                 )
  120.             );
  121.             /** Calculate breadcrumb ld data */
  122.             if (null !== $navigationTree) {
  123.                 $plainBreadcrumb $this->plainBreadcrumbCalculator->getProductBreadcrumb($navigationTree$productEntity);
  124.             }
  125.         } else {
  126.             /** Calculate breadcrumb ld data */
  127.             if (null !== $navigationTree && null !== $category) {
  128.                 $plainBreadcrumb $this->plainBreadcrumbCalculator->getCategoryBreadcrumb($category$salesChannelEntity);
  129.             }
  130.         }
  131.         /** Build the breadcrumb ld, if data available */
  132.         if (null !== $plainBreadcrumb) {
  133.             $ld[] = $this->breadcrumbRichSnippetLdBuilder->build(
  134.                 new BreadcrumbRichSnippetLdBuilderStruct(
  135.                     $customSettings,
  136.                     $plainBreadcrumb,
  137.                     $salesChannelEntity,
  138.                     $salesChannelDomainId,
  139.                     $productEntity
  140.                 )
  141.             );
  142.         }
  143.         /** Build the logo ld data */
  144.         $ld[] = $this->logoRichSnippetLdBuilder->build(
  145.             new LogoRichSnippetLdBuilderStruct(
  146.                 $customSettings,
  147.                 $salesChannelEntity
  148.             )
  149.         );
  150.         /** Build the local business ld data */
  151.         $ld[] = $this->localBusinessRichSnippetLdBuilder->build(
  152.             new LocalBusinessRichSnippetLdBuilderStruct(
  153.                 $customSettings,
  154.                 $salesChannelEntity
  155.             )
  156.         );
  157.         if(!empty($ld)) {
  158.             /** Wrap the ld data in a struct */
  159.             $richSnippetDataStruct = new RichSnippetDataStruct($ld);
  160.             if(!empty($richSnippetDataStruct->getLdJson())) {
  161.                 $page->addExtension(
  162.                     self::DREISC_SEO_INSTALLMENT_RICH_SNIPPET_DATA,
  163.                     $richSnippetDataStruct
  164.                 );
  165.             }
  166.         }
  167.     }
  168. }