Completed
Push — develop ( 1f8c39...fd8d97 )
by Carlo
03:09
created

AbstractBuilder::processFixtureAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
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\Builder;
8
use Magefix\Fixture\Factory\Builder as FixtureBuilder;
9
use Magefix\Fixtures\Data\Provider;
10
use Magefix\Magento\Store\Scope as MagentoStoreScope;
11
use Magefix\Exceptions\ProviderMethodNotFound;
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
     * @var Provider
28
     */
29
    private $_dataProvider;
30
31
    /**
32
     * @var string
33
     */
34
    private $_hook;
35
36
    /**
37
     * @var array
38
     */
39
    protected $_data;
40
41
    public function __construct(array $parserData, MagentoModel $model, Provider $dataProvider, $hook)
42
    {
43
        $this->_mageModel    = $model;
44
        $this->_dataProvider = $dataProvider;
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
     * @param $entity
76
     *
77
     * @throws UndefinedDataProvider
78
     *
79
     */
80
    public function throwUndefinedDataProvider($entity)
81
    {
82
        $this->_throwUndefinedDataProvider($entity);
83
    }
84
85
    /**
86
     * @param array $attributes
87
     *
88
     * @return array
89
     * @throws \Exception
90
     */
91
    public function processFixtureAttributes(array $attributes)
92
    {
93
        $fixtureAttributes = $this->_processFixtureAttributesWithProvider($attributes, $this->_getDataProvider());
94
95
        return $fixtureAttributes;
96
    }
97
98
    /**
99
     * @param array    $attributes
100
     * @param Provider $dataProvider
101
     *
102
     */
103
    public function processFixtureAttributesWithProvider(array $attributes, Provider $dataProvider)
104
    {
105
        $this->_processFixtureAttributesWithProvider($attributes, $dataProvider);
106
    }
107
108
    /**
109
     * @param $mageModel
110
     * @param $data
111
     *
112
     */
113
    public function saveFixtureWithModelAndData($mageModel, $data)
114
    {
115
        $this->_saveFixtureWithModelAndData($mageModel, $data);
116
    }
117
118
    /**
119
     * @return array
120
     * @throws \Exception
121
     */
122
    protected function _getMageModelData()
123
    {
124
        return $this->_getMageModel()->getData();
125
    }
126
127
    /**
128
     * @return MagentoModel
129
     */
130
    protected function _getMageModel()
131
    {
132
        return $this->_mageModel;
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138
    protected function _getDataProvider()
139
    {
140
        return $this->_dataProvider;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    protected function _getHook()
147
    {
148
        return $this->_hook;
149
    }
150
151
    /**
152
     * @return array
153
     * @throws \Exception
154
     */
155
    protected function _getFixtureAttributes()
156
    {
157
        $this->_throwUndefinedAttributesException(isset($this->_data['fixture']['attributes']));
158
159
        return $this->_processFixtureAttributes($this->_data['fixture']['attributes']);
160
    }
161
162
    /**
163
     * @param array $attributes
164
     *
165
     * @return array
166
     * @throws \Exception
167
     */
168
    protected function _processFixtureAttributes(array $attributes)
169
    {
170
        $fixtureAttributes = $this->_processFixtureAttributesWithProvider($attributes, $this->_getDataProvider());
171
172
        return $fixtureAttributes;
173
    }
174
175
    /**
176
     * @param array    $attributes
177
     * @param Provider $provider
178
     *
179
     * @return array
180
     * @throws ProviderMethodNotFound
181
     */
182
    private function _processFixtureAttributesWithProvider(array $attributes, Provider $provider)
183
    {
184
        $fixtureAttributes = [];
185
        foreach ($attributes as $key => $attributeValue) {
186
            if (is_array($attributeValue)) {
187
                $fixtureAttributes[$key] = $attributeValue;
188
                continue;
189
            }
190
            if (preg_match('/^\{\{.*?\}\}$/i', $attributeValue)) {
191
                $method = trim($attributeValue, '{}');
192
                $this->_throwMethodNotFoundExceptionForProvider($provider, $method);
193
                $fixtureAttributes[$key] = $provider->$method();
194
            } else {
195
                $fixtureAttributes[$key] = $attributeValue;
196
            }
197
        }
198
199
        return $fixtureAttributes;
200
    }
201
202
    /**
203
     * @return int
204
     * @throws \Exception
205
     */
206
    protected function _saveFixture()
207
    {
208
        MagentoStoreScope::setAdminStoreScope();
209
        $this->_getMageModel()->save();
210
        MagentoStoreScope::setCurrentStoreScope();
211
212
        return $this->_getMageModel()->getId();
213
    }
214
215
    /**
216
     * @param MagentoModel $mageModel
217
     * @param              $data
218
     *
219
     * @return int
220
     * @throws NullFixtureId
221
     */
222
    protected function _saveFixtureWithModelAndData(MagentoModel $mageModel, $data)
223
    {
224
        MagentoStoreScope::setAdminStoreScope();
225
        $mageModel->setData($data);
226
        $mageModel->save();
227
        MagentoStoreScope::setCurrentStoreScope();
228
229
        return $mageModel->getId();
230
    }
231
232
    /**
233
     * Add data to model, but don't save fixture
234
     *
235
     * @param array $data
236
     *
237
     * @return int
238
     * @throws NullFixtureId
239
     */
240
    protected function _initFixtureWithData(array $data)
241
    {
242
        $this->_getMageModel()->setData($data);
243
    }
244
245
    /**
246
     * @param $node
247
     *
248
     * @return array
249
     */
250
    protected function _buildManySimple($node)
251
    {
252
        $products = $this->_data['fixture'][$node]['products'];
253
254
        return FixtureBuilder::buildMany(
255
            FixtureBuilder::SIMPLE_PRODUCT_FIXTURE_TYPE, $this, $products, $this->getHook()
256
        );
257
    }
258
259
260
    /**
261
     * @param boolean $isSet
262
     * @param string  $message
263
     *
264
     * @throws UndefinedAttributes
265
     *
266
     */
267
    protected function _throwUndefinedAttributesException(
268
        $isSet, $message = 'Fixture attributes have not been defined. Check fixture yml file.'
269
    ) {
270
        if (!$isSet) {
271
            throw new UndefinedAttributes($message);
272
        }
273
    }
274
275
    /**
276
     * @param array $a
277
     *
278
     * @throws UndefinedDataProvider
279
     *
280
     */
281
    protected function _throwUndefinedDataProvider(array $a)
282
    {
283
        if (!isset($a['data_provider'])) {
284
            throw new UndefinedDataProvider(
285
                'Fixture has no data provider specified. Check fixture yml file.'
286
            );
287
        }
288
    }
289
290
    /**
291
     * @param Provider $provider
292
     * @param          $method
293
     *
294
     * @throws ProviderMethodNotFound
295
     *
296
     */
297
    protected function _throwMethodNotFoundExceptionForProvider(Provider $provider, $method)
298
    {
299
        if (!method_exists($provider, $method)) {
300
            throw new ProviderMethodNotFound(
301
                "Given provider does not have the method " . $method . ' -> ' . get_class($provider)
302
            );
303
        }
304
    }
305
}
306