src/WellCommerce/Bundle/AppBundle/EventListener/ThemeSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\AppBundle\EventListener;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\PropertyAccess\PropertyAccess;
  20. use WellCommerce\Bundle\AppBundle\DataSet\Admin\ThemeDataSet;
  21. use WellCommerce\Bundle\AppBundle\Entity\Client;
  22. use WellCommerce\Bundle\AppBundle\Entity\Theme;
  23. use WellCommerce\Bundle\AppBundle\Service\Shop\Storage\ShopStorage;
  24. use WellCommerce\Bundle\AppBundle\Service\Theme\CssEditor\CssEditor;
  25. use WellCommerce\Bundle\AppBundle\Service\Theme\Storage\ThemeStorage;
  26. use WellCommerce\Bundle\CatalogBundle\Controller\Front\CategoryController;
  27. use WellCommerce\Bundle\CatalogBundle\Controller\Front\ProductController;
  28. use WellCommerce\Bundle\CatalogBundle\Entity\Category;
  29. use WellCommerce\Bundle\CatalogBundle\Entity\Product;
  30. use WellCommerce\Bundle\CmsBundle\Controller\Front\PageController;
  31. use WellCommerce\Bundle\CmsBundle\Entity\Page;
  32. use WellCommerce\Bundle\CoreBundle\Doctrine\Event\EntityEvent;
  33. use WellCommerce\Bundle\CoreBundle\Helper\Request\RequestHelper;
  34. use WellCommerce\Bundle\CoreBundle\Helper\Security\SecurityHelper;
  35. use WellCommerce\Bundle\CoreBundle\Helper\Translator\TranslatorHelper;
  36. use WellCommerce\Component\Form\Event\FormEvent;
  37. /**
  38.  * Class ThemeSubscriber
  39.  *
  40.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  41.  */
  42. class ThemeSubscriber implements EventSubscriberInterface
  43. {
  44.     protected RequestHelper          $requestHelper;
  45.     protected EntityManagerInterface $entityManager;
  46.     protected SecurityHelper         $securityHelper;
  47.     protected ThemeDataSet           $themeDataSet;
  48.     protected ThemeStorage           $themeStorage;
  49.     protected TranslatorHelper       $translatorHelper;
  50.     protected ShopStorage            $shopStorage;
  51.     protected CssEditor              $cssEditor;
  52.     public function __construct(
  53.         RequestHelper $requestHelper,
  54.         EntityManagerInterface $entityManager,
  55.         SecurityHelper $securityHelper,
  56.         ThemeDataSet $themeDataSet,
  57.         ThemeStorage $themeStorage,
  58.         TranslatorHelper $translatorHelper,
  59.         ShopStorage $shopStorage,
  60.         CssEditor $cssEditor
  61.     ) {
  62.         $this->requestHelper    $requestHelper;
  63.         $this->entityManager    $entityManager;
  64.         $this->securityHelper   $securityHelper;
  65.         $this->themeDataSet     $themeDataSet;
  66.         $this->themeStorage     $themeStorage;
  67.         $this->translatorHelper $translatorHelper;
  68.         $this->shopStorage      $shopStorage;
  69.         $this->cssEditor        $cssEditor;
  70.     }
  71.     public function onKernelController(ControllerEvent $event)
  72.     {
  73.         $theme $this->shopStorage->getCurrentShop()->getTheme();
  74.         $this->themeStorage->setCurrentTheme($theme);
  75.     }
  76.     public function onThemePreCreate(EntityEvent $event)
  77.     {
  78.         $resource $event->getEntity();
  79.         if ($resource instanceof Theme) {
  80.             $request  $this->requestHelper->getCurrentRequest();
  81.             $settings $this->getCssSettingsFromRequest($request);
  82.             $resource->setCss($settings);
  83.         }
  84.     }
  85.     public function onThemePreUpdate(EntityEvent $event)
  86.     {
  87.         $resource $event->getEntity();
  88.         if ($resource instanceof Theme) {
  89.             $request  $this->requestHelper->getCurrentRequest();
  90.             $settings $this->getCssSettingsFromRequest($request);
  91.             $settings $this->mergeUnmodifiedSettings($resource->getCss(), $settings);
  92.             $resource->setCss($settings);
  93.         }
  94.     }
  95.     private function getCssSettingsFromRequest(Request $request)
  96.     {
  97.         $settings   = [];
  98.         $accessor   PropertyAccess::createPropertyAccessor();
  99.         $parameters $request->request->all();
  100.         if ($accessor->isReadable($parameters'[css_data]')) {
  101.             $settings $accessor->getValue($parameters'[css_data]');
  102.         }
  103.         return !is_array($settings) ? [] : $settings;
  104.     }
  105.     private function mergeUnmodifiedSettings(array $oldSettings, array $newSettings): array
  106.     {
  107.         foreach ($newSettings as $key => &$setting) {
  108.             if (is_array($setting) && array_key_exists('unmodified'$setting) && isset($setting[0])) {
  109.                 $setting[0] = (!== (int) $setting[0]) ? $setting[0] : $oldSettings[$key][0];
  110.             }
  111.         }
  112.         return $newSettings;
  113.     }
  114.     public static function getSubscribedEvents()
  115.     {
  116.         return [
  117.             'theme.pre_create'       => 'onThemePreCreate',
  118.             'theme.pre_update'       => 'onThemePreUpdate',
  119.             KernelEvents::CONTROLLER => ['onKernelController', -10],
  120.         ];
  121.     }
  122. }