core/lib/Thelia/Action/OrderStatus.php line 43

  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 Propel\Runtime\ActiveQuery\Criteria;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Thelia\Core\Event\OrderStatus\OrderStatusCreateEvent;
  16. use Thelia\Core\Event\OrderStatus\OrderStatusDeleteEvent;
  17. use Thelia\Core\Event\OrderStatus\OrderStatusEvent;
  18. use Thelia\Core\Event\OrderStatus\OrderStatusUpdateEvent;
  19. use Thelia\Core\Event\TheliaEvents;
  20. use Thelia\Core\Event\UpdatePositionEvent;
  21. use Thelia\Core\Translation\Translator;
  22. use Thelia\Model\OrderQuery;
  23. use Thelia\Model\OrderStatus as OrderStatusModel;
  24. use Thelia\Model\OrderStatusQuery;
  25. /**
  26.  * Class OrderStatus.
  27.  *
  28.  * @author Gilles Bourgeat <gbourgeat@openstudio.fr>
  29.  *
  30.  * @since 2.4
  31.  */
  32. class OrderStatus extends BaseAction implements EventSubscriberInterface
  33. {
  34.     public function create(OrderStatusCreateEvent $event): void
  35.     {
  36.         $this->createOrUpdate($event, new OrderStatusModel());
  37.     }
  38.     public function update(OrderStatusUpdateEvent $event): void
  39.     {
  40.         $orderStatus $this->getOrderStatus($event);
  41.         $this->createOrUpdate($event$orderStatus);
  42.     }
  43.     /**
  44.      * @throws \Exception
  45.      */
  46.     public function delete(OrderStatusDeleteEvent $event): void
  47.     {
  48.         $orderStatus $this->getOrderStatus($event);
  49.         if ($orderStatus->getProtectedStatus()) {
  50.             throw new \Exception(
  51.                 Translator::getInstance()->trans('This status is protected.')
  52.                 .' '.Translator::getInstance()->trans('You can not delete it.')
  53.             );
  54.         }
  55.         if (null !== OrderQuery::create()->findOneByStatusId($orderStatus->getId())) {
  56.             throw new \Exception(
  57.                 Translator::getInstance()->trans('Some commands use this status.')
  58.                 .' '.Translator::getInstance()->trans('You can not delete it.')
  59.             );
  60.         }
  61.         $orderStatus->delete();
  62.         $event->setOrderStatus($orderStatus);
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public static function getSubscribedEvents()
  68.     {
  69.         return [
  70.             TheliaEvents::ORDER_STATUS_CREATE => ['create'128],
  71.             TheliaEvents::ORDER_STATUS_UPDATE => ['update'128],
  72.             TheliaEvents::ORDER_STATUS_DELETE => ['delete'128],
  73.             TheliaEvents::ORDER_STATUS_UPDATE_POSITION => ['updatePosition'128],
  74.         ];
  75.     }
  76.     protected function createOrUpdate(OrderStatusEvent $eventOrderStatusModel $orderStatus): void
  77.     {
  78.         $orderStatus
  79.             ->setCode(!$orderStatus->getProtectedStatus() ? $event->getCode() : $orderStatus->getCode())
  80.             ->setColor($event->getColor())
  81.             // i18n
  82.             ->setLocale($event->getLocale())
  83.             ->setTitle($event->getTitle())
  84.             ->setDescription($event->getDescription())
  85.             ->setPostscriptum($event->getPostscriptum())
  86.             ->setChapo($event->getChapo());
  87.         if ($orderStatus->getId() === null) {
  88.             $orderStatus->setPosition(
  89.                 OrderStatusQuery::create()->orderByPosition(Criteria::DESC)->findOne()->getPosition() + 1
  90.             );
  91.         }
  92.         $orderStatus->save();
  93.         $event->setOrderStatus($orderStatus);
  94.     }
  95.     /**
  96.      * Changes position, selecting absolute ou relative change.
  97.      */
  98.     public function updatePosition(UpdatePositionEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  99.     {
  100.         $this->genericUpdatePosition(OrderStatusQuery::create(), $event$dispatcher);
  101.     }
  102.     /**
  103.      * @return OrderStatusModel
  104.      */
  105.     protected function getOrderStatus(OrderStatusUpdateEvent $event)
  106.     {
  107.         if (null === $orderStatus OrderStatusQuery::create()->findOneById($event->getId())) {
  108.             throw new \LogicException(
  109.                 'Order status not found'
  110.             );
  111.         }
  112.         return $orderStatus;
  113.     }
  114. }