custom/plugins/DreiscSeoPro/src/Subscriber/Installment/SocialMedia/SocialMediaSubscriber.php line 55

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSeoPro\Subscriber\Installment\SocialMedia;
  3. use DreiscSeoPro\Core\CustomSetting\CustomSettingLoader;
  4. use DreiscSeoPro\Core\SocialMedia\SocialMediaFetcher;
  5. use DreiscSeoPro\Core\SocialMedia\SocialMediaFetcherStruct;
  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 SocialMediaSubscriber implements EventSubscriberInterface
  17. {
  18.     public const DREISC_SEO_INSTALLMENT_SOCIAL_MEDIA_DATA 'dreiscSeoInstallmentSocialMediaData';
  19.     /**
  20.      * @var CustomSettingLoader
  21.      */
  22.     private $customSettingLoader;
  23.     /**
  24.      * @var SocialMediaFetcher
  25.      */
  26.     private $socialMediaFetcher;
  27.     /**
  28.      * @param CustomSettingLoader $customSettingLoader
  29.      * @param SocialMediaFetcher $socialMediaFetcher
  30.      */
  31.     public function __construct(CustomSettingLoader $customSettingLoaderSocialMediaFetcher $socialMediaFetcher)
  32.     {
  33.         $this->customSettingLoader $customSettingLoader;
  34.         $this->socialMediaFetcher $socialMediaFetcher;
  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.         $seoDataFetchResultStruct null;
  54.         /** Load the custom settings */
  55.         $customSettings $this->customSettingLoader->load($salesChannelEntity->getId(), true);
  56.         if ($page instanceof ProductPage) {
  57.             $seoDataFetchResultStruct $this->socialMediaFetcher->fetch(
  58.                 new SocialMediaFetcherStruct(
  59.                     $customSettings,
  60.                     ProductDefinition::ENTITY_NAME,
  61.                     $page->getProduct()->getId(),
  62.                     $languageId
  63.                 )
  64.             );
  65.         } elseif ($page instanceof NavigationPage) {
  66.             /** Try to fetch the active category */
  67.             $categoryId $event->getRequest()->attributes->get('navigationId');
  68.             /** Fetch the home category id */
  69.             if(
  70.                 empty($categoryId) &&
  71.                 'frontend.home.page' === $event->getRequest()->attributes->get('_route') &&
  72.                 null !== $page->getHeader() &&
  73.                 null !== $page->getHeader()->getNavigation()
  74.             ) {
  75.                 $activeCategory $page->getHeader()->getNavigation()->getActive();
  76.                 if (null !== $activeCategory) {
  77.                     $categoryId $activeCategory->getId();
  78.                 }
  79.             }
  80.             /** Abort, if category is null */
  81.             if (null === $categoryId) {
  82.                 return;
  83.             }
  84.             $seoDataFetchResultStruct $this->socialMediaFetcher->fetch(
  85.                 new SocialMediaFetcherStruct(
  86.                     $customSettings,
  87.                     CategoryDefinition::ENTITY_NAME,
  88.                     $categoryId,
  89.                     $languageId
  90.                 )
  91.             );
  92.         }
  93.         if (null !== $seoDataFetchResultStruct) {
  94.             $page->addExtension(
  95.                 self::DREISC_SEO_INSTALLMENT_SOCIAL_MEDIA_DATA,
  96.                 new SocialMediaDataStruct(
  97.                     $seoDataFetchResultStruct->getFacebookTitle(),
  98.                     $seoDataFetchResultStruct->getFacebookDescription(),
  99.                     $seoDataFetchResultStruct->getFacebookImage(),
  100.                     $seoDataFetchResultStruct->getTwitterTitle(),
  101.                     $seoDataFetchResultStruct->getTwitterDescription(),
  102.                     $seoDataFetchResultStruct->getTwitterImage()
  103.                 )
  104.             );
  105.         }
  106.     }
  107. }