<?php declare(strict_types=1);
namespace DreiscSeoPro\Subscriber\Installment\SocialMedia;
use DreiscSeoPro\Core\CustomSetting\CustomSettingLoader;
use DreiscSeoPro\Core\SocialMedia\SocialMediaFetcher;
use DreiscSeoPro\Core\SocialMedia\SocialMediaFetcherStruct;
use Shopware\Core\Content\Category\CategoryDefinition;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Storefront\Page\Navigation\NavigationPage;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\Page;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPage;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SocialMediaSubscriber implements EventSubscriberInterface
{
public const DREISC_SEO_INSTALLMENT_SOCIAL_MEDIA_DATA = 'dreiscSeoInstallmentSocialMediaData';
/**
* @var CustomSettingLoader
*/
private $customSettingLoader;
/**
* @var SocialMediaFetcher
*/
private $socialMediaFetcher;
/**
* @param CustomSettingLoader $customSettingLoader
* @param SocialMediaFetcher $socialMediaFetcher
*/
public function __construct(CustomSettingLoader $customSettingLoader, SocialMediaFetcher $socialMediaFetcher)
{
$this->customSettingLoader = $customSettingLoader;
$this->socialMediaFetcher = $socialMediaFetcher;
}
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => 'addInstallment',
NavigationPageLoadedEvent::class => 'addInstallment',
];
}
/**
* @param PageLoadedEvent $event
* @throws InconsistentCriteriaIdsException
*/
public function addInstallment(PageLoadedEvent $event)
{
/** @var Page|ProductPage|NavigationPage $page */
$page = $event->getPage();
$salesChannelEntity = $event->getSalesChannelContext()->getSalesChannel();
$languageId = $event->getContext()->getLanguageId();
$seoDataFetchResultStruct = null;
/** Load the custom settings */
$customSettings = $this->customSettingLoader->load($salesChannelEntity->getId(), true);
if ($page instanceof ProductPage) {
$seoDataFetchResultStruct = $this->socialMediaFetcher->fetch(
new SocialMediaFetcherStruct(
$customSettings,
ProductDefinition::ENTITY_NAME,
$page->getProduct()->getId(),
$languageId
)
);
} elseif ($page instanceof NavigationPage) {
/** Try to fetch the active category */
$categoryId = $event->getRequest()->attributes->get('navigationId');
/** Fetch the home category id */
if(
empty($categoryId) &&
'frontend.home.page' === $event->getRequest()->attributes->get('_route') &&
null !== $page->getHeader() &&
null !== $page->getHeader()->getNavigation()
) {
$activeCategory = $page->getHeader()->getNavigation()->getActive();
if (null !== $activeCategory) {
$categoryId = $activeCategory->getId();
}
}
/** Abort, if category is null */
if (null === $categoryId) {
return;
}
$seoDataFetchResultStruct = $this->socialMediaFetcher->fetch(
new SocialMediaFetcherStruct(
$customSettings,
CategoryDefinition::ENTITY_NAME,
$categoryId,
$languageId
)
);
}
if (null !== $seoDataFetchResultStruct) {
$page->addExtension(
self::DREISC_SEO_INSTALLMENT_SOCIAL_MEDIA_DATA,
new SocialMediaDataStruct(
$seoDataFetchResultStruct->getFacebookTitle(),
$seoDataFetchResultStruct->getFacebookDescription(),
$seoDataFetchResultStruct->getFacebookImage(),
$seoDataFetchResultStruct->getTwitterTitle(),
$seoDataFetchResultStruct->getTwitterDescription(),
$seoDataFetchResultStruct->getTwitterImage()
)
);
}
}
}