Completed
Push — master ( a9b7ce...a1aea0 )
by Tim
11s
created

VariantSuperAttributeObserver::process()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
ccs 0
cts 17
cp 0
rs 8.8571
cc 2
eloc 14
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Variant\Observers\VariantSuperAttributeObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-variant
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Variant\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Variant\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Variant\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Oberserver that provides functionality for the product variant super attributes replace operation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-product-variant
35
 * @link      http://www.techdivision.com
36
 */
37
class VariantSuperAttributeObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The ID of the actual store to use.
42
     *
43
     * @var integer
44
     */
45
    protected $storeId;
46
47
    /**
48
     * The EAV attribute to handle.
49
     *
50
     * @var array
51
     */
52
    protected $eavAttribute;
53
54
    /**
55
     * The tempoarary stored product super attribute ID.
56
     *
57
     * @var integer
58
     */
59
    protected $productSuperAttributeId;
60
61
    /**
62
     * Will be invoked by the action on the events the listener has been registered for.
63
     *
64
     * @param array $row The row to handle
65
     *
66
     * @return array The modified row
67
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
68
     */
69
    public function handle(array $row)
70
    {
71
72
        // initialize the row
73
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
75
        // process the functionality and return the row
76
        $this->process();
77
78
        // return the processed row
79
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
    }
81
82
    /**
83
     * Process the observer's business logic.
84
     *
85
     * @return array The processed row
86
     */
87
    protected function process()
88
    {
89
90
        // load parent/child IDs
91
        $parentId = $this->mapParentSku($this->getValue(ColumnKeys::VARIANT_PARENT_SKU));
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
93
        // query whether or not, the parent ID have changed
94
        if ($this->isParentId($parentId)) {
95
            return;
96
        }
97
98
        // prepare the store view code
99
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
101
        // preserve the parent ID
102
        $this->setParentId($parentId);
103
104
        // extract the parent/child ID as well as option value and variation label from the row
105
        $optionValue = $this->getValue(ColumnKeys::VARIANT_OPTION_VALUE);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
107
        // load the store ID
108
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
109
        $this->storeId = $store[MemberNames::STORE_ID];
110
111
        // load the EAV attribute
112
        $this->eavAttribute = $this->getEavAttributeByOptionValueAndStoreId($optionValue, $this->storeId);
113
114
        // initialize and save the super attribute
115
        $productSuperAttribute = $this->initializeProductSuperAttribute($this->prepareProducSuperAttributeAttributes());
116
        $this->productSuperAttributeId = $this->persistProductSuperAttribute($productSuperAttribute);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->productSuperAttributeId is correct as $this->persistProductSup...$productSuperAttribute) (which targets TechDivision\Import\Prod...ProductSuperAttribute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
117
118
        // initialize and save the super attribute label
119
        $productSuperAttributeLabel = $this->initializeProductSuperAttributeLabel($this->prepareProductSuperAttributeLabelAttributes());
120
        $this->persistProductSuperAttributeLabel($productSuperAttributeLabel);
121
    }
122
123
    /**
124
     * Prepare the product super attribute attributes that has to be persisted.
125
     *
126
     * @return array The prepared product attribute attributes
127
     */
128
    protected function prepareProducSuperAttributeAttributes()
129
    {
130
131
        // load the parent ID
132
        $parentId = $this->getParentId();
133
134
        // load the attribute ID
135
        $attributeId = $this->eavAttribute[MemberNames::ATTRIBUTE_ID];
136
137
        // initialize the attributes and return them
138
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            array(
140
                MemberNames::PRODUCT_ID   => $parentId,
141
                MemberNames::ATTRIBUTE_ID => $attributeId,
142
                MemberNames::POSITION     => 0
143
            )
144
        );
145
    }
146
147
    /**
148
     * Prepare the product super attribute label attributes that has to be persisted.
149
     *
150
     * @return array The prepared product super attribute label attributes
151
     */
152
    protected function prepareProductSuperAttributeLabelAttributes()
153
    {
154
155
        // extract the parent/child ID as well as option value and variation label from the row
156
        $variationLabel = $this->getValue(ColumnKeys::VARIANT_VARIATION_LABEL);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
158
        // query whether or not we've to create super attribute labels
159
        if (empty($variationLabel)) {
160
            $variationLabel = $this->eavAttribute[MemberNames::FRONTENT_LABEL];
161
        }
162
163
        // initialize the attributes and return them
164
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...SuperAttributeObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
            array(
166
                MemberNames::PRODUCT_SUPER_ATTRIBUTE_ID => $this->productSuperAttributeId,
167
                MemberNames::STORE_ID                   => $this->storeId,
168
                MemberNames::USE_DEFAULT                => 0,
169
                MemberNames::VALUE                      => $variationLabel
170
            )
171
        );
