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

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