Completed
Pull Request — master (#74)
by Tim
04:34
created

parseAdditionaAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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