Completed
Pull Request — master (#89)
by Tim
10:26 queued 08:30
created

AdditionalAttributeObserver::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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
use Doctrine\Common\Annotations\Annotation\Attributes;
26
27
/**
28
 * Observer that prepares the additional product attribues found in the CSV file
29
 * in the row 'additional_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
35
 * @link      http://www.techdivision.com
36
 */
37
class AdditionalAttributeObserver extends AbstractObserver
38
{
39
40
    /**
41
     * Will be invoked by the action on the events the listener has been registered for.
42
     *
43
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
44
     *
45
     * @return array The modified row
46
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
47
     */
48 1
    public function handle(SubjectInterface $subject)
49
    {
50
51
        // initialize the row
52 1
        $this->setSubject($subject);
53 1
        $this->setRow($subject->getRow());
54
55
        // process the functionality and return the row
56 1
        $this->process();
57
58
        // return the processed row
59 1
        return $this->getRow();
60
    }
61
62
    /**
63
     * Process the observer's business logic.
64
     *
65
     * @return array The processed row
66
     */
67 1
    protected function process()
68
    {
69
70
        // query whether or not the row has additional attributes
71 1
        if ($additionalAttributes = $this->getValue(ColumnKeys::ADDITIONAL_ATTRIBUTES)) {
72
            // explode the additional attributes
73 1
            $additionalAttributes = $this->parseAdditionaAttributes($additionalAttributes);
74
            // load the subject instance
75 1
            $subject = $this->getSubject();
76
            // iterate over the attributes and append them to the row
77 1
            foreach ($additionalAttributes as $additionalAttribute) {
78
                // explode attribute code/option value from the attribute
79 1
                list ($attributeCode, $optionValue) = $subject->explode($additionalAttribute, '=');
80
81
                // try to load the appropriate key for the value
82 1
                if (!$subject->hasHeader($attributeCode)) {
83 1
                    $subject->addHeader($attributeCode);
84 1
                }
85
86
                // append/replace the attribute value
87 1
                $this->setValue($attributeCode, $optionValue);
88
89
                // add a log message in debug mod
90 1
                if ($subject->isDebugMode()) {
91 1
                    $subject->getSystemLogger()->debug(
92 1
                        sprintf(
93 1
                            'Extract new column "%s" with value "%s" from column "%s" in file %s on line %d',
94 1
                            $attributeCode,
95 1
                            $optionValue,
96 1
                            ColumnKeys::ADDITIONAL_ATTRIBUTES,
97 1
                            $subject->getFilename(),
98 1
                            $subject->getLineNumber()
99 1
                        )
100 1
                    );
101 1
                }
102 1
            }
103 1
        }
104 1
    }
105
106
    /**
107
     * Parses the string with the additional attributes as CSV and returns an array.
108
     *
109
     * @param string $additionalAttributes The string with the additional attributes
110
     *
111
     * @return array The array with the parsed
112
     * @link http://php.net/manual/de/function.str-getcsv.php
113
     */
114 1
    protected function parseAdditionaAttributes($additionalAttributes)
115
    {
116
117
        // load the global configuration
118 1
        $configuration = $this->getSubject()->getConfiguration()->getConfiguration();
119
120
        // initializet delimiter, enclosure and escape char
121 1
        $delimiter = $configuration->getDelimiter();
122 1
        $enclosure = $configuration->getEnclosure();
123 1
        $escape = $configuration->getEscape();
124
125
        // parse and return the found data as array
126 1
        return str_getcsv($additionalAttributes, $delimiter, $enclosure, $escape);
127
    }
128
}
129