<?php
declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o.
* @link https://crehler.com/
* @support support@crehler.com
*
* @author Mateusz FlasiĆski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Catalog\Subscriber;
use Crehler\Catalog\Service\Seo\SeoUrlTemplateManager;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SystemConfigWritten implements EventSubscriberInterface
{
private SeoUrlTemplateManager $seoUrlTemplateManager;
public function __construct(SeoUrlTemplateManager $seoUrlTemplateManager)
{
$this->seoUrlTemplateManager = $seoUrlTemplateManager;
}
public static function getSubscribedEvents()
{
return ['system_config.written' => 'onConfigWritten'];
}
public function onConfigWritten(EntityWrittenEvent $event): void
{
foreach ($event->getWriteResults() as $entityWriteResult) {
if (!isset($entityWriteResult->getPayload()['configurationKey'])) {
return;
}
if (!isset($entityWriteResult->getPayload()['configurationValue'])) {
return;
}
if (strpos($entityWriteResult->getPayload()['configurationKey'], 'CrehlerCatalog.config.seo') === false) {
continue;
}
$this->seoUrlTemplateManager->writeTemplateFromConfigKey($entityWriteResult->getPayload()['configurationKey'], $entityWriteResult->getPayload()['configurationValue']);
}
}
}