QuoteSetup.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\Eav\Model\Entity\Setup\Context;
  8. use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
  9. use Magento\Eav\Setup\EavSetup;
  10. use Magento\Framework\App\CacheInterface;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\Setup\ModuleDataSetupInterface;
  13. /**
  14. * Quote module setup class
  15. *
  16. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  17. * @codeCoverageIgnore
  18. */
  19. class QuoteSetup extends EavSetup
  20. {
  21. /**
  22. * @var ScopeConfigInterface
  23. */
  24. protected $_config;
  25. /**
  26. * @var \Magento\Framework\Encryption\EncryptorInterface
  27. */
  28. protected $_encryptor;
  29. /**
  30. * @var string
  31. */
  32. private static $connectionName = 'checkout';
  33. /**
  34. * @param ModuleDataSetupInterface $setup
  35. * @param Context $context
  36. * @param CacheInterface $cache
  37. * @param CollectionFactory $attrGroupCollectionFactory
  38. * @param ScopeConfigInterface $config
  39. */
  40. public function __construct(
  41. ModuleDataSetupInterface $setup,
  42. Context $context,
  43. CacheInterface $cache,
  44. CollectionFactory $attrGroupCollectionFactory,
  45. ScopeConfigInterface $config
  46. ) {
  47. $this->_config = $config;
  48. $this->_encryptor = $context->getEncryptor();
  49. parent::__construct($setup, $context, $cache, $attrGroupCollectionFactory);
  50. }
  51. /**
  52. * List of entities converted from EAV to flat data structure
  53. *
  54. * @var $_flatEntityTables array
  55. */
  56. protected $_flatEntityTables = [
  57. 'quote' => 'quote',
  58. 'quote_item' => 'quote_item',
  59. 'quote_address' => 'quote_address',
  60. 'quote_address_item' => 'quote_address_item',
  61. 'quote_address_rate' => 'quote_shipping_rate',
  62. 'quote_payment' => 'quote_payment',
  63. ];
  64. /**
  65. * Check if table exist for flat entity
  66. *
  67. * @param string $table
  68. * @return bool
  69. */
  70. protected function _flatTableExist($table)
  71. {
  72. $tablesList = $this->getConnection()->listTables();
  73. return in_array(
  74. strtoupper($this->getTable($table)),
  75. array_map('strtoupper', $tablesList)
  76. );
  77. }
  78. /**
  79. * Add entity attribute. Overwritten for flat entities support
  80. *
  81. * @param int|string $entityTypeId
  82. * @param string $code
  83. * @param array $attr
  84. * @return $this
  85. */
  86. public function addAttribute($entityTypeId, $code, array $attr)
  87. {
  88. if (isset(
  89. $this->_flatEntityTables[$entityTypeId]
  90. ) && $this->_flatTableExist(
  91. $this->_flatEntityTables[$entityTypeId]
  92. )
  93. ) {
  94. $this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
  95. } else {
  96. parent::addAttribute($entityTypeId, $code, $attr);
  97. }
  98. return $this;
  99. }
  100. /**
  101. * Add attribute as separate column in the table
  102. *
  103. * @param string $table
  104. * @param string $attribute
  105. * @param array $attr
  106. * @return $this
  107. */
  108. protected function _addFlatAttribute($table, $attribute, $attr)
  109. {
  110. $tableInfo = $this->getConnection()
  111. ->describeTable($this->getTable($table));
  112. if (isset($tableInfo[$attribute])) {
  113. return $this;
  114. }
  115. $columnDefinition = $this->_getAttributeColumnDefinition($attribute, $attr);
  116. $this->getConnection()->addColumn(
  117. $this->getTable($table),
  118. $attribute,
  119. $columnDefinition
  120. );
  121. return $this;
  122. }
  123. /**
  124. * Retrieve definition of column for create in flat table
  125. *
  126. * @param string $code
  127. * @param array $data
  128. * @return array
  129. * @SuppressWarnings(PHPMD.NPathComplexity)
  130. */
  131. protected function _getAttributeColumnDefinition($code, $data)
  132. {
  133. // Convert attribute type to column info
  134. $data['type'] = isset($data['type']) ? $data['type'] : 'varchar';
  135. $type = null;
  136. $length = null;
  137. switch ($data['type']) {
  138. case 'timestamp':
  139. $type = \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP;
  140. break;
  141. case 'datetime':
  142. $type = \Magento\Framework\DB\Ddl\Table::TYPE_DATETIME;
  143. break;
  144. case 'decimal':
  145. $type = \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL;
  146. $length = '12,4';
  147. break;
  148. case 'int':
  149. $type = \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER;
  150. break;
  151. case 'text':
  152. $type = \Magento\Framework\DB\Ddl\Table::TYPE_TEXT;
  153. $length = 65536;
  154. break;
  155. case 'char':
  156. case 'varchar':
  157. $type = \Magento\Framework\DB\Ddl\Table::TYPE_TEXT;
  158. $length = 255;
  159. break;
  160. }
  161. if ($type !== null) {
  162. $data['type'] = $type;
  163. $data['length'] = $length;
  164. }
  165. $data['nullable'] = isset($data['required']) ? !$data['required'] : true;
  166. $data['comment'] = isset($data['comment']) ? $data['comment'] : ucwords(str_replace('_', ' ', $code));
  167. return $data;
  168. }
  169. /**
  170. * Get config model
  171. *
  172. * @return ScopeConfigInterface
  173. */
  174. public function getConfigModel()
  175. {
  176. return $this->_config;
  177. }
  178. /**
  179. * @return \Magento\Framework\Encryption\EncryptorInterface
  180. */
  181. public function getEncryptor()
  182. {
  183. return $this->_encryptor;
  184. }
  185. /**
  186. * Get quote connection
  187. *
  188. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  189. */
  190. public function getConnection()
  191. {
  192. return $this->getSetup()->getConnection(self::$connectionName);
  193. }
  194. /**
  195. * Get table name
  196. *
  197. * @param string $table
  198. * @return string
  199. */
  200. public function getTable($table)
  201. {
  202. return $this->getSetup()->getTable($table, self::$connectionName);
  203. }
  204. }