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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Mtf\Util\Generate\Fixture;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\ObjectManagerInterface;
use Magento\Eav\Model\Config;
use Magento\Framework\DB\Adapter\AdapterInterface;
/**
* Provider of fields from database.
*/
class FieldsProvider
{
/**
* EAV configuration.
*
* @var Config
*/
protected $eavConfig;
/**
* Resources and connections registry and factory.
*
* @var Resource
*/
protected $resource;
/**
* Magento connection.
*
* @var AdapterInterface
*/
protected $connection;
/**
* @constructor
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->eavConfig = $objectManager->create(\Magento\Eav\Model\Config::class);
$this->resource = $objectManager->create(\Magento\Framework\App\ResourceConnection::class);
}
/**
* Check connection to DB.
*
* @return bool
*/
public function checkConnection()
{
$this->connection = $this->getConnection('core');
if (!$this->connection || $this->connection instanceof \Zend_Db_Adapter_Exception) {
echo ('Connection to Magento 2 database is absent. Fixture data has not been fetched.' . PHP_EOL);
return false;
}
return true;
}
/**
* Collect fields for the entity based on its type.
*
* @param array $fixture
* @return array
*/
public function getFields(array $fixture)
{
$method = $fixture['type'] . 'CollectFields';
if (!method_exists($this, $method)) {
return [];
}
return $this->$method($fixture);
}
/**
* Collect fields for the entity with eav type.
*
* @param array $fixture
* @return array
*/
protected function eavCollectFields(array $fixture)
{
$entityType = $fixture['entity_type'];
$collection = $this->eavConfig->getEntityType($entityType)->getAttributeCollection();
$attributes = [];
foreach ($collection as $attribute) {
if (isset($fixture['product_type'])) {
$applyTo = $attribute->getApplyTo();
if (!empty($applyTo) && !in_array($fixture['product_type'], $applyTo)) {
continue;
}
}
/** @var $attribute \Magento\Eav\Model\Entity\Attribute */
$code = $attribute->getAttributeCode();
$attributes[$code] = [
'attribute_code' => $code,
'backend_type' => $attribute->getBackendType(),
'is_required' => $attribute->getIsRequired(),
'default_value' => $attribute->getDefaultValue(),
'input' => $attribute->getFrontendInput(),
];
}
return $attributes;
}
/**
* Collect fields for the entity with table type.
*
* @param array $fixture
* @return array
*/
protected function tableCollectFields(array $fixture)
{
return $this->flatCollectFields($fixture);
}
/**
* Collect fields for the entity with flat type.
*
* @param array $fixture
* @return array
*/
protected function flatCollectFields(array $fixture)
{
$entityType = $fixture['entity_type'];
/** @var $connection \Magento\Framework\DB\Adapter\AdapterInterface */
$fields = $this->connection->describeTable($entityType);
$attributes = [];
foreach ($fields as $code => $field) {
$attributes[$code] = [
'attribute_code' => $code,
'backend_type' => $field['DATA_TYPE'],
'is_required' => ($field['PRIMARY'] || $field['IDENTITY']),
'default_value' => $field['DEFAULT'],
'input' => '',
];
}
return $attributes;
}
/**
* Collect fields for the entity with composite type.
*
* @param array $fixture
* @return array
*/
protected function compositeCollectFields(array $fixture)
{
$entityTypes = $fixture['entities'];
$fields = [];
foreach ($entityTypes as $entityType) {
$fields = array_merge($fields, $this->connection->describeTable($entityType));
}
$attributes = [];
foreach ($fields as $code => $field) {
$attributes[$code] = [
'attribute_code' => $code,
'backend_type' => $field['DATA_TYPE'],
'is_required' => ($field['PRIMARY'] || $field['IDENTITY']),
'default_value' => $field['DEFAULT'],
'input' => '',
];
}
return $attributes;
}
/**
* Retrieve connection to resource specified by $resourceName.
*
* @param string $resourceName
* @return \Exception|false|\Magento\Framework\DB\Adapter\AdapterInterface
*/
protected function getConnection($resourceName)
{
try {
$connection = $this->resource->getConnection($resourceName);
return $connection;
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
return $e;
}
}
}