Completed
Push — 14.x ( 9c911b )
by Tim
02:29
created

AdditionalAttributeObserver::parseAdditionaAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
use TechDivision\Import\Subjects\SubjectInterface;
25
26
/**
27
 * Observer that prepares the additional product attribues found in the CSV file
28
 * in the row 'additional_attributes'.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class AdditionalAttributeObserver extends AbstractObserver
37
{
38
39
    /**
40
     * Will be invoked by the action on the events the listener has been registered for.
41
     *
42
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
43
     *
44
     * @return array The modified row
45
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
46
     */
47 2
    public function handle(SubjectInterface $subject)
48
    {
49
50
        // initialize the row
51 2
        $this->setSubject($subject);
52 2
        $this->setRow($subject->getRow());
53
54
        // process the functionality and return the row
55 2
        $this->process();
56
57
        // return the processed row
58
        return $this->getRow();
59
    }
60
61
    /**
62
     * Process the observer's business logic.
63
     *
64
     * @return array The processed row
65
     */
66 2
    protected function process()
67
    {
68
69
        // query whether or not the row has additional attributes
70 2
        if ($additionalAttributes = $this->getValue(ColumnKeys::ADDITIONAL_ATTRIBUTES)) {
71
            // load the subject instance
72 2
            $subject = $this->getSubject();
73
            // explode the additional attributes
74 2
            $additionalAttributes = $subject->explode($additionalAttributes);
75
            // iterate over the attributes and append them to the row
76
            foreach ($additionalAttributes as $additionalAttribute) {
77
                // explode attribute code/option value from the attribute
78
                list ($attributeCode, $optionValue) = $subject->explode($additionalAttribute, '=');
79
80
                // try to load the appropriate key for the value
81
                if (!$subject->hasHeader($attributeCode)) {
82
                    $subject->addHeader($attributeCode);
83
                }
84
85
                // append/replace the attribute value
86
                $this->setValue($attributeCode, $optionValue);
87
88
                // add a log message in debug mod
89
                if ($subject->isDebugMode()) {
90
                    $subject->getSystemLogger()->debug(
91
                        sprintf(
92
                            'Extract new column "%s" with value "%s" from column "%s" in file %s on line %d',
93
                            $attributeCode,
94
                            $optionValue,
95
                            ColumnKeys::ADDITIONAL_ATTRIBUTES,
96
                            $subject->getFilename(),
97
                            $subject->getLineNumber()
98
                        )
99
                    );
100
                }
101
            }
102
        }
103
    }
104
}
105