Completed
Push — master ( 2b9e5a...666597 )
by Tim
17s queued 11s
created

AdditionalAttributeObserver::process()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 0
cp 0
rs 9.0488
c 0
b 0
f 0
cc 5
nc 6
nop 0
crap 30
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 TechDivision\Import\Serializer\SerializerFactoryInterface;
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 implements ObserverFactoryInterface
38
{
39
40
    /**
41
     * The serializer used to serializer/unserialize the categories from the path column.
42
     *
43
     * @var \TechDivision\Import\Serializer\SerializerInterface
44
     */
45
    protected $serializer;
46
47 2
    /**
48
     * The serializer factory instance.
49
     *
50
     * @var \TechDivision\Import\Serializer\SerializerFactoryInterface
51 2
     */
52 2
    protected $serializerFactory;
53
54
    /**
55 2
     * Initialize the subject instance.
56
     *
57
     * @param \TechDivision\Import\Serializer\SerializerFactoryInterface $serializerFactory The serializer factory instance
58 2
     * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector     The state detector instance to use
59
     */
60
    public function __construct(
61
        SerializerFactoryInterface $serializerFactory,
62
        StateDetectorInterface $stateDetector = null
63
    ) {
64
65
        // initialize the bunch processor and the attribute loader instance
66 2
        $this->serializerFactory = $serializerFactory;
67
68
        // pass the state detector to the parent method
69
        parent::__construct($stateDetector);
70 2
    }
71
72 2
    /**
73
     * Will be invoked by the observer visitor when a factory has been defined to create the observer instance.
74 2
     *
75
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
76 2
     *
77
     * @return \TechDivision\Import\Observers\ObserverInterface The observer instance
78 2
     */
79
    public function createObserver(SubjectInterface $subject)
80 2
    {
81
82 2
        // initialize the serializer instance
83
        $this->serializer = $this->serializerFactory->createSerializer($subject->getConfiguration()->getImportAdapter());
0 ignored issues
show
Documentation introduced by
$subject->getConfiguration()->getImportAdapter() is of type object<TechDivision\Impo...ConfigurationInterface>, but the function expects a object<TechDivision\Impo...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84 2
85
        // return the initialized instance
86
        return $this;
87 2
    }
88
89
    /**
90
     * Will be invoked by the action on the events the listener has been registered for.
91 2
     *
92 2
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
93
     *
94
     * @return array The modified row
95
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
96 2
     */
97
    public function handle(SubjectInterface $subject)
98
    {
99 2
100 2
        // initialize the row
101 2
        $this->setSubject($subject);
102 2
        $this->setRow($subject->getRow());
103 2
104 2
        // process the functionality and return the row
105 2
        $this->process();
106 2
107 2
        // return the processed row
108
        return $this->getRow();
109
    }
110
111
    /**
112
     * Process the observer's business logic.
113 2
     *
114
     * @return array The processed row
115
     */
116
    protected function process()
117
    {
118
119
        // query whether or not the row has additional attributes
120
        if ($additionalAttributes = $this->getValue(ColumnKeys::ADDITIONAL_ATTRIBUTES)) {
121
            // load the subject instance
122
            $subject = $this->getSubject();
123
            // explode the additional attributes
124
            $additionalAttributes = $this->serializer->denormalize($additionalAttributes, false);
0 ignored issues
show
Bug introduced by
The method denormalize() does not seem to exist on object<TechDivision\Impo...er\SerializerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
            // iterate over the attributes and append them to the row
126
            foreach ($additionalAttributes as $attributeCode => $optionValue) {
127
                // try to load the appropriate key for the value
128
                if ($subject->hasHeader($attributeCode) === false) {
129
                    $subject->addHeader($attributeCode);
130
                }
131
132
                // append/replace the attribute value
133
                $this->setValue($attributeCode, $optionValue);
134
135
                // add a log message in debug mod
136
                if ($subject->isDebugMode()) {
137
                    $subject->getSystemLogger()->debug(
138
                        sprintf(
139
                            'Extract new column "%s" with value "%s" from column "%s" in file %s on line %d',
140
                            $attributeCode,
141
                            $optionValue,
142
                            ColumnKeys::ADDITIONAL_ATTRIBUTES,
143
                            $subject->getFilename(),
144
                            $subject->getLineNumber()
145
                        )
146
                    );
147
                }
148
            }
149
        }
150
    }
151
}
152