local/modules/TheliaMigrateCountry/EventListeners/MigrateCountryListener.php line 35

  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 TheliaMigrateCountry\EventListeners;
  12. use Propel\Runtime\Exception\PropelException;
  13. use Propel\Runtime\Propel;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Thelia\Model\AddressQuery;
  16. use Thelia\Model\Base\CountryQuery;
  17. use Thelia\Model\CountryAreaQuery;
  18. use Thelia\Model\Map\AddressTableMap;
  19. use Thelia\Model\Map\CountryAreaTableMap;
  20. use Thelia\Model\Map\TaxRuleCountryTableMap;
  21. use Thelia\Model\TaxRuleCountryQuery;
  22. use TheliaMigrateCountry\Events\MigrateCountryEvent;
  23. use TheliaMigrateCountry\Events\MigrateCountryEvents;
  24. /**
  25.  * Class MigrateCountryListener.
  26.  *
  27.  * @author Julien Chanséaume <julien@thelia.net>
  28.  */
  29. class MigrateCountryListener implements EventSubscriberInterface
  30. {
  31.     public function migrateCountry(MigrateCountryEvent $event): void
  32.     {
  33.         $counter = [];
  34.         // update address
  35.         $counter[AddressTableMap::TABLE_NAME] = $this->migrateAddress($event);
  36.         // tax rules
  37.         $counter[TaxRuleCountryTableMap::TABLE_NAME] = $this->migrateAddress($event);
  38.         // shipping zone
  39.         $counter[CountryAreaTableMap::TABLE_NAME] = $this->migrateAddress($event);
  40.         // if it succeeds we toggle the visibility of old country and new
  41.         $this->setCountriesVisibility($event);
  42.         $event->setCounter($counter);
  43.     }
  44.     protected function migrateAddress(MigrateCountryEvent $event)
  45.     {
  46.         $con Propel::getWriteConnection(AddressTableMap::DATABASE_NAME);
  47.         $con->beginTransaction();
  48.         try {
  49.             $updatedRows AddressQuery::create()
  50.                 ->filterByCountryId($event->getCountry())
  51.                 ->update(
  52.                     [
  53.                         'CountryId' => $event->getNewCountry(),
  54.                         'StateId' => $event->getNewState(),
  55.                     ]
  56.                 );
  57.             $con->commit();
  58.             return $updatedRows;
  59.         } catch (PropelException $e) {
  60.             $con->rollback();
  61.             throw $e;
  62.         }
  63.     }
  64.     protected function migrateTaxRules(MigrateCountryEvent $event)
  65.     {
  66.         $con Propel::getWriteConnection(TaxRuleCountryTableMap::DATABASE_NAME);
  67.         $con->beginTransaction();
  68.         try {
  69.             $updatedRows TaxRuleCountryQuery::create()
  70.                 ->filterByCountryId($event->getCountry())
  71.                 ->update(
  72.                     [
  73.                         'CountryId' => $event->getNewCountry(),
  74.                         'StateId' => $event->getNewState(),
  75.                     ]
  76.                 );
  77.             $con->commit();
  78.             return $updatedRows;
  79.         } catch (PropelException $e) {
  80.             $con->rollback();
  81.             throw $e;
  82.         }
  83.     }
  84.     protected function migrateShippingZones(MigrateCountryEvent $event)
  85.     {
  86.         $con Propel::getWriteConnection(CountryAreaTableMap::DATABASE_NAME);
  87.         $con->beginTransaction();
  88.         try {
  89.             $updatedRows CountryAreaQuery::create()
  90.                 ->filterByCountryId($event->getCountry())
  91.                 ->update(
  92.                     [
  93.                         'CountryId' => $event->getNewCountry(),
  94.                         'StateId' => $event->getNewState(),
  95.                     ]
  96.                 );
  97.             $con->commit();
  98.             return $updatedRows;
  99.         } catch (PropelException $e) {
  100.             $con->rollback();
  101.             throw $e;
  102.         }
  103.     }
  104.     private function setCountriesVisibility(MigrateCountryEvent $event): void
  105.     {
  106.         $oldCountry CountryQuery::create()->findPk($event->getCountry());
  107.         if (null !== $oldCountry) {
  108.             $oldCountry
  109.                 ->setVisible(0)
  110.                 ->save()
  111.             ;
  112.         }
  113.         $newCountry CountryQuery::create()->findPk($event->getNewCountry());
  114.         if (null !== $newCountry) {
  115.             $newCountry
  116.                 ->setVisible(1)
  117.                 ->save()
  118.             ;
  119.         }
  120.     }
  121.     /**
  122.      * {@inheritdoc}
  123.      */
  124.     public static function getSubscribedEvents()
  125.     {
  126.         return [
  127.             MigrateCountryEvents::MIGRATE_COUNTRY => 'migrateCountry',
  128.         ];
  129.     }
  130. }