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

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