core/lib/Thelia/Action/State.php line 48

  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\EventDispatcherInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\State\StateCreateEvent;
  15. use Thelia\Core\Event\State\StateDeleteEvent;
  16. use Thelia\Core\Event\State\StateToggleVisibilityEvent;
  17. use Thelia\Core\Event\State\StateUpdateEvent;
  18. use Thelia\Core\Event\TheliaEvents;
  19. use Thelia\Model\State as StateModel;
  20. use Thelia\Model\StateQuery;
  21. /**
  22.  * Class State.
  23.  *
  24.  * @author Julien Chanséaume <julien@thelia.net>
  25.  */
  26. class State extends BaseAction implements EventSubscriberInterface
  27. {
  28.     public function create(StateCreateEvent $event): void
  29.     {
  30.         $state = new StateModel();
  31.         $state
  32.             ->setVisible($event->isVisible())
  33.             ->setCountryId($event->getCountry())
  34.             ->setIsocode($event->getIsocode())
  35.             ->setLocale($event->getLocale())
  36.             ->setTitle($event->getTitle())
  37.             ->save()
  38.         ;
  39.         $event->setState($state);
  40.     }
  41.     public function update(StateUpdateEvent $event): void
  42.     {
  43.         if (null !== $state StateQuery::create()->findPk($event->getStateId())) {
  44.             $state
  45.                 ->setVisible($event->isVisible())
  46.                 ->setCountryId($event->getCountry())
  47.                 ->setIsocode($event->getIsocode())
  48.                 ->setLocale($event->getLocale())
  49.                 ->setTitle($event->getTitle())
  50.                 ->save()
  51.             ;
  52.             $event->setState($state);
  53.         }
  54.     }
  55.     public function delete(StateDeleteEvent $event): void
  56.     {
  57.         if (null !== $state StateQuery::create()->findPk($event->getStateId())) {
  58.             $state->delete();
  59.             $event->setState($state);
  60.         }
  61.     }
  62.     /**
  63.      * Toggle State visibility.
  64.      */
  65.     public function toggleVisibility(StateToggleVisibilityEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  66.     {
  67.         $state $event->getState();
  68.         $state
  69.             ->setVisible(!$state->getVisible())
  70.             ->save()
  71.         ;
  72.         $event->setState($state);
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public static function getSubscribedEvents()
  78.     {
  79.         return [
  80.             TheliaEvents::STATE_CREATE => ['create'128],
  81.             TheliaEvents::STATE_UPDATE => ['update'128],
  82.             TheliaEvents::STATE_DELETE => ['delete'128],
  83.             TheliaEvents::STATE_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  84.         ];
  85.     }
  86. }