core/lib/Thelia/Action/Country.php line 80

  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\Country\CountryCreateEvent;
  15. use Thelia\Core\Event\Country\CountryDeleteEvent;
  16. use Thelia\Core\Event\Country\CountryToggleDefaultEvent;
  17. use Thelia\Core\Event\Country\CountryToggleVisibilityEvent;
  18. use Thelia\Core\Event\Country\CountryUpdateEvent;
  19. use Thelia\Core\Event\TheliaEvents;
  20. use Thelia\Model\Country as CountryModel;
  21. use Thelia\Model\CountryQuery;
  22. /**
  23.  * Class Country.
  24.  *
  25.  * @author Manuel Raynaud <manu@raynaud.io>
  26.  */
  27. class Country extends BaseAction implements EventSubscriberInterface
  28. {
  29.     public function create(CountryCreateEvent $event): void
  30.     {
  31.         $country = new CountryModel();
  32.         $country
  33.             ->setVisible($event->isVisible())
  34.             ->setIsocode($event->getIsocode())
  35.             ->setIsoalpha2($event->getIsoAlpha2())
  36.             ->setIsoalpha3($event->getIsoAlpha3())
  37.             ->setHasStates($event->isHasStates())
  38.             ->setLocale($event->getLocale())
  39.             ->setTitle($event->getTitle())
  40.             ->save();
  41.         $event->setCountry($country);
  42.     }
  43.     public function update(CountryUpdateEvent $event): void
  44.     {
  45.         if (null !== $country CountryQuery::create()->findPk($event->getCountryId())) {
  46.             $country
  47.                 ->setVisible($event->isVisible())
  48.                 ->setIsocode($event->getIsocode())
  49.                 ->setIsoalpha2($event->getIsoAlpha2())
  50.                 ->setIsoalpha3($event->getIsoAlpha3())
  51.                 ->setHasStates($event->isHasStates())
  52.                 ->setNeedZipCode($event->isNeedZipCode())
  53.                 ->setZipCodeFormat($event->getZipCodeFormat())
  54.                 ->setLocale($event->getLocale())
  55.                 ->setTitle($event->getTitle())
  56.                 ->setChapo($event->getChapo())
  57.                 ->setDescription($event->getDescription())
  58.                 ->save();
  59.             $event->setCountry($country);
  60.         }
  61.     }
  62.     public function delete(CountryDeleteEvent $event): void
  63.     {
  64.         if (null !== $country CountryQuery::create()->findPk($event->getCountryId())) {
  65.             $country->delete();
  66.             $event->setCountry($country);
  67.         }
  68.     }
  69.     public function toggleDefault(CountryToggleDefaultEvent $event): void
  70.     {
  71.         if (null !== $country CountryQuery::create()->findPk($event->getCountryId())) {
  72.             $country->toggleDefault();
  73.             $event->setCountry($country);
  74.         }
  75.     }
  76.     /**
  77.      * Toggle Country visibility.
  78.      */
  79.     public function toggleVisibility(CountryToggleVisibilityEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  80.     {
  81.         $country $event->getCountry();
  82.         $country
  83.             ->setVisible(!$country->getVisible())
  84.             ->save();
  85.         $event->setCountry($country);
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public static function getSubscribedEvents()
  91.     {
  92.         return [
  93.             TheliaEvents::COUNTRY_CREATE => ['create'128],
  94.             TheliaEvents::COUNTRY_UPDATE => ['update'128],
  95.             TheliaEvents::COUNTRY_DELETE => ['delete'128],
  96.             TheliaEvents::COUNTRY_TOGGLE_DEFAULT => ['toggleDefault'128],
  97.             TheliaEvents::COUNTRY_TOGGLE_VISIBILITY => ['toggleVisibility'128],
  98.         ];
  99.     }
  100. }