Failed Conditions
Pull Request — develop (#8)
by Carlo
02:35
created

AbstractBuilder::iterateFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
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
     * @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
     * Iterate fixture data
87
     */
88
    public function iterateFixture()
89
    {
90
        $iterator = new DataIterator($this->_data);
91
        $this->_data = $iterator->apply($this->_getDataProvider());
92
    }
93
94
    /**
95
     * @param array $attributes
96
     * @deprecated fixture processing delegated to iterator
97
     *
98
     * @return array
99
     * @throws \Exception
100
     */
101
    public function processFixtureAttributes(array $attributes)
102
    {
103
        $fixtureAttributes = $this->_processFixtureAttributesWithProvider($attributes, $this->_getDataProvider());
0 ignored issues
show
Bug introduced by
The method _processFixtureAttributesWithProvider() does not exist on Magefix\Fixture\Builder\AbstractBuilder. Did you maybe mean processFixtureAttributes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
104
105
        return $fixtureAttributes;
106
    }
107
108
    /**
109
     * @param array    $attributes
110
     * @param Provider $dataProvider
111
     * @deprecated fixture processing delegated to iterator
112
     *
113
     */
114
    public function processFixtureAttributesWithProvider(array $attributes, Provider $dataProvider)
115
    {
116
        $this->_processFixtureAttributesWithProvider($attributes, $dataProvider);
0 ignored issues
show
Bug introduced by
The method _processFixtureAttributesWithProvider() does not exist on Magefix\Fixture\Builder\AbstractBuilder. Did you maybe mean processFixtureAttributes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
117
    }
118
119
    /**
120
     * @param $mageModel
121
     * @param $data
122
     *
123
     */
124
    public function saveFixtureWithModelAndData($mageModel, $data)
125
    {
126
        $this->_saveFixtureWithModelAndData($mageModel, $data);
127
    }
128
129
    /**
130
     * @return array
131
     * @throws \Exception
132
     */
133
    protected function _getMageModelData()
134
    {
135
        return $this->_getMageModel()->getData();
136
    }
137
138
    /**
139
     * @return MagentoModel
140
     */
141
    protected function _getMageModel()
142
    {
143
        return $this->_mageModel;
144
    }
145
146
    /**
147
     * @return mixed
148
     */
149
    protected function _getDataProvider()
150
    {
151
        return $this->_dataProvider;
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    protected function _getHook()
158
    {
159
        return $this->_hook;
160
    }
161
162
    /**
163
     * @return array
164
     * @throws \Exception
165
     */
166
    protected function _getFixtureAttributes()
167
    {
168
        $this->_throwUndefinedAttributesException(isset($this->_data['fixture']['attributes']));
169
170
        return $this->_data['fixture']['attributes'];
171
    }
172
173
    /**
174
     * @return int
175
     * @throws \Exception
176
     */
177
    protected function _saveFixture()
178
    {
179
        MagentoStoreScope::setAdminStoreScope();
180
        $this->_getMageModel()->save();
181
        MagentoStoreScope::setCurrentStoreScope();
182
183
        return $this->_getMageModel()->getId();
184
    }
185
186
    /**
187
     * @param MagentoModel $mageModel
188
     * @param $data
189
     * @return mixed
190
     * @throws \Exception
191
     */
192
    protected function _saveFixtureWithModelAndData(MagentoModel $mageModel, $data)
193
    {
194
        MagentoStoreScope::setAdminStoreScope();
195
        $mageModel->setData($data);
196
        $mageModel->save();
197
        MagentoStoreScope::setCurrentStoreScope();
198
199
        return $mageModel->getId();
200
    }
201
202
    /**
203
     * Add data to model, but don't invoke save
204
     *
205
     * @param array $data
206
     */
207
    protected function _initFixtureWithData(array $data)
208
    {
209
        $this->_getMageModel()->setData($data);
210
    }
211
212
    /**
213
     * @param $node
214
     *
215
     * @return array
216
     */
217
    protected function _buildManySimple($node)
218
    {
219
        $products = $this->_data['fixture'][$node]['products'];
220
221
        return FixtureBuilder::buildMany(
222
            FixtureBuilder::SIMPLE_PRODUCT_FIXTURE_TYPE, $this, $products, $this->getHook()
223
        );
224
    }
225
226
    /**
227
     * @param boolean $isSet
228
     * @param string  $message
229
     *
230
     * @throws UndefinedAttributes
231
     *
232
     */
233
    protected function _throwUndefinedAttributesException(
234
        $isSet, $message = 'Fixture attributes have not been defined. Check fixture yml file.'
235
    ) {
236
        if (!$isSet) {
237
            throw new UndefinedAttributes($message);
238
        }
239
    }
240
241
    /**
242
     * @param array $a
243
     *
244
     * @throws UndefinedDataProvider
245
     *
246
     */
247
    protected function _throwUndefinedDataProvider(array $a)
248
    {
249
        if (!isset($a['data_provider'])) {
250
            throw new UndefinedDataProvider(
251
                'Fixture has no data provider specified. Check fixture yml file.'
252
            );
253
        }
254
    }
255
}
256