vendor/shopware/core/System/Country/CountryTaxFreeDeprecationUpdater.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Country;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Doctrine\RetryableQuery;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\Framework\Feature;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be remove on version 6.5.0
  11.  */
  12. class CountryTaxFreeDeprecationUpdater implements EventSubscriberInterface
  13. {
  14.     private bool $blueGreenEnabled;
  15.     private Connection $connection;
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(bool $blueGreenEnabledConnection $connection)
  20.     {
  21.         $this->blueGreenEnabled $blueGreenEnabled;
  22.         $this->connection $connection;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         if (Feature::isActive('v6.5.0.0')) {
  27.             return [];
  28.         }
  29.         return [
  30.             CountryEvents::COUNTRY_WRITTEN_EVENT => 'updated',
  31.         ];
  32.     }
  33.     public function updated(EntityWrittenEvent $event): void
  34.     {
  35.         if ($this->blueGreenEnabled) {
  36.             return;
  37.         }
  38.         $taxFreePort = [];
  39.         $companyTaxFreePort = [];
  40.         $taxFreeBackport = [];
  41.         $companyTaxFreeBackport = [];
  42.         foreach ($event->getPayloads() as $payload) {
  43.             if (\array_key_exists('customerTax'$payload)) {
  44.                 $taxFreeBackport[] = $payload['id'];
  45.             } elseif (\array_key_exists('taxFree'$payload)) {
  46.                 $taxFreePort[] = $payload['id'];
  47.             }
  48.             if (\array_key_exists('companyTax'$payload)) {
  49.                 $companyTaxFreeBackport[] = $payload['id'];
  50.             } elseif (\array_key_exists('companyTaxFree'$payload)) {
  51.                 $companyTaxFreePort[] = $payload['id'];
  52.             }
  53.         }
  54.         $this->port($taxFreePortCountryDefinition::TYPE_CUSTOMER_TAX_FREE);
  55.         $this->port($companyTaxFreePortCountryDefinition::TYPE_COMPANY_TAX_FREE);
  56.         $this->backport($taxFreeBackportCountryDefinition::TYPE_CUSTOMER_TAX_FREE);
  57.         $this->backport($companyTaxFreeBackportCountryDefinition::TYPE_COMPANY_TAX_FREE);
  58.     }
  59.     private function port(array $idsstring $taxFreeType): void
  60.     {
  61.         $ids array_unique(array_filter($ids));
  62.         if (empty($ids)) {
  63.             return;
  64.         }
  65.         $countries $this->connection->fetchAllAssociative(
  66.             'SELECT id, tax_free, company_tax_free, customer_tax, company_tax FROM country WHERE id IN (:ids)',
  67.             ['ids' => Uuid::fromHexToBytesList($ids)],
  68.             ['ids' => Connection::PARAM_STR_ARRAY]
  69.         );
  70.         if ($taxFreeType === CountryDefinition::TYPE_CUSTOMER_TAX_FREE) {
  71.             $query 'UPDATE `country`
  72.                     SET `customer_tax` = JSON_OBJECT("enabled", :isTaxFree, "currencyId", :currencyId, "amount", :amount)
  73.                     WHERE id = :countryId;';
  74.         } else {
  75.             $query 'UPDATE `country`
  76.                     SET `company_tax` = JSON_OBJECT("enabled", :isTaxFree, "currencyId", :currencyId, "amount", :amount)
  77.                     WHERE id = :countryId;';
  78.         }
  79.         $update = new RetryableQuery($this->connection$this->connection->prepare($query));
  80.         foreach ($countries as $country) {
  81.             if ($taxFreeType === CountryDefinition::TYPE_CUSTOMER_TAX_FREE) {
  82.                 $tax json_decode($country['customer_tax'], true);
  83.                 $isTaxFree $country['tax_free'];
  84.             } else {
  85.                 $tax json_decode($country['company_tax'], true);
  86.                 $isTaxFree $country['company_tax_free'];
  87.             }
  88.             if ((bool) $isTaxFree === (bool) $tax['enabled']) {
  89.                 continue;
  90.             }
  91.             $update->execute([
  92.                 'countryId' => $country['id'],
  93.                 'isTaxFree' => $isTaxFree,
  94.                 'currencyId' => $tax['currencyId'],
  95.                 'amount' => $tax['amount'],
  96.             ]);
  97.         }
  98.     }
  99.     private function backport(array $idsstring $taxFreeType): void
  100.     {
  101.         $ids array_unique(array_filter($ids));
  102.         if (empty($ids)) {
  103.             return;
  104.         }
  105.         $countries $this->connection->fetchAllAssociative(
  106.             'SELECT id, tax_free, company_tax_free, customer_tax, company_tax FROM country WHERE id IN (:ids)',
  107.             ['ids' => Uuid::fromHexToBytesList($ids)],
  108.             ['ids' => Connection::PARAM_STR_ARRAY]
  109.         );
  110.         if ($taxFreeType === CountryDefinition::TYPE_CUSTOMER_TAX_FREE) {
  111.             $query 'UPDATE `country` SET `tax_free` = :isTaxFree WHERE id = :countryId;';
  112.         } else {
  113.             $query 'UPDATE `country` SET `company_tax_free` = :isTaxFree WHERE id = :countryId;';
  114.         }
  115.         $update = new RetryableQuery($this->connection$this->connection->prepare($query));
  116.         foreach ($countries as $country) {
  117.             if ($taxFreeType === CountryDefinition::TYPE_CUSTOMER_TAX_FREE) {
  118.                 $tax json_decode($country['customer_tax'], true);
  119.                 $isTaxFree $country['tax_free'];
  120.             } else {
  121.                 $tax json_decode($country['company_tax'], true);
  122.                 $isTaxFree $country['company_tax_free'];
  123.             }
  124.             if ((bool) $isTaxFree === (bool) $tax['enabled']) {
  125.                 continue;
  126.             }
  127.             $update->execute([
  128.                 'countryId' => $country['id'],
  129.                 'isTaxFree' => $tax['enabled'],
  130.             ]);
  131.         }
  132.     }
  133. }