ConvertSerializedDataToJson.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Setup;
  7. use Magento\Framework\DB\AggregatedFieldDataConverter;
  8. use Magento\Framework\DB\DataConverter\SerializedToJson;
  9. use Magento\Framework\DB\FieldToConvert;
  10. use Magento\Framework\DB\Select\QueryModifierFactory;
  11. use Magento\Framework\DB\Query\Generator;
  12. /**
  13. * Convert serialized data in quote tables to JSON
  14. */
  15. class ConvertSerializedDataToJson
  16. {
  17. /**
  18. * @var QuoteSetup
  19. */
  20. private $quoteSetup;
  21. /**
  22. * @var QueryModifierFactory
  23. */
  24. private $queryModifierFactory;
  25. /**
  26. * @var Generator
  27. */
  28. private $queryGenerator;
  29. /**
  30. * @var AggregatedFieldDataConverter
  31. */
  32. private $aggregatedFieldConverter;
  33. /**
  34. * Constructor
  35. *
  36. * @param QuoteSetup $quoteSetup
  37. * @param AggregatedFieldDataConverter $aggregatedFieldConverter
  38. * @param QueryModifierFactory $queryModifierFactory
  39. * @param Generator $queryGenerator
  40. */
  41. public function __construct(
  42. QuoteSetup $quoteSetup,
  43. AggregatedFieldDataConverter $aggregatedFieldConverter,
  44. QueryModifierFactory $queryModifierFactory,
  45. Generator $queryGenerator
  46. ) {
  47. $this->quoteSetup = $quoteSetup;
  48. $this->aggregatedFieldConverter = $aggregatedFieldConverter;
  49. $this->queryModifierFactory = $queryModifierFactory;
  50. $this->queryGenerator = $queryGenerator;
  51. }
  52. /**
  53. * Convert data for additional_information field in quote_payment table from serialized
  54. * to JSON format
  55. *
  56. * @return void
  57. * @throws \InvalidArgumentException
  58. */
  59. public function convert()
  60. {
  61. $queryModifier = $this->queryModifierFactory->create(
  62. 'in',
  63. [
  64. 'values' => [
  65. 'code' => [
  66. 'parameters',
  67. 'info_buyRequest',
  68. 'attributes',
  69. 'bundle_option_ids',
  70. 'bundle_selection_ids',
  71. 'bundle_selection_attributes',
  72. ]
  73. ]
  74. ]
  75. );
  76. $this->aggregatedFieldConverter->convert(
  77. [
  78. new FieldToConvert(
  79. SerializedToJson::class,
  80. $this->quoteSetup->getTable('quote_payment'),
  81. 'payment_id',
  82. 'additional_information'
  83. ),
  84. new FieldToConvert(
  85. SerializedToJson::class,
  86. $this->quoteSetup->getTable('quote_payment'),
  87. 'payment_id',
  88. 'additional_data'
  89. ),
  90. new FieldToConvert(
  91. SerializedToJson::class,
  92. $this->quoteSetup->getTable('quote_address'),
  93. 'address_id',
  94. 'applied_taxes'
  95. ),
  96. new FieldToConvert(
  97. SerializedToJson::class,
  98. $this->quoteSetup->getTable('quote_item_option'),
  99. 'option_id',
  100. 'value',
  101. $queryModifier
  102. ),
  103. ],
  104. $this->quoteSetup->getConnection()
  105. );
  106. $select = $this->quoteSetup->getSetup()
  107. ->getConnection()
  108. ->select()
  109. ->from(
  110. $this->quoteSetup->getSetup()
  111. ->getTable('catalog_product_option'),
  112. ['option_id']
  113. )
  114. ->where('type = ?', 'file');
  115. $iterator = $this->queryGenerator->generate('option_id', $select);
  116. foreach ($iterator as $selectByRange) {
  117. $codes = $this->quoteSetup->getSetup()
  118. ->getConnection()
  119. ->fetchCol($selectByRange);
  120. $codes = array_map(
  121. function ($id) {
  122. return 'option_' . $id;
  123. },
  124. $codes
  125. );
  126. $queryModifier = $this->queryModifierFactory->create(
  127. 'in',
  128. [
  129. 'values' => [
  130. 'code' => $codes
  131. ]
  132. ]
  133. );
  134. $this->aggregatedFieldConverter->convert(
  135. [
  136. new FieldToConvert(
  137. SerializedToJson::class,
  138. $this->quoteSetup->getTable('quote_item_option'),
  139. 'option_id',
  140. 'value',
  141. $queryModifier
  142. ),
  143. ],
  144. $this->quoteSetup->getConnection()
  145. );
  146. }
  147. }
  148. }