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