core/lib/Thelia/Action/Hook.php line 66

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Action;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. use Thelia\Core\Event\Cache\CacheEvent;
  15. use Thelia\Core\Event\Hook\HookCreateAllEvent;
  16. use Thelia\Core\Event\Hook\HookCreateEvent;
  17. use Thelia\Core\Event\Hook\HookDeactivationEvent;
  18. use Thelia\Core\Event\Hook\HookDeleteEvent;
  19. use Thelia\Core\Event\Hook\HookToggleActivationEvent;
  20. use Thelia\Core\Event\Hook\HookToggleNativeEvent;
  21. use Thelia\Core\Event\Hook\HookUpdateEvent;
  22. use Thelia\Core\Event\TheliaEvents;
  23. use Thelia\Model\Hook as HookModel;
  24. use Thelia\Model\HookQuery;
  25. /**
  26.  * Class HookAction.
  27.  *
  28.  * @author  Julien Chanséaume <jchanseaume@openstudio.fr>
  29.  */
  30. class Hook extends BaseAction implements EventSubscriberInterface
  31. {
  32.     /** @var string */
  33.     protected $cacheDir;
  34.     /** @var EventDispatcherInterface */
  35.     protected $dispatcher;
  36.     public function __construct($kernelCacheDirEventDispatcherInterface $dispatcher)
  37.     {
  38.         $this->cacheDir $kernelCacheDir;
  39.         $this->dispatcher $dispatcher;
  40.     }
  41.     public function create(HookCreateEvent $event): void
  42.     {
  43.         $hook = new HookModel();
  44.         $hook
  45.             ->setLocale($event->getLocale())
  46.             ->setCode($event->getCode())
  47.             ->setType($event->getType())
  48.             ->setNative($event->getNative())
  49.             ->setActivate($event->getActive())
  50.             ->setTitle($event->getTitle())
  51.             ->save();
  52.         $event->setHook($hook);
  53.         $this->cacheClear();
  54.     }
  55.     public function update(HookUpdateEvent $event): void
  56.     {
  57.         if (null !== $hook HookQuery::create()->findPk($event->getHookId())) {
  58.             $hook
  59.                 ->setLocale($event->getLocale())
  60.                 ->setCode($event->getCode())
  61.                 ->setType($event->getType())
  62.                 ->setNative($event->getNative())
  63.                 ->setActivate($event->getActive())
  64.                 ->setBlock($event->getBlock())
  65.                 ->setByModule($event->getByModule())
  66.                 ->setTitle($event->getTitle())
  67.                 ->setChapo($event->getChapo())
  68.                 ->setDescription($event->getDescription())
  69.                 ->save();
  70.             $event->setHook($hook);
  71.             $this->cacheClear();
  72.         }
  73.     }
  74.     public function delete(HookDeleteEvent $event): void
  75.     {
  76.         if (null !== $hook HookQuery::create()->findPk($event->getHookId())) {
  77.             $hook->delete();
  78.             $event->setHook($hook);
  79.             $this->cacheClear();
  80.         }
  81.     }
  82.     public function createAll(HookCreateAllEvent $event): void
  83.     {
  84.         $hook = new HookModel();
  85.         $hook
  86.             ->setLocale($event->getLocale())
  87.             ->setCode($event->getCode())
  88.             ->setType($event->getType())
  89.             ->setNative($event->getNative())
  90.             ->setActivate($event->getActive())
  91.             ->setBlock($event->getBlock())
  92.             ->setByModule($event->getByModule())
  93.             ->setTitle($event->getTitle())
  94.             ->setChapo($event->getChapo())
  95.             ->setDescription($event->getDescription())
  96.             ->save();
  97.         $event->setHook($hook);
  98.     }
  99.     public function deactivation(HookDeactivationEvent $event): void
  100.     {
  101.         if (null !== $hook HookQuery::create()->findPk($event->getHookId())) {
  102.             $hook
  103.                 ->setActivate(false)
  104.                 ->save();
  105.             $event->setHook($hook);
  106.         }
  107.     }
  108.     public function toggleNative(HookToggleNativeEvent $event): void
  109.     {
  110.         if (null !== $hook HookQuery::create()->findPk($event->getHookId())) {
  111.             $hook
  112.                 ->setNative(!$hook->getNative())
  113.                 ->save();
  114.             $event->setHook($hook);
  115.         }
  116.     }
  117.     public function toggleActivation(HookToggleActivationEvent $event): void
  118.     {
  119.         if (null !== $hook HookQuery::create()->findPk($event->getHookId())) {
  120.             $hook
  121.                 ->setActivate(!$hook->getActivate())
  122.                 ->save();
  123.             $event->setHook($hook);
  124.             $this->cacheClear();
  125.         }
  126.     }
  127.     protected function cacheClear(): void
  128.     {
  129.         $cacheEvent = new CacheEvent($this->cacheDir);
  130.         $this->dispatcher->dispatch($cacheEventTheliaEvents::CACHE_CLEAR);
  131.     }
  132.     /**
  133.      * {@inheritdoc}
  134.      */
  135.     public static function getSubscribedEvents()
  136.     {
  137.         return [
  138.             TheliaEvents::HOOK_CREATE => ['create'128],
  139.             TheliaEvents::HOOK_UPDATE => ['update'128],
  140.             TheliaEvents::HOOK_DELETE => ['delete'128],
  141.             TheliaEvents::HOOK_TOGGLE_ACTIVATION => ['toggleActivation'128],
  142.             TheliaEvents::HOOK_TOGGLE_NATIVE => ['toggleNative'128],
  143.             TheliaEvents::HOOK_CREATE_ALL => ['createAll'128],
  144.             TheliaEvents::HOOK_DEACTIVATION => ['deactivation'128],
  145.         ];
  146.     }
  147. }