1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magefix\Fixture\Builder; |
4
|
|
|
|
5
|
|
|
use Mage_Core_Model_Abstract as MagentoModel; |
6
|
|
|
use Magefix\Exceptions\UndefinedDataProvider; |
7
|
|
|
use Magefix\Fixture\DataIterator; |
8
|
|
|
use Magefix\Fixture\Builder; |
9
|
|
|
use Magefix\Fixture\Factory\Builder as FixtureBuilder; |
10
|
|
|
use Magefix\Fixtures\Data\Provider; |
11
|
|
|
use Magefix\Magento\Store\Scope as MagentoStoreScope; |
12
|
|
|
use Magefix\Exceptions\UndefinedAttributes; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AbstractBuilder |
16
|
|
|
* |
17
|
|
|
* @package Magefix\Fixture\Builder |
18
|
|
|
* @author Carlo Tasca <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractBuilder implements Builder |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var MagentoModel |
24
|
|
|
*/ |
25
|
|
|
private $_mageModel; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Provider |
29
|
|
|
*/ |
30
|
|
|
private $_dataProvider; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $_hook; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $_data; |
41
|
|
|
|
42
|
|
|
public function __construct(array $parserData, MagentoModel $model, $hook) |
43
|
|
|
{ |
44
|
|
|
$this->_mageModel = $model; |
45
|
|
|
$this->_data = $parserData; |
46
|
|
|
$this->_hook = $hook; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
* Concrete classes to provide implementation for build method |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
abstract public function build(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return MagentoModel |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function getMageModel() |
62
|
|
|
{ |
63
|
|
|
return $this->_getMageModel(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getHook() |
70
|
|
|
{ |
71
|
|
|
return $this->_getHook(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Iterate fixture data |
76
|
|
|
*/ |
77
|
|
|
public function invokeProvidersMethods() |
78
|
|
|
{ |
79
|
|
|
$iterator = new DataIterator($this->_data); |
80
|
|
|
$this->_data = $iterator->apply($this->_getDataProvider()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $mageModel |
85
|
|
|
* @param $data |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function saveFixtureWithModelAndData($mageModel, $data) |
89
|
|
|
{ |
90
|
|
|
$this->_saveFixtureWithModelAndData($mageModel, $data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $entity |
95
|
|
|
* |
96
|
|
|
* @throws UndefinedDataProvider |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
public function throwUndefinedDataProvider($entity) |
100
|
|
|
{ |
101
|
|
|
$this->_throwUndefinedDataProvider($entity); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function _build() |
105
|
|
|
{ |
106
|
|
|
$this->invokeProvidersMethods(); |
107
|
|
|
$defaultData = $this->_getMageModelData() ? $this->_getMageModelData() : []; |
108
|
|
|
$mergedData = array_merge($defaultData, $this->_getFixtureAttributes()); |
109
|
|
|
|
110
|
|
|
$this->_getMageModel()->setData($mergedData); |
111
|
|
|
|
112
|
|
|
return $this->_saveFixture(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
* @throws \Exception |
118
|
|
|
*/ |
119
|
|
|
protected function _getMageModelData() |
120
|
|
|
{ |
121
|
|
|
return $this->_getMageModel()->getData(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return MagentoModel |
126
|
|
|
*/ |
127
|
|
|
protected function _getMageModel() |
128
|
|
|
{ |
129
|
|
|
return $this->_mageModel; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
protected function _getDataProvider() |
136
|
|
|
{ |
137
|
|
|
$this->_throwUndefinedDataProvider($this->_data['fixture']); |
138
|
|
|
|
139
|
|
|
if (is_null($this->_dataProvider)) { |
140
|
|
|
$dataProvider = new \ReflectionClass($this->_data['fixture']['data_provider']); |
141
|
|
|
$this->_dataProvider = $dataProvider->newInstance(); |
142
|
|
|
} |
143
|
|
|
return $this->_dataProvider; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
protected function _getHook() |
150
|
|
|
{ |
151
|
|
|
return $this->_hook; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return array |
156
|
|
|
* @throws \Exception |
157
|
|
|
*/ |
158
|
|
|
protected function _getFixtureAttributes() |
159
|
|
|
{ |
160
|
|
|
$this->_throwUndefinedAttributesException(isset($this->_data['fixture']['attributes'])); |
161
|
|
|
|
162
|
|
|
return $this->_data['fixture']['attributes']; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return int |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
protected function _saveFixture() |
170
|
|
|
{ |
171
|
|
|
MagentoStoreScope::setAdminStoreScope(); |
172
|
|
|
$this->_getMageModel()->save(); |
173
|
|
|
MagentoStoreScope::setCurrentStoreScope(); |
174
|
|
|
|
175
|
|
|
return $this->_getMageModel()->getId(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param MagentoModel $mageModel |
180
|
|
|
* @param $data |
181
|
|
|
* @return mixed |
182
|
|
|
* @throws \Exception |
183
|
|
|
*/ |
184
|
|
|
protected function _saveFixtureWithModelAndData(MagentoModel $mageModel, $data) |
185
|
|
|
{ |
186
|
|
|
MagentoStoreScope::setAdminStoreScope(); |
187
|
|
|
$mageModel->setData($data); |
188
|
|
|
$mageModel->save(); |
189
|
|
|
MagentoStoreScope::setCurrentStoreScope(); |
190
|
|
|
|
191
|
|
|
return $mageModel->getId(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Add data to model, but don't invoke save |
196
|
|
|
* |
197
|
|
|
* @param array $data |
198
|
|
|
*/ |
199
|
|
|
protected function _initFixtureWithData(array $data) |
200
|
|
|
{ |
201
|
|
|
$this->_getMageModel()->setData($data); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param $node |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
protected function _buildManySimple($node) |
210
|
|
|
{ |
211
|
|
|
$products = $this->_data['fixture'][$node]['products']; |
212
|
|
|
|
213
|
|
|
return FixtureBuilder::buildMany( |
214
|
|
|
FixtureBuilder::SIMPLE_PRODUCT_FIXTURE_TYPE, $this, $products, $this->getHook() |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param boolean $isSet |
220
|
|
|
* @param string $message |
221
|
|
|
* |
222
|
|
|
* @throws UndefinedAttributes |
223
|
|
|
* |
224
|
|
|
*/ |
225
|
|
|
protected function _throwUndefinedAttributesException( |
226
|
|
|
$isSet, $message = 'Fixture attributes have not been defined. Check fixture yml file.' |
227
|
|
|
) |
228
|
|
|
{ |
229
|
|
|
if (!$isSet) { |
230
|
|
|
throw new UndefinedAttributes($message); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param array $a |
236
|
|
|
* |
237
|
|
|
* @throws UndefinedDataProvider |
238
|
|
|
* |
239
|
|
|
*/ |
240
|
|
|
protected function _throwUndefinedDataProvider(array $a) |
241
|
|
|
{ |
242
|
|
|
if (!isset($a['data_provider'])) { |
243
|
|
|
throw new UndefinedDataProvider( |
244
|
|
|
'Fixture has no data provider specified. Check fixture yml file.' |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|