172
    }
173
174
    /**
175
     * Initialize the product super attribute with the passed attributes and returns an instance.
176
     *
177
     * @param array $attr The product super attribute attributes
178
     *
179
     * @return array The initialized product super attribute
180
     */
181
    protected function initializeProductSuperAttribute(array $attr)
182
    {
183
        return $attr;
184
    }
185
186
    /**
187
     * Initialize the product super attribute label with the passed attributes and returns an instance.
188
     *
189
     * @param array $attr The product super attribute label attributes
190
     *
191
     * @return array The initialized product super attribute label
192
     */
193
    protected function initializeProductSuperAttributeLabel(array $attr)
194
    {
195
        return $attr;
196
    }
197
198
    /**
199
     * Map's the passed SKU of the parent product to it's PK.
200
     *
201
     * @param string $parentSku The SKU of the parent product
202
     *
203
     * @return integer The primary key used to create relations
204
     */
205
    protected function mapParentSku($parentSku)
206
    {
207
        return $this->mapSkuToEntityId($parentSku);
208
    }
209
210
    /**
211
     * Return the entity ID for the passed SKU.
212
     *
213
     * @param string $sku The SKU to return the entity ID for
214
     *
215
     * @return integer The mapped entity ID
216
     * @throws \Exception Is thrown if the SKU is not mapped yet
217
     */
218
    protected function mapSkuToEntityId($sku)
219
    {
220
        return $this->getSubject()->mapSkuToEntityId($sku);
221
    }
222
223
    /**
224
     * Return's TRUE if the passed ID is the parent one.
225
     *
226
     * @param integer $parentId The parent ID to check
227
     *
228
     * @return boolean TRUE if the passed ID is the parent one
229
     */
230
    protected function isParentId($parentId)
231
    {
232
        return $this->getParentId() === $parentId;
233
    }
234
235
    /**
236
     * Set's the ID of the parent product to relate the variant with.
237
     *
238
     * @param integer $parentId The ID of the parent product
239
     *
240
     * @return void
241
     */
242
    protected function setParentId($parentId)
243
    {
244
        $this->getSubject()->setParentId($parentId);
245
    }
246
247
    /**
248
     * Return's the ID of the parent product to relate the variant with.
249
     *
250
     * @return integer The ID of the parent product
251
     */
252
    protected function getParentId()
253
    {
254
        return $this->getSubject()->getParentId();
255
    }
256
257
    /**
258
     * Return's the store for the passed store code.
259
     *
260
     * @param string $storeCode The store code to return the store for
261
     *
262
     * @return array The requested store
263
     * @throws \Exception Is thrown, if the requested store is not available
264
     */
265
    protected function getStoreByStoreCode($storeCode)
266
    {
267
        return $this->getSubject()->getStoreByStoreCode($storeCode);
268
    }
269
270
    /**
271
     * Return's an array with the available stores.
272
     *
273
     * @return array The available stores
274
     */
275
    protected function getStores()
276
    {
277
        return $this->getSubject()->getStores();
278
    }
279
280
    /**
281
     * Return's the first EAV attribute for the passed option value and store ID.
282
     *
283
     * @param string $optionValue The option value of the EAV attributes
284
     * @param string $storeId     The store ID of the EAV attribues
285
     *
286
     * @return array The array with the EAV attribute
287
     */
288
    protected function getEavAttributeByOptionValueAndStoreId($optionValue, $storeId)
289
    {
290
        return $this->getSubject()->getEavAttributeByOptionValueAndStoreId($optionValue, $storeId);
291
    }
292
293
    /**
294
     * Persist's the passed product super attribute data and return's the ID.
295
     *
296
     * @param array $productSuperAttribute The product super attribute data to persist
297
     *
298
     * @return void
299
     */
300
    protected function persistProductSuperAttribute($productSuperAttribute)
301
    {
302
        return $this->getSubject()->persistProductSuperAttribute($productSuperAttribute);
303
    }
304
305
    /**
306
     * Persist's the passed product super attribute label data and return's the ID.
307
     *
308
     * @param array $productSuperAttributeLabel The product super attribute label data to persist
309
     *
310
     * @return void
311
     */
312
    protected function persistProductSuperAttributeLabel($productSuperAttributeLabel)
313
    {
314
        return $this->getSubject()->persistProductSuperAttributeLabel($productSuperAttributeLabel);
315
    }
316
}
317