Completed
Push — develop ( 0ee7e1...a464a6 )
by Carlo
02:50
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\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 $mageModel
96
     * @param $data
97
     *
98
     */
99
    public function saveFixtureWithModelAndData($mageModel, $data)
100
    {
101
        $this->_saveFixtureWithModelAndData($mageModel, $data);
102
    }
103
104
    /**
105
     * @return array
106
     * @throws \Exception
107
     */
108
    protected function _getMageModelData()
109
    {
110
        return $this->_getMageModel()->getData();
111
    }
112
113
    /**
114
     * @return MagentoModel
115
     */
116
    protected function _getMageModel()
117
    {
118
        return $this->_mageModel;
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    protected function _getDataProvider()
125
    {
126
        return $this->_dataProvider;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    protected function _getHook()
133
    {
134
        return $this->_hook;
135
    }
136
137
    /**
138
     * @return array
139
     * @throws \Exception
140
     */
141
    protected function _getFixtureAttributes()
142
    {
143
        $this->_throwUndefinedAttributesException(isset($this->_data['fixture']['attributes']));
144
145
        return $this->_data['fixture']['attributes'];
146
    }
147
148
    /**
149
     * @return int
150
     * @throws \Exception
151
     */
152
    protected function _saveFixture()
153
    {
154
        MagentoStoreScope::setAdminStoreScope();
155
        $this->_getMageModel()->save();
156
        MagentoStoreScope::setCurrentStoreScope();
157
158
        return $this->_getMageModel()->getId();
159
    }
160
161
    /**
162
     * @param MagentoModel $mageModel
163
     * @param $data
164
     * @return mixed
165
     * @throws \Exception
166
     */
167
    protected function _saveFixtureWithModelAndData(MagentoModel $mageModel, $data)
168
    {
169
        MagentoStoreScope::setAdminStoreScope();
170
        $mageModel->setData($data);
171
        $mageModel->save();
172
        MagentoStoreScope::setCurrentStoreScope();
173
174
        return $mageModel->getId();
175
    }
176
177
    /**
178
     * Add data to model, but don't invoke save
179
     *
180
     * @param array $data
181
     */
182
    protected function _initFixtureWithData(array $data)
183
    {
184
        $this->_getMageModel()->setData($data);
185
    }
186
187
    /**
188
     * @param $node
189
     *
190
     * @return array
191
     */
192
    protected function _buildManySimple($node)
193
    {
194
        $products = $this->_data['fixture'][$node]['products'];
195
196
        return FixtureBuilder::buildMany(
197
            FixtureBuilder::SIMPLE_PRODUCT_FIXTURE_TYPE, $this, $products, $this->getHook()
198
        );
199
    }
200
201
    /**
202
     * @param boolean $isSet
203
     * @param string  $message
204
     *
205
     * @throws UndefinedAttributes
206
     *
207
     */
208
    protected function _throwUndefinedAttributesException(
209
        $isSet, $message = 'Fixture attributes have not been defined. Check fixture yml file.'
210
    ) {
211
        if (!$isSet) {
212
            throw new UndefinedAttributes($message);
213
        }
214
    }
215
216
    /**
217
     * @param array $a
218
     *
219
     * @throws UndefinedDataProvider
220
     *
221
     */
222
    protected function _throwUndefinedDataProvider(array $a)
223
    {
224
        if (!isset($a['data_provider'])) {
225
            throw new UndefinedDataProvider(
226
                'Fixture has no data provider specified. Check fixture yml file.'
227
            );
228
        }
229
    }
230
}
231