Completed
Push — master ( fd8695...cb1565 )
by Tim
20s queued 11s
created

AbstractColumnCollectorObserver::createObserver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\GenericColumnCollectorObserver
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 TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Loaders\LoaderInterface;
25
use TechDivision\Import\Subjects\SubjectInterface;
26
use TechDivision\Import\Interfaces\HookAwareInterface;
27
use TechDivision\Import\Services\RegistryProcessorInterface;
28
use TechDivision\Import\Utils\ColumnKeys;
29
use TechDivision\Import\Utils\StoreViewCodes;
30
31
/**
32
 * Observer that loads configurable data into the registry.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2021 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import
38
 * @link      http://www.techdivision.com
39
 */
40
abstract class AbstractColumnCollectorObserver extends AbstractObserver implements HookAwareInterface, ObserverFactoryInterface
41
{
42
43
    /**
44
     * The loader instance for the custom validations.
45
     *
46
     * @var \TechDivision\Import\Loaders\LoaderInterface
47
     */
48
    private $loader;
49
50
    /**
51
     * The registry processor instance.
52
     *
53
     * @var \TechDivision\Import\Services\RegistryProcessorInterface
54
     */
55
    private $registryProcessor;
56
57
    /**
58
     * The flag to query whether or not the value has to be validated on the main row only.
59
     *
60
     * @var boolean
61
     */
62
    private $mainRowOnly = false;
63
64
    /**
65
     * The array with the column names to assemble the data for.
66
     *
67
     * @var array
68
     */
69
    private $columnNames = array();
70
71
    /**
72
     * The array with the collected column values.
73
     *
74
     * @var array
75
     */
76
    private $values = array();
77
78
    /**
79
     * Initializes the callback with the loader instance.
80
     *
81
     * @param \TechDivision\Import\Loaders\LoaderInterface             $loader            The loader for the validations
82
     * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance
83
     * @param boolean                                                  $mainRowOnly       The flag to decide whether or not only values of the main row has to be
84
     */
85
    public function __construct(
86
        LoaderInterface $loader,
87
        RegistryProcessorInterface $registryProcessor,
88
        bool $mainRowOnly = false
89
    ) {
90
        $this->loader = $loader;
91
        $this->mainRowOnly = $mainRowOnly;
92
        $this->registryProcessor = $registryProcessor;
93
    }
94
95
    /**
96
     * Will be invoked by the observer visitor when a factory has been defined to create the observer instance.
97
     *
98
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
99
     *
100
     * @return \TechDivision\Import\Observers\ObserverInterface The observer instance
101
     */
102
    public function createObserver(SubjectInterface $subject) : ObserverInterface
103
    {
104
105
        // load the names of the columns we want to collect the values for
106
        $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...
107
108
        // return the initialized observer instance
109
        return $this;
110
    }
111
112
    /**
113
     * Will be invoked by the action on the events the listener has been registered for.
114
     *
115
     * @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance
116
     *
117
     * @return array The modified row
118
     * @see \TechDivision\Import\Observers\ObserverInterface::handle()
119
     */
120
    public function handle(SubjectInterface $subject) : array
121
    {
122
123
        // initialize the row
124
        $this->setSubject($subject);
125
        $this->setRow($subject->getRow());
126
127
        // process the functionality and return the row
128
        $this->process();
129
130
        // return the processed row
131
        return $this->getRow();
132
    }
133
134
    /**
135
     * Return's the loader instance for the custom validations.
136
     *
137
     * @return \TechDivision\Import\Loaders\LoaderInterface The loader instance
138
     */
139
    protected function getLoader() : LoaderInterface
140
    {
141
        return $this->loader;
142
    }
143
144
    /**
145
     * Return's the registry processor instance.
146
     *
147
     * @return \TechDivision\Import\Services\RegistryProcessorInterface The processor instance
148
     */
149
    protected function getRegistryProcessor() : RegistryProcessorInterface
150
    {
151
        return $this->registryProcessor;
152
    }
153
154
    /**
155
     * Query whether or not we've to parse the main row only.
156
     *
157
     * @return bool TRUE if only the main row has to be parsed, else FALSE
158
     */
159
    protected function useMainRowOnly() : bool
160
    {
161
        return $this->mainRowOnly;
162
    }
163
164
    /**
165
     * Return's the primary key value that will be used as second incdex.
166
     *
167
     * @return string The primary key to be used
168
     */
169
    abstract protected function getPrimaryKey() : string;
170
171
    /**
172
     * Process the observer's business logic.
173
     *
174
     * @return void
175
     */
176
    protected function process() : void
177
    {
178
179
        // query whether or not we've
180
        // to parse the main row only
181
        if ($this->useMainRowOnly()) {
182
            // load the store view code to figure out if we're on a main row or not
183
            $storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE, StoreViewCodes::DEF);
184
            // query whether or not we're in the
185
            // main row, if not stop processing
186
            if ($storeViewCode !== StoreViewCodes::DEF) {
187
                return;
188
            }
189
        }
190
191
        // load the names of the columns we want to collect the values for
192
        $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...
193
194
        // collect the values using the column name and the primary
195
        // key name as indexes to allow fast access to the values
196
        foreach ($columnNames as $columnName) {
197
            $this->values[$columnName][$this->getPrimaryKey()] = $this->getValue($columnName);
198
        }
199
    }
200
201
    /**
202
     * Intializes the previously loaded global data for exactly one bunch.
203
     *
204
     * @param string $serial The serial of the actual import
205
     *
206
     * @return void
207
     */
208
    public function setUp($serial)
209
    {
210
        // do nothing here
211
    }
212
213
    /**
214
     * Clean up the global data after importing the variants.
215
     *
216
     * @param string $serial The serial of the actual import
217
     *
218
     * @return void
219
     */
220
    public function tearDown($serial)
221
    {
222
223
        // load the registry processor
224
        $this->getRegistryProcessor()->mergeAttributesRecursive(RegistryKeys::COLLECTED_COLUMNS, $this->values);
225
226
        // log a debug message that the observer
227
        // successfully updated the status data
228
        $this->getSystemLogger()->notice(
229
            sprintf(
230
                'Observer "%s" successfully updated status data for "%s" with "%d" rows',
231
                get_class($this),
232
                RegistryKeys::COLLECTED_COLUMNS,
233
                sizeof($this->values)
234
            )
235
        );
236
    }
237
}
238