Completed
Push — master ( a1aea0...81b71c )
by Tim
9s
created

getEavAttributeByAttributeCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
     * Process the observer's business logic.
63
     *
64
     * @return array The processed row
65
     */
66
    protected function process()
67
    {
68
69
        // load parent/child IDs
70
        $parentId = $this->mapParentSku($parentSku = $this->getValue(ColumnKeys::VARIANT_PARENT_SKU));
71
72
        // query whether or not, the parent ID have changed
73
        if ($this->isParentId($parentId)) {
74
            return;
75
        }
76
77
        // prepare the store view code
78
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Unused Code introduced by
The call to VariantSuperAttributeObs...:prepareStoreViewCode() has too many arguments starting with $this->getRow().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
79
80
        // preserve the parent ID
81
        $this->setParentId($parentId);
82
83
        // extract the option value and attribute code from the row
84
        $attributeCode = $this->getValue(ColumnKeys::VARIANT_ATTRIBUTE_CODE);
85
86
        // load the store ID
87
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
88
        $this->storeId = $store[MemberNames::STORE_ID];
89
90
        // load the EAV attribute with the found attribute code
91
        $this->eavAttribute = $this->getEavAttributeByAttributeCode($attributeCode);
92
93
        try {
94
            // initialize and save the super attribute
95
            $productSuperAttribute = $this->initializeProductSuperAttribute($this->prepareProducSuperAttributeAttributes());
96
            $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...
97
98
            // initialize and save the super attribute label
99
            $productSuperAttributeLabel = $this->initializeProductSuperAttributeLabel($this->prepareProductSuperAttributeLabelAttributes());
100
            $this->persistProductSuperAttributeLabel($productSuperAttributeLabel);
101
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
102
        } catch (\Exception $e) {
103
            // prepare a more detailed error messsage
104
            $message = sprintf(
105
                'Super attribute for SKU %s and attribute %s can\'t be created in file %s on line %d',
106
                $parentSku,
107
                $attributeCode,
108
                $this->getFilename(),
109
                $this->getLineNumber()
110
            );
111
112
            // query whether or not, debug mode is enabled
113
            if ($this->isDebugMode()) {
114
                // log a warning and return immediately
115
                $this->getSystemLogger()->warning($message);
116
                return;
117
            }
118
119
            // if we're NOT in debug mode, re-throw a more detailed exception
120
            throw new \Exception($message, null, $e);
121
        }
122
    }
123
124
    /**
125
     * Prepare the product super attribute attributes that has to be persisted.
126
     *
127
     * @return array The prepared product attribute attributes
128
     */
129
    protected function prepareProducSuperAttributeAttributes()
130
    {
131
132
        // load the parent ID
133
        $parentId = $this->getParentId();
134
135
        // load the attribute ID
136
        $attributeId = $this->eavAttribute[MemberNames::ATTRIBUTE_ID];
137
138
        // initialize the attributes and return them
139
        return $this->initializeEntity(
140
            array(
141
                MemberNames::PRODUCT_ID   => $parentId,
142
                MemberNames::ATTRIBUTE_ID => $attributeId,
143
                MemberNames::POSITION     => 0
144
            )
145
        );
146
    }
147
148
    /**
149
     * Prepare the product super attribute label attributes that has to be persisted.
150
     *
151
     * @return array The prepared product super attribute label attributes
152
     */
153
    protected function prepareProductSuperAttributeLabelAttributes()
154
    {
155
156
        // extract the parent/child ID as well as option value and variation label from the row
157
        $variationLabel = $this->getValue(ColumnKeys::VARIANT_VARIATION_LABEL);
158
159
        // query whether or not we've to create super attribute labels
160
        if (empty($variationLabel)) {
161
            $variationLabel = $this->eavAttribute[MemberNames::FRONTENT_LABEL];
162
        }
163
164
        // initialize the attributes and return them
165
        return $this->initializeEntity(
166
            array(
167
                MemberNames::PRODUCT_SUPER_ATTRIBUTE_ID => $this->productSuperAttributeId,
168
                MemberNames::STORE_ID                   => $this->storeId,
169
                MemberNames::USE_DEFAULT                => 0,
170
                MemberNames::VALUE                      => $variationLabel
171
            )
172
        );
173
    }
174
175
    /**
176
     * Initialize the product super attribute with the passed attributes and returns an instance.
177
     *
178
     * @param array $attr The product super attribute attributes
179
     *
180
     * @return array The initialized product super attribute
181
     */
182
    protected function initializeProductSuperAttribute(array $attr)
183
    {
184
        return $attr;
185
    }
186
187
    /**
188
     * Initialize the product super attribute label with the passed attributes and returns an instance.
189
     *
190
     * @param array $attr The product super attribute label attributes
191
     *
192
     * @return array The initialized product super attribute label
193
     */
194
    protected function initializeProductSuperAttributeLabel(array $attr)
195
    {
196
        return $attr;
197
    }
198
199
    /**
200
     * Map's the passed SKU of the parent product to it's PK.
201
     *
202
     * @param string $parentSku The SKU of the parent product
203
     *
204
     * @return integer The primary key used to create relations
205
     */
206
    protected function mapParentSku($parentSku)
207
    {
208
        return $this->mapSkuToEntityId($parentSku);
209
    }
210
211
    /**
212
     * Return the entity ID for the passed SKU.
213
     *
214
     * @param string $sku The SKU to return the entity ID for
215
     *
216
     * @return integer The mapped entity ID
217
     * @throws \Exception Is thrown if the SKU is not mapped yet
218
     */
219
    protected function mapSkuToEntityId($sku)
220
    {
221
        return $this->getSubject()->mapSkuToEntityId($sku);
222
    }
223
224
    /**
225
     * Return's TRUE if the passed ID is the parent one.
226
     *
227
     * @param integer $parentId The parent ID to check
228
     *
229
     * @return boolean TRUE if the passed ID is the parent one
230
     */
231
    protected function isParentId($parentId)
232
    {
233
        return $this->getParentId() === $parentId;
234
    }
235
236
    /**
237
     * Set's the ID of the parent product to relate the variant with.
238
     *
239
     * @param integer $parentId The ID of the parent product
240
     *
241
     * @return void
242
     */
243
    protected function setParentId($parentId)
244
    {
245
        $this->getSubject()->setParentId($parentId);
246
    }
247
248
    /**
249
     * Return's the ID of the parent product to relate the variant with.
250
     *
251
     * @return integer The ID of the parent product
252
     */
253
    protected function getParentId()
254
    {
255
        return $this->getSubject()->getParentId();
256
    }
257
258
    /**
259
     * Return's the store for the passed store code.
260
     *
261
     * @param string $storeCode The store code to return the store for
262
     *
263
     * @return array The requested store
264
     * @throws \Exception Is thrown, if the requested store is not available
265
     */
266
    protected function getStoreByStoreCode($storeCode)
267
    {
268
        return $this->getSubject()->getStoreByStoreCode($storeCode);
269
    }
270
271
    /**
272
     * Return's an array with the available stores.
273
     *
274
     * @return array The available stores
275
     */
276
    protected function getStores()
277
    {
278
        return $this->getSubject()->getStores();
279
    }
280
281
    /**
282
     * Return's the first EAV attribute for the passed attribute code.
283
     *
284
     * @param string $attributeCode The attribute code
285
     *
286
     * @return array The array with the EAV attribute
287
     */
288
    protected function getEavAttributeByAttributeCode($attributeCode)
289
    {
290
        return $this->getSubject()->getEavAttributeByAttributeCode($attributeCode);
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