Completed
Push — master ( aee93b...d79f49 )
by Tim
02:06
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\GenericHookAwareColumnCollectorObserver
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 2021 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 Ramsey\Uuid\Uuid;
24
use TechDivision\Import\Utils\RegistryKeys;
25
use TechDivision\Import\Loaders\LoaderInterface;
26
use TechDivision\Import\Subjects\SubjectInterface;
27
use TechDivision\Import\Interfaces\HookAwareInterface;
28
use TechDivision\Import\Services\RegistryProcessorInterface;
29
30
/**
31
 * Observer that loads configurable data into the registry.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2021 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import
37
 * @link      http://www.techdivision.com
38
 */
39
class GenericHookAwareColumnCollectorObserver extends AbstractObserver implements HookAwareInterface, ObserverFactoryInterface
40
{
41
42
    /**
43
     * The loader instance for the custom validations.
44
     *
45
     * @var \TechDivision\Import\Loaders\LoaderInterface
46
     */
47
    protected $loader;
48
49
    /**
50
     * The registry processor instance.
51
     *
52
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
53
     */
54
    protected $registryProcessor;
55
56
    /**
57
     * The array with the column names to assemble the data for.
58
     *
59
     * @var array
60
     */
61
    protected $columnNames = array();
62
63
    /**
64
     * The array with the collected column values.
65
     *
66
     * @var array
67
     */
68
    protected $values = array();
69
70
    /**
71
     * Initializes the callback with the loader instance.
72
     *
73
     * @param \TechDivision\Import\Loaders\LoaderInterface             $loader            The loader for the validations
74
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance
75
     */
76
    public function __construct(LoaderInterface $loader, RegistryProcessorInterface $registryProcessor)
77
    {
78
        $this->loader = $loader;
79
        $this->registryProcessor = $registryProcessor;
80
    }
81
82
    /**
83
     * Will be invoked by the observer visitor when a factory has been defined to create the observer instance.
84
     *
85
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
86
     *
87
     * @return \TechDivision\Import\Observers\ObserverInterface The observer instance
88
     */
89
    public function createObserver(SubjectInterface $subject)
90
    {
91
92
        // load the names of the columns we want to collect the values for
93
        $this->columnNames = $this->getLoader()->load($subject->getConfiguration());
0 ignored issues
show
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $subject->getConfiguration().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Documentation Bug introduced by
It seems like $this->getLoader()->load...ct->getConfiguration()) of type object<ArrayAccess> is incompatible with the declared type array of property $columnNames.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
95
        // return the initialized observer instance
96
        return $this;
97
    }
98
99
    /**
100
     * Will be invoked by the action on the events the listener has been registered for.
101
     *
102
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
103
     *
104
     * @return array The modified row
105
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
106
     */
107
    public function handle(SubjectInterface $subject)
108
    {
109
110
        // initialize the row
111
        $this->setSubject($subject);
112
        $this->setRow($subject->getRow());
113
114
        // process the functionality and return the row
115
        $this->process();
116
117
        // return the processed row
118
        return $this->getRow();
119
    }
120
121
    /**
122
     * Return's the loader instance for the custom validations.
123
     *
124
     * @return \TechDivision\Import\Loaders\LoaderInterface The loader instance
125
     */
126
    protected function getLoader()
127
    {
128
        return $this->loader;
129
    }
130
131
    /**
132
     * Return's the registry processor instance.
133
     *
134
     * @return \TechDivision\Import\Services\RegistryProcessorInterface The processor instance
135
     */
136
    protected function getRegistryProcessor()
137
    {
138
        return $this->registryProcessor;
139
    }
140
141
    /**
142
     * Process the observer's business logic.
143
     *
144
     * @return array The processed row
145
     * @throws \Exception Is thrown, if the product with the SKU can not be loaded
146
     */
147
    protected function process()
148
    {
149
150
        // load the names of the columns we want to collect the values for
151
        $columnNames = $this->getLoader()->load($this->getSubject()->getConfiguration());
0 ignored issues
show
Unused Code introduced by
The call to LoaderInterface::load() has too many arguments starting with $this->getSubject()->getConfiguration().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
152
153
        // collect the values, keeping in mind, thath the index must be a string
154
        // as later on it will be merged with the function `array_merge_recursive()`
155
        // an values with the same key will be overwritten
156
        foreach ($columnNames as $columnName) {
157
            $this->values[$columnName][Uuid::uuid4()->toString()] = $this->getValue($columnName);
158
        }
159
    }
160
161
    /**
162
     * Intializes the previously loaded global data for exactly one bunch.
163
     *
164
     * @param string $serial The serial of the actual import
165
     *
166
     * @return void
167
     */
168
    public function setUp($serial)
169
    {
170
    }
171
172
    /**
173
     * Clean up the global data after importing the variants.
174
     *
175
     * @param string $serial The serial of the actual import
176
     *
177
     * @return void
178
     */
179
    public function tearDown($serial)
180
    {
181
182
        // load the registry processor
183
        $this->getRegistryProcessor()->mergeAttributesRecursive(RegistryKeys::COLLECTED_COLUMNS, $this->values);
184
185
        // log a debug message that the observer
186
        // successfully updated the status data
187
        $this->getSystemLogger()->notice(
188
            sprintf(
189
                'Observer "%s" successfully updated status data for "%s"',
190
                get_class($this),
191
                RegistryKeys::COLLECTED_COLUMNS
192
            )
193
        );
194
    }
195
}
196