src/EventSubscriber/TaxClassEventSubscriber.php line 136

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Ecommerce\Model\AbstractTaxClass;
  4. use Pimcore\Event\DataObjectEvents;
  5. use Pimcore\Event\Model\DataObjectEvent;
  6. use Pimcore\Model\DataObject\OnlineShopTaxClass;
  7. use Pimcore\Model\DataObject\Service;
  8. use App\Classes\Tools\ForceInheritance;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class TaxClassEventSubscriber implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents()
  13.     {
  14.         return [
  15.             DataObjectEvents::PRE_ADD => [
  16.                 ['addPrefixToNewEntry'10]
  17.             ],
  18.             DataObjectEvents::PRE_UPDATE => [
  19.                 ['updateKey'10],
  20.                 ['updatePath'8],
  21.                 ['updateAttributes'6],
  22.                 ['calculateZipCodes'4],
  23.                 ['validate'2]
  24.             ]
  25.         ];
  26.     }
  27.     private function isResponsibleFor(DataObjectEvent $event): bool
  28.     {
  29.         if ($event->getObject() instanceof AbstractTaxClass) {
  30.             return true;
  31.         }
  32.         return false;
  33.     }
  34.     protected function isCountryObjectOnly(OnlineShopTaxClass $tax)
  35.     {
  36.         $state ForceInheritance::run(function () use ($tax) {
  37.             return strtoupper($tax->getState());
  38.         });
  39.         if (!empty($state) && $state != '*') {
  40.             return false;
  41.         }
  42.         if (!empty($tax->getZipCode()) && $this->trimZipCodes($tax->getZipCode()) != '*') {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.     protected function isStateObjectOnly(OnlineShopTaxClass $tax)
  48.     {
  49.         $state ForceInheritance::run(function () use ($tax) {
  50.             return strtoupper($tax->getState());
  51.         });
  52.         if (empty($state)) {
  53.             return false;
  54.         }
  55.         if (!empty($tax->getZipCode()) && $this->trimZipCodes($tax->getZipCode()) != '*') {
  56.             return false;
  57.         }
  58.         return true;
  59.     }
  60.     protected function isTaxOverideObjectOnly(OnlineShopTaxClass $tax)
  61.     {
  62.         if (empty($tax->getZipCode()) && $this->trimZipCodes($tax->getZipCode()) != '*') {
  63.             return false;
  64.         }
  65.         return true;
  66.     }
  67.     public function addPrefixToNewEntry(DataObjectEvent $event)
  68.     {
  69.         if (!$this->isResponsibleFor($event)) {
  70.             return;
  71.         }
  72.         return;
  73.         /** @var OnlineShopTaxClass */
  74.         $object $event->getObject();
  75.         $object->setKey('__'.$object->getKey());
  76.     }
  77.     public function updateKey(DataObjectEvent $event)
  78.     {
  79.         if (!$this->isResponsibleFor($event)) {
  80.             return;
  81.         }
  82.         /** @var OnlineShopTaxClass */
  83.         $object $event->getObject();
  84.         $inheritedCountryCode ForceInheritance::run(function () use ($object) {
  85.             return strtoupper($object->getCountryCode());
  86.         });
  87.         $inheritedStateCode ForceInheritance::run(function () use ($object) {
  88.             return strtoupper($object->getState());
  89.         });
  90.         $key $object->getKey();
  91.         if ($object->isPublished() && $this->isCountryObjectOnly($object)) {
  92.             $key ForceInheritance::run(function () use ($object) {
  93.                 return strtoupper($object->getCountryCode());
  94.             });
  95.         } else if ($object->isPublished() && $this->isStateObjectOnly($object)) {
  96.             if (empty($inheritedCountryCode)) {
  97.                 throw new \Exception("You must select country code!");
  98.             }
  99.             $key ForceInheritance::run(function () use ($object) {
  100.                 return strtoupper($object->getState());
  101.             });
  102.         } else if ($object->isPublished()){
  103.             if (empty($inheritedStateCode)) {
  104.                 throw new \Exception("You must select state/region!");
  105.             }
  106.         }
  107.         $object->setKey($key);
  108.     }
  109.     public function updatePath(DataObjectEvent $event)
  110.     {
  111.         if (!$this->isResponsibleFor($event)) {
  112.             return;
  113.         }
  114.         /** @var OnlineShopTaxClass */
  115.         $object $event->getObject();
  116.         $countryCode ForceInheritance::run(function () use ($object) {
  117.             return strtoupper($object->getCountryCode());
  118.         });
  119.         $stateCode ForceInheritance::run(function () use ($object) {
  120.             return strtoupper($object->getState());
  121.         });
  122.         $combinationMode ForceInheritance::run(function () use ($object) {
  123.             return $object->getTaxEntryCombinationType();
  124.         });
  125.         $applicationType ForceInheritance::run(function () use ($object) {
  126.             return $object->getApplyToAddressType();
  127.         });
  128.         $parent Service::createFolderByPath(AbstractTaxClass::FOLDER_PATH);
  129.         $options = [
  130.             'combination_mode' => $combinationMode,
  131.             'application_type' => $applicationType
  132.         ];
  133.         if ($object->isPublished() && $this->isStateObjectOnly($object)) {
  134.             $parent OnlineShopTaxClass::getByPath(AbstractTaxClass::FOLDER_PATH.$countryCode);
  135.             if (!$parent) {
  136.                 $parent $this->createTax($countryCodenull$options);
  137.                 // Leave empty so he would inherit this value from parent
  138.                 // This should apply only once, during creation tree, so this is reason why we put this here
  139.                 $object->setTaxEntryCombinationType('');
  140.                 $object->setApplyToAddressType('');
  141.             }
  142.         }
  143.         if ($object->isPublished() && $this->isTaxOverideObjectOnly($object)) {
  144.             $parent OnlineShopTaxClass::getByPath(AbstractTaxClass::FOLDER_PATH.$countryCode.'/'.$stateCode);
  145.             if (!$parent) {
  146.                 $parent $this->createTax($countryCode$stateCode$options);
  147.                 // Leave empty so he would inherit this value from parent
  148.                 // This should apply only once, during creation tree, so this is reason why we put this here
  149.                 $object->setTaxEntryCombinationType('');
  150.                 $object->setApplyToAddressType('');
  151.             }
  152.         }
  153.         $object->setParent($parent);
  154.     }
  155.     public function updateAttributes(DataObjectEvent $event)
  156.     {
  157.         if (!$this->isResponsibleFor($event)) {
  158.             return;
  159.         }
  160.         $object $event->getObject();
  161.         if (!$object->isPublished()) {
  162.             return;
  163.         }
  164.         if (!empty($object->getZipCode())) {
  165.             $object->setCountry("");
  166.             $object->setState("");
  167.         } else if (!empty($object->getState())) {
  168.             $object->setCountry("");
  169.         } else if (!empty($object->getCountry())) {
  170.         }
  171.     }
  172.     public function calculateZipCodes(DataObjectEvent $event)
  173.     {
  174.         if (!$this->isResponsibleFor($event)) {
  175.             return;
  176.         }
  177.         /** @var OnlineShopTaxClass */
  178.         $object $event->getObject();
  179.         $cleanZipcode $this->trimZipCodes($object->getZipCode() ?? '');
  180.         if (!$this->isPatern($cleanZipcode)) {
  181.             $object->setZipCodeValues(','.$cleanZipcode.',');
  182.         }
  183.     }
  184.     public function validate(DataObjectEvent $event)
  185.     {
  186.         if (!$this->isResponsibleFor($event)) {
  187.             return;
  188.         }
  189.         /** @var OnlineShopTaxClass */
  190.         $object $event->getObject();
  191.         if (!$object->isPublished()) {
  192.             return;
  193.         }
  194.         // Nothing for now
  195.     }
  196.     protected function isPatern(string $value)
  197.     {
  198.         return preg_match_all('/^[A-Za-z0-9,]$/'$value$matches);
  199.     }
  200.     protected function trimZipCodes(?string $zipCodes)
  201.     {
  202.         $zipCodes str_replace(' '''$zipCodes ?? '');
  203.         return trim($zipCodes" ,\t\n\r\0\x0B");
  204.     }
  205.     protected function createTax(string $countryCode, ?string $stateCode null, array $options = [])
  206.     {
  207.         $object = new OnlineShopTaxClass();
  208.         // $object->setTaxEntryCombinationType(OnlineShopTaxClass::COMBINATION_MODE_COMBINE);
  209.         $object->setTaxEntryCombinationType($options['combination_mode']);
  210.         $object->setApplyToAddressType($options['application_type']);
  211.         $object->setCountry($countryCode);
  212.         $object->setPublished(true);
  213.         $object->setKey($countryCode);
  214.         $path OnlineShopTaxClass::FOLDER_PATH;
  215.         if ($stateCode) {
  216.             $path OnlineShopTaxClass::FOLDER_PATH.strtoupper($countryCode).'/';
  217.             // Leave empty so he would inherit this value from parent
  218.             $object->setCountry("");
  219.             $object->setState($stateCode);
  220.             $object->setKey($stateCode);
  221.         }
  222.         $parent null;
  223.         if ($stateCode) {
  224.             $parent OnlineShopTaxClass::getByPath($path);
  225.             if (!$parent) {
  226.                 $parent $this->createTax($countryCodenull$options);
  227.                 // Leave empty so he would inherit this value from parent
  228.                 $object->setTaxEntryCombinationType('');
  229.                 $object->setApplyToAddressType('');
  230.             }
  231.         } else {
  232.             $parent Service::createFolderByPath(AbstractTaxClass::FOLDER_PATH);
  233.         }
  234.         $object->setParent($parent);
  235.         // dd($object->getKey(), $object->getPath(), '-----', $parent->getPath(), $parent->getKey());
  236.         $object->save();
  237.         return $object;
  238.     }
  239. }