core/lib/Thelia/Action/Address.php line 53

  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\Exception\PropelException;
  13. use Propel\Runtime\Propel;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Thelia\Core\Event\Address\AddressCreateOrUpdateEvent;
  17. use Thelia\Core\Event\TheliaEvents;
  18. use Thelia\Model\Address as AddressModel;
  19. use Thelia\Model\Event\AddressEvent;
  20. use Thelia\Model\Map\AddressTableMap;
  21. /**
  22.  * Class Address.
  23.  *
  24.  * @author Manuel Raynaud <manu@raynaud.io>
  25.  */
  26. class Address extends BaseAction implements EventSubscriberInterface
  27. {
  28.     public function create(AddressCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  29.     {
  30.         $address = new AddressModel();
  31.         $address->setCustomer($event->getCustomer());
  32.         $this->createOrUpdate($address$event$dispatcher);
  33.     }
  34.     public function update(AddressCreateOrUpdateEvent $event$eventNameEventDispatcherInterface $dispatcher): void
  35.     {
  36.         $addressModel $event->getAddress();
  37.         $this->createOrUpdate($addressModel$event$dispatcher);
  38.     }
  39.     public function delete(AddressEvent $event): void
  40.     {
  41.         $address $event->getModel();
  42.         $address->delete();
  43.     }
  44.     public function useDefault(AddressEvent $event): void
  45.     {
  46.         $address $event->getModel();
  47.         $address->makeItDefault();
  48.     }
  49.     protected function createOrUpdate(AddressModel $addressModelAddressCreateOrUpdateEvent $event$dispatcher): void
  50.     {
  51.         $con Propel::getWriteConnection(AddressTableMap::DATABASE_NAME);
  52.         $con->beginTransaction();
  53.         try {
  54.             $addressModel
  55.                 ->setLabel($event->getLabel())
  56.                 ->setTitleId($event->getTitle())
  57.                 ->setFirstname($event->getFirstname())
  58.                 ->setLastname($event->getLastname())
  59.                 ->setAddress1($event->getAddress1())
  60.                 ->setAddress2($event->getAddress2())
  61.                 ->setAddress3($event->getAddress3())
  62.                 ->setZipcode($event->getZipcode())
  63.                 ->setCity($event->getCity())
  64.                 ->setCountryId($event->getCountry())
  65.                 ->setStateId($event->getState())
  66.                 ->setCellphone($event->getCellphone())
  67.                 ->setPhone($event->getPhone())
  68.                 ->setCompany($event->getCompany())
  69.                 ->save()
  70.             ;
  71.             if ($event->getIsDefault() && !$addressModel->getIsDefault()) {
  72.                 $addressModel->makeItDefault();
  73.             }
  74.             $event->setAddress($addressModel);
  75.             $con->commit();
  76.         } catch (PropelException $e) {
  77.             $con->rollback();
  78.             throw $e;
  79.         }
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public static function getSubscribedEvents()
  85.     {
  86.         return [
  87.             TheliaEvents::ADDRESS_CREATE => ['create'128],
  88.             TheliaEvents::ADDRESS_UPDATE => ['update'128],
  89.             TheliaEvents::ADDRESS_DELETE => ['delete'128],
  90.             TheliaEvents::ADDRESS_DEFAULT => ['useDefault'128],
  91.         ];
  92.     }
  93. }