Completed
Push — master ( 6ad7a0...8501d0 )
by Tim
11s
created

AdditionalAttributeObserver::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\AdditionalAttributeObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Observers;
22
23
use TechDivision\Import\Utils\ColumnKeys;
24
25
/**
26
 * Observer that prepares the additional product attribues found in the CSV file
27
 * in the row 'additional_attributes'.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class AdditionalAttributeObserver extends AbstractObserver
36
{
37
38
    /**
39
     * Will be invoked by the action on the events the listener has been registered for.
40
     *
41
     * @param array $row The row to handle
42
     *
43
     * @return array The modified row
44
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
45
     */
46
    public function handle(array $row)
47
    {
48
49
        // initialize the row
50
        $this->setRow($row);
51
52
        // process the functionality and return the row
53
        $this->process();
54
55
        // return the processed row
56
        return $this->getRow();
57
    }
58
59
    /**
60
     * Process the observer's business logic.
61
     *
62
     * @return array The processed row
63
     */
64
    protected function process()
65
    {
66
67
        // query whether or not the row has additional attributes
68
        if ($additionalAttributes = $this->getValue(ColumnKeys::ADDITIONAL_ATTRIBUTES)) {
69
            // query if the additional attributes have a value, at least
70
            if (strstr($additionalAttributes, '=') === false) {
71
                return;
72
            }
73
74
            // explode the additional attributes
75
            $additionalAttributes = explode(',', $additionalAttributes);
76
77
            // iterate over the attributes and append them to the row
78
            foreach ($additionalAttributes as $additionalAttribute) {
79
                // explode attribute code/option value from the attribute
80
                list ($attributeCode, $optionValue) = explode('=', $additionalAttribute);
81
82
                // try to load the appropriate key for the value
83
                if (!$this->hasHeader($attributeCode)) {
84
                    $this->addHeader($attributeCode);
85
                }
86
87
                // append/replace the attribute value
88
                $this->setValue($attributeCode, $optionValue);
89
            }
90
        }
91
    }
92
}
93