core/lib/Thelia/Action/ShippingZone.php line 39

  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 Thelia\Core\Event\ShippingZone\ShippingZoneAddAreaEvent;
  14. use Thelia\Core\Event\ShippingZone\ShippingZoneRemoveAreaEvent;
  15. use Thelia\Core\Event\TheliaEvents;
  16. use Thelia\Model\AreaDeliveryModule;
  17. use Thelia\Model\AreaDeliveryModuleQuery;
  18. /**
  19.  * Class ShippingZone.
  20.  *
  21.  * @author Manuel Raynaud <manu@raynaud.io>
  22.  */
  23. class ShippingZone extends BaseAction implements EventSubscriberInterface
  24. {
  25.     public function addArea(ShippingZoneAddAreaEvent $event): void
  26.     {
  27.         $areaDelivery = new AreaDeliveryModule();
  28.         $areaDelivery
  29.             ->setAreaId($event->getAreaId())
  30.             ->setDeliveryModuleId($event->getShippingZoneId())
  31.             ->save();
  32.     }
  33.     public function removeArea(ShippingZoneRemoveAreaEvent $event): void
  34.     {
  35.         $areaDelivery AreaDeliveryModuleQuery::create()
  36.             ->filterByAreaId($event->getAreaId())
  37.             ->filterByDeliveryModuleId($event->getShippingZoneId())
  38.             ->findOne();
  39.         if ($areaDelivery) {
  40.             $areaDelivery->delete();
  41.         } else {
  42.             throw new \RuntimeException(sprintf('areaDeliveryModule not found with area_id = %d and delivery_module_id = %d'$event->getAreaId(), $event->getShippingZoneId()));
  43.         }
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public static function getSubscribedEvents()
  49.     {
  50.         return [
  51.             TheliaEvents::SHIPPING_ZONE_ADD_AREA => ['addArea'128],
  52.             TheliaEvents::SHIPPING_ZONE_REMOVE_AREA => ['removeArea'128],
  53.         ];
  54.     }
  55. }