<?php
declare(strict_types=0);
/*
* WellCommerce Foundation
*
* This file is part of the WellCommerce package.
*
* (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace WellCommerce\Bundle\AppBundle\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\PropertyAccess\PropertyAccess;
use WellCommerce\Bundle\AppBundle\DataSet\Admin\ThemeDataSet;
use WellCommerce\Bundle\AppBundle\Entity\Client;
use WellCommerce\Bundle\AppBundle\Entity\Theme;
use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
use WellCommerce\Bundle\AppBundle\Service\Theme\CssEditor\CssEditor;
use WellCommerce\Bundle\AppBundle\Service\Theme\Storage\ThemeStorage;
use WellCommerce\Bundle\CatalogBundle\Controller\Front\CategoryController;
use WellCommerce\Bundle\CatalogBundle\Controller\Front\ProductController;
use WellCommerce\Bundle\CatalogBundle\Entity\Category;
use WellCommerce\Bundle\CatalogBundle\Entity\Product;
use WellCommerce\Bundle\CmsBundle\Controller\Front\PageController;
use WellCommerce\Bundle\CmsBundle\Entity\Page;
use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelper;
use WellCommerce\Bundle\CoreBundle\Helper\Security\SecurityHelper;
use WellCommerce\Bundle\CoreBundle\Helper\Translator\TranslatorHelper;
use WellCommerce\Component\Form\Event\FormEvent;
/**
* Class ThemeSubscriber
*
* @author Adam Piotrowski <adam@wellcommerce.org>
*/
class ThemeSubscriber implements EventSubscriberInterface
{
protected RequestHelper $requestHelper;
protected EntityManagerInterface $entityManager;
protected SecurityHelper $securityHelper;
protected ThemeDataSet $themeDataSet;
protected ThemeStorage $themeStorage;
protected TranslatorHelper $translatorHelper;
protected ShopStorage $shopStorage;
protected CssEditor $cssEditor;
public function __construct(
RequestHelper $requestHelper,
EntityManagerInterface $entityManager,
SecurityHelper $securityHelper,
ThemeDataSet $themeDataSet,
ThemeStorage $themeStorage,
TranslatorHelper $translatorHelper,
ShopStorage $shopStorage,
CssEditor $cssEditor
) {
$this->requestHelper = $requestHelper;
$this->entityManager = $entityManager;
$this->securityHelper = $securityHelper;
$this->themeDataSet = $themeDataSet;
$this->themeStorage = $themeStorage;
$this->translatorHelper = $translatorHelper;
$this->shopStorage = $shopStorage;
$this->cssEditor = $cssEditor;
}
public function onKernelController(ControllerEvent $event)
{
$theme = $this->shopStorage->getCurrentShop()->getTheme();
$this->themeStorage->setCurrentTheme($theme);
}
public function onThemePreCreate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof Theme) {
$request = $this->requestHelper->getCurrentRequest();
$settings = $this->getCssSettingsFromRequest($request);
$resource->setCss($settings);
}
}
public function onThemePreUpdate(EntityEvent $event)
{
$resource = $event->getEntity();
if ($resource instanceof Theme) {
$request = $this->requestHelper->getCurrentRequest();
$settings = $this->getCssSettingsFromRequest($request);
$settings = $this->mergeUnmodifiedSettings($resource->getCss(), $settings);
$resource->setCss($settings);
}
}
private function getCssSettingsFromRequest(Request $request)
{
$settings = [];
$accessor = PropertyAccess::createPropertyAccessor();
$parameters = $request->request->all();
if ($accessor->isReadable($parameters, '[css_data]')) {
$settings = $accessor->getValue($parameters, '[css_data]');
}
return !is_array($settings) ? [] : $settings;
}
private function mergeUnmodifiedSettings(array $oldSettings, array $newSettings): array
{
foreach ($newSettings as $key => &$setting) {
if (is_array($setting) && array_key_exists('unmodified', $setting) && isset($setting[0])) {
$setting[0] = (0 !== (int) $setting[0]) ? $setting[0] : $oldSettings[$key][0];
}
}
return $newSettings;
}
public static function getSubscribedEvents()
{
return [
'theme.pre_create' => 'onThemePreCreate',
'theme.pre_update' => 'onThemePreUpdate',
KernelEvents::CONTROLLER => ['onKernelController', -10],
];
}
}