Completed
Pull Request — master (#17)
by Tim
03:53
created

ProductAttributeObserver::getAttributeId()   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
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductAttributeObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Observer that creates/updates the product's attributes.
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
35
 * @link      http://www.techdivision.com
36
 */
37
class ProductAttributeObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The ID of the attribute to create the values for.
42
     *
43
     * @var integer
44
     */
45
    protected $attributeId;
46
47
    /**
48
     * The attribute code of the attribute to create the values for.
49
     *
50
     * @var string
51
     */
52
    protected $attributeCode;
53
54
    /**
55
     * The backend type of the attribute to create the values for.
56
     *
57
     * @var string
58
     */
59
    protected $backendType;
60
61
    /**
62
     * The attribute value to process.
63
     *
64
     * @var mixed
65
     */
66
    protected $attributeValue;
67
68
    /**
69
     * Will be invoked by the action on the events the listener has been registered for.
70
     *
71
     * @param array $row The row to handle
72
     *
73
     * @return array The modified row
74
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
75
     */
76
    public function handle(array $row)
77
    {
78
79
        // initialize the row
80
        $this->setRow($row);
81
82
        // process the functionality and return the row
83
        $this->process();
84
85
        // return the processed row
86
        return $this->getRow();
87
    }
88
89
    /**
90
     * Process the observer's business logic.
91
     *
92
     * @return void
93
     */
94
    public function process()
95
    {
96
97
        // initialize the store view code
98
        $this->prepareStoreViewCode();
99
100
        // load the attributes by the found attribute set
101
        $attributes = $this->getAttributes();
102
103
        // iterate over the attribute related by the found attribute set
104
        foreach ($attributes as $attribute) {
105
            // load the attribute code/ID
106
            $attributeCode = $attribute[MemberNames::ATTRIBUTE_CODE];
107
            $attributeId = (integer) $attribute[MemberNames::ATTRIBUTE_ID];
108
109
            // query weather or not we've a mapping, if yes, map the attribute code
110
            $attributeCode = $this->mapAttributeCodeByHeaderMapping($attributeCode);
111
112
            // query whether or not we've a attribute value found
113
            $attributeValue = $this->getValue($attributeCode);
114
            if ($attributeValue == null) {
115
                continue;
116
            }
117
118
            // load the backend type => to find the apropriate entity
119
            $backendType = $attribute[MemberNames::BACKEND_TYPE];
120
            if ($backendType == null) {
121
                $this->getSystemLogger()
122
                     ->warning(sprintf('Found EMTPY backend type for attribute %s', $attributeCode));
123
                continue;
124
            }
125
126
            // load the supported backend types
127
            $backendTypes = $this->getBackendTypes();
128
129
            // query whether or not we've found a supported backend type
130
            if (isset($backendTypes[$backendType])) {
131
                // initialize attribute ID/code and backend type
132
                $this->setAttributeId($attributeId);
133
                $this->setAttributeCode($attributeCode);
134
                $this->setBackendType($backendType);
135
136
                // initialize the persist method for the found backend type
137
                list ($persistMethod, ) = $backendTypes[$backendType];
138
139
                // set the attribute value
140
                $this->setAttributeValue($attributeValue);
141
142
                // load the prepared values
143
                $entity = $this->initializeAttribute($this->prepareAttributes());
144
145
                // persist the attribute
146
                $this->$persistMethod($entity);
147
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
148
            } else {
149
                // log the debug message
150
                $this->getSystemLogger()->debug(
151
                    sprintf('Found invalid backend type %s for attribute %s', $backendType, $attributeCode)
152
                );
153
            }
154
        }
155
    }
156
157
    /**
158
     * Prepare the attributes of the entity that has to be persisted.
159
     *
160
     * @return array The prepared attributes
161
     */
162
    public function prepareAttributes()
163
    {
164
165
        // load the attribute value
166
        $attributeValue = $this->getAttributeValue();
167
168
        // laod the callbacks for the actual attribute code
169
        $callbacks = $this->getCallbacksByType($this->getAttributeCode());
170
171
        // invoke the pre-cast callbacks
172
        foreach ($callbacks as $callback) {
173
            $attributeValue = $callback->handle($attributeValue);
174
        }
175
176
        // load the ID of the product that has been created recently
177
        $lastEntityId = $this->getPrimaryKey();
178
179
        // load the ID of the attribute to create the values for
180
        $attributeId = $this->getAttributeId();
181
182
        // load the store ID
183
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
184
185
        // load the backend type of the actual attribute
186
        $backendType = $this->getBackendType();
187
188
        // cast the value based on the backend type
189
        $castedValue = $this->castValueByBackendType($backendType, $attributeValue);
190
191
        // prepare the attribute values
192
        return $this->initializeEntity(
193
            array(
194
                MemberNames::ENTITY_ID    => $lastEntityId,
195
                MemberNames::ATTRIBUTE_ID => $attributeId,
196
                MemberNames::STORE_ID     => $storeId,
197
                MemberNames::VALUE        => $castedValue
198
            )
199
        );
200
    }
201
202
    /**
203
     * Initialize the category product with the passed attributes and returns an instance.
204
     *
205
     * @param array $attr The category product attributes
206
     *
207
     * @return array The initialized category product
208
     */
209
    public function initializeAttribute(array $attr)
210
    {
211
        return $attr;
212
    }
213
214
    /**
215
     * Return's the PK to create the product => attribute relation.
216
     *
217
     * @return integer The PK to create the relation with
218
     */
219
    public function getPrimaryKey()
220
    {
221
        return $this->getLastEntityId();
222
    }
223
224
    /**
225
     * Set's the attribute value to process.
226
     *
227
     * @param mixed $attributeValue The attribute value
228
     *
229
     * @return void
230
     */
231
    public function setAttributeValue($attributeValue)
232
    {
233
        $this->attributeValue = $attributeValue;
234
    }
235
236
    /**
237
     * Return's the attribute value to process.
238
     *
239
     * @return mixed The attribute value
240
     */
241
    public function getAttributeValue()
242
    {
243
        return $this->attributeValue;
244
    }
245
246
    /**
247
     * Set's the backend type of the attribute to create the values for.
248
     *
249
     * @param string $backendType The backend type
250
     *
251
     * @return void
252
     */
253
    public function setBackendType($backendType)
254
    {
255
        $this->backendType = $backendType;
256
    }
257
258
    /**
259
     * Return's the backend type of the attribute to create the values for.
260
     *
261
     * @return string The backend type
262
     */
263
    public function getBackendType()
264
    {
265
        return $this->backendType;
266
    }
267
268
    /**
269
     * Set's the attribute code of the attribute to create the values for.
270
     *
271
     * @param string $attributeCode The attribute code
272
     *
273
     * @return void
274
     */
275
    public function setAttributeCode($attributeCode)
276
    {
277
        $this->attributeCode = $attributeCode;
278
    }
279
280
    /**
281
     * Return's the attribute code of the attribute to create the values for.
282
     *
283
     * @return string The attribute code
284
     */
285
    public function getAttributeCode()
286
    {
287
        return $this->attributeCode;
288
    }
289
290
    /**
291
     * Set's the ID of the attribute to create the values for.
292
     *
293
     * @param integer $attributeId The attribute ID
294
     *
295
     * @return void
296
     */
297
    public function setAttributeId($attributeId)
298
    {
299
        $this->attributeId = $attributeId;
300
    }
301
302
    /**
303
     * Return's the ID of the attribute to create the values for.
304
     *
305
     * @return integer The attribute ID
306
     */
307
    public function getAttributeId()
308
    {
309
        return $this->attributeId;
310
    }
311
312
    /**
313
     * Map the passed attribute code, if a header mapping exists and return the
314
     * mapped mapping.
315
     *
316
     * @param string $attributeCode The attribute code to map
317
     *
318
     * @return string The mapped attribute code, or the original one
319
     */
320
    public function mapAttributeCodeByHeaderMapping($attributeCode)
321
    {
322
        return $this->getSubject()->mapAttributeCodeByHeaderMapping($attributeCode);
323
    }
324
325
    /**
326
     * Return's the array with callbacks for the passed type.
327
     *
328
     * @param string $type The type of the callbacks to return
329
     *
330
     * @return array The callbacks
331
     */
332
    public function getCallbacksByType($type)
333
    {
334
        return $this->getSubject()->getCallbacksByType($type);
335
    }
336
337
    /**
338
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
339
     *
340
     * @return array The mapping for the supported backend types
341
     */
342
    public function getBackendTypes()
343
    {
344
        return $this->getSubject()->getBackendTypes();
345
    }
346
347
    /**
348
     * Return's the attributes for the attribute set of the product that has to be created.
349
     *
350
     * @return array The attributes
351
     * @throws \Exception
352
     */
353
    public function getAttributes()
354
    {
355
        return $this->getSubject()->getAttributes();
356
    }
357
358
    /**
359
     * Return's the store ID of the actual row, or of the default store
360
     * if no store view code is set in the CSV file.
361
     *
362
     * @param string|null $default The default store view code to use, if no store view code is set in the CSV file
363
     *
364
     * @return integer The ID of the actual store
365
     * @throws \Exception Is thrown, if the store with the actual code is not available
366
     */
367
    public function getRowStoreId($default = null)
368
    {
369
        return $this->getSubject()->getRowStoreId($default);
370
    }
371
372
    /**
373
     * Persist's the passed product varchar attribute.
374
     *
375
     * @param array $attribute The attribute to persist
376
     *
377
     * @return void
378
     */
379
    public function persistProductVarcharAttribute($attribute)
380
    {
381
        $this->getSubject()->persistProductVarcharAttribute($attribute);
382
    }
383
384
    /**
385
     * Persist's the passed product integer attribute.
386
     *
387
     * @param array $attribute The attribute to persist
388
     *
389
     * @return void
390
     */
391
    public function persistProductIntAttribute($attribute)
392
    {
393
        $this->getSubject()->persistProductIntAttribute($attribute);
394
    }
395
396
    /**
397
     * Persist's the passed product decimal attribute.
398
     *
399
     * @param array $attribute The attribute to persist
400
     *
401
     * @return void
402
     */
403
    public function persistProductDecimalAttribute($attribute)
404
    {
405
        $this->getSubject()->persistProductDecimalAttribute($attribute);
406
    }
407
408
    /**
409
     * Persist's the passed product datetime attribute.
410
     *
411
     * @param array $attribute The attribute to persist
412
     *
413
     * @return void
414
     */
415
    public function persistProductDatetimeAttribute($attribute)
416
    {
417
        $this->getSubject()->persistProductDatetimeAttribute($attribute);
418
    }
419
420
    /**
421
     * Persist's the passed product text attribute.
422
     *
423
     * @param array $attribute The attribute to persist
424
     *
425
     * @return void
426
     */
427
    public function persistProductTextAttribute($attribute)
428
    {
429
        $this->getSubject()->persistProductTextAttribute($attribute);
430
    }
431
}
432