core/lib/Thelia/Action/Area.php line 76

  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\Area\AreaAddCountryEvent;
  15. use Thelia\Core\Event\Area\AreaRemoveCountryEvent;
  16. use Thelia\Core\Event\TheliaEvents;
  17. use Thelia\Model\CountryArea;
  18. use Thelia\Model\CountryAreaQuery;
  19. use Thelia\Model\Event\AreaEvent;
  20. /**
  21.  * Class Area.
  22.  *
  23.  * @author Manuel Raynaud <manu@raynaud.io>
  24.  */
  25. class Area extends BaseAction implements EventSubscriberInterface
  26. {
  27.     public function addCountry(AreaAddCountryEvent $event): void
  28.     {
  29.         $countryIds $event->getCountryIds();
  30.         $area $event->getModel();
  31.         foreach ($countryIds as $countryId) {
  32.             $countryArea = new CountryArea();
  33.             $country explode('-'$countryId);
  34.             if (\count($country) === 1) {
  35.                 $country[1] = null;
  36.             }
  37.             if ($country[1] == 0) {
  38.                 $country[1] = null;
  39.             }
  40.             $countryArea
  41.                 ->setAreaId($area->getId())
  42.                 ->setCountryId($country[0])
  43.                 ->setStateId($country[1])
  44.                 ->save()
  45.             ;
  46.         }
  47.     }
  48.     public function removeCountry(AreaRemoveCountryEvent $event)
  49.     {
  50.         $area $event->getModel();
  51.         CountryAreaQuery::create()
  52.                 ->filterByCountryId($event->getCountryIds())
  53.                 ->filterByStateId($event->getStateId())
  54.                 ->filterByAreaId($area->getId())
  55.                 ->delete();
  56.         return $area;
  57.     }
  58.     public function delete(AreaEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  59.     {
  60.         $event->getModel()
  61.             ->delete();
  62.     }
  63.     public function save(AreaEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  64.     {
  65.         $event->getModel()
  66.             ->save();
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public static function getSubscribedEvents()
  72.     {
  73.         return [
  74.             TheliaEvents::AREA_ADD_COUNTRY => ['addCountry'128],
  75.             TheliaEvents::AREA_REMOVE_COUNTRY => ['removeCountry'128],
  76.             TheliaEvents::AREA_POSTAGE_UPDATE => ['updatePostage'128],
  77.             TheliaEvents::AREA_DELETE => ['delete'128],
  78.             TheliaEvents::AREA_CREATE => ['save'128],
  79.             TheliaEvents::AREA_UPDATE => ['save'128],
  80.         ];
  81.     }
  82. }