<?php declare(strict_types=1);
namespace zenit\PlatformFontChange\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
use Shopware\Core\System\SystemConfig\Exception\InvalidDomainException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use zenit\PlatformFontChange\Struct\SystemConfigData;
class StorefrontRenderSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $pluginName = 'zenitPlatformFontChange';
/**
* @var string
*/
private $configPath = 'zenitPlatformFontChange.config.';
/**
* @var SystemConfigService
*/
private $systemConfig;
/**
* HeaderPageletLoadedSubscriber constructor.
* @param SystemConfigService $systemConfig
*/
public function __construct(SystemConfigService $systemConfig)
{
$this->systemConfig = $systemConfig;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return[
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
/**
* @param StorefrontRenderEvent $event
* @throws InvalidDomainException
* @throws InvalidUuidException
* @throws InconsistentCriteriaIdsException
*/
public function onStorefrontRender(StorefrontRenderEvent $event)
{
$shopId = $event->getSalesChannelContext()->getSalesChannel()->getId();
// get config
$systemConfig = $this->systemConfig->getDomain($this->configPath, $shopId, true);
// replace domainstrings in keys
$config = [];
foreach($systemConfig as $key => $value) {
$config[str_replace($this->configPath, '',$key)] = $value;
}
if ($this->systemConfig->get($this->configPath . 'activeExtern', $shopId)) {
/**
* @var string $fontBaseExtern
* @var string $styleBaseExtern
* @var string $fontHeadlineExtern
* @var string $styleHeadlineExtern
*/
$fontBaseExtern = $this->systemConfig->get($this->configPath . 'fontBaseExtern', $shopId);
$styleBaseExtern = $this->systemConfig->get($this->configPath . 'styleBaseExtern', $shopId);
$fontHeadlineExtern = $this->systemConfig->get($this->configPath . 'fontHeadlineExtern', $shopId);
$styleHeadlineExtern = $this->systemConfig->get($this->configPath . 'styleHeadlineExtern', $shopId);
// get font family
if (!empty($fontBaseExtern)) {
if (strpos($fontBaseExtern, "'") === false) {
if(strpos($fontBaseExtern, ",") !== false) {
$fontFamilyBaseExtern = substr($fontBaseExtern, 0, strpos($fontBaseExtern, ","));
} else {
$fontFamilyBaseExtern = $fontBaseExtern;
}
} else {
$fontFamilyBaseExtern = $this->getStringBetween($fontBaseExtern, "'", "'");
}
}
if (!empty($fontHeadlineExtern)) {
if (strpos($fontHeadlineExtern, "'") === false) {
if(strpos($fontHeadlineExtern, ",") !== false) {
$fontFamilyHeadlineExtern = substr($fontHeadlineExtern, 0, strpos($fontHeadlineExtern, ","));
} else {
$fontFamilyHeadlineExtern = $fontHeadlineExtern;
}
} else {
$fontFamilyHeadlineExtern = $this->getStringBetween($fontHeadlineExtern, "'", "'");
}
}
// avoid double implementation when base and headline fonts are the same
if (!empty($fontFamilyBaseExtern) && !empty($fontFamilyHeadlineExtern)) {
if ($fontFamilyBaseExtern === $fontFamilyHeadlineExtern) {
$config['fontFamilyCumulativeExtern'] = str_replace(' ', '+', $fontFamilyBaseExtern);
if (!empty($styleBaseExtern) && !empty($styleHeadlineExtern)) {
$config['fontStyleCumulativeExtern'] = array_merge($styleBaseExtern, $styleHeadlineExtern);
}
} else {
$config['fontFamilyBaseExtern'] = str_replace(' ', '+', $fontFamilyBaseExtern);
$config['fontStyleBaseExtern'] = $styleBaseExtern;
$config['fontFamilyHeadlineExtern'] = str_replace(' ', '+', $fontFamilyHeadlineExtern);
$config['fontStyleHeadlineExtern'] = $styleHeadlineExtern;
}
}
}
// set config
$configValues = new SystemConfigData($config);
// add config
$event->getContext()->addExtension($this->pluginName, $configValues);
}
/**
* @param $string
* @param $start
* @param $end
* @return false|string
*/
private function getStringBetween($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
}