core/lib/Thelia/Action/CustomerTitle.php line 41

  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\Propel;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Thelia\Core\Event\CustomerTitle\CustomerTitleEvent;
  15. use Thelia\Core\Event\TheliaEvents;
  16. use Thelia\Model\CustomerTitle as CustomerTitleModel;
  17. use Thelia\Model\Map\CustomerTitleTableMap;
  18. /**
  19.  * Class CustomerTitle.
  20.  *
  21.  * @author Benjamin Perche <bperche@openstudio.fr>
  22.  */
  23. class CustomerTitle extends BaseAction implements EventSubscriberInterface
  24. {
  25.     public function create(CustomerTitleEvent $event): void
  26.     {
  27.         $this->createOrUpdate($event, new CustomerTitleModel());
  28.     }
  29.     public function update(CustomerTitleEvent $event): void
  30.     {
  31.         $this->checkCustomerTitle($event);
  32.         $this->createOrUpdate($event$event->getCustomerTitle());
  33.     }
  34.     public function delete(CustomerTitleEvent $event): void
  35.     {
  36.         $this->checkCustomerTitle($event);
  37.         $con Propel::getConnection(CustomerTitleTableMap::DATABASE_NAME);
  38.         $con->beginTransaction();
  39.         try {
  40.             $event->getCustomerTitle()->delete();
  41.             $con->commit();
  42.         } catch (\Exception $e) {
  43.             $con->rollBack();
  44.             throw $e;
  45.         }
  46.         $event->setCustomerTitle(null);
  47.     }
  48.     protected function checkCustomerTitle(CustomerTitleEvent $event): void
  49.     {
  50.         if (null === $event->getCustomerTitle()) {
  51.             throw new \LogicException(
  52.                 'You must set the customer title before its update'
  53.             );
  54.         }
  55.     }
  56.     protected function createOrUpdate(CustomerTitleEvent $eventCustomerTitleModel $customerTitle): void
  57.     {
  58.         $con Propel::getConnection(CustomerTitleTableMap::DATABASE_NAME);
  59.         $con->beginTransaction();
  60.         $i18n $customerTitle->getTranslation($event->getLocale(), $con);
  61.         try {
  62.             $i18n
  63.                 ->setShort($event->getShort())
  64.                 ->setLong($event->getLong())
  65.             ;
  66.             $customerTitle->save($con);
  67.             if ($event->isDefault()) {
  68.                 $customerTitle->toggleDefault($con);
  69.                 $event->setDefault(false);
  70.             }
  71.             $con->commit();
  72.         } catch (\Exception $e) {
  73.             $con->rollBack();
  74.             throw $e;
  75.         }
  76.         $event->setCustomerTitle($customerTitle);
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public static function getSubscribedEvents()
  82.     {
  83.         return [
  84.             TheliaEvents::CUSTOMER_TITLE_CREATE => ['create'],
  85.             TheliaEvents::CUSTOMER_TITLE_UPDATE => ['update'],
  86.             TheliaEvents::CUSTOMER_TITLE_DELETE => ['delete'],
  87.         ];
  88.     }
  89. }