Completed
Push — master ( 3a08fe...aee93b )
by Tim
18:05 queued 16:13
created

AttributeObserverTrait   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 663
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80.71%

Importance

Changes 0
Metric Value
wmc 50
lcom 1
cbo 1
dl 0
loc 663
ccs 113
cts 140
cp 0.8071
rs 8.337
c 0
b 0
f 0

32 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeCode() 0 4 1
A getAttributeValue() 0 4 1
A getPrimaryKeyValue() 0 4 1
A initializeAttribute() 0 4 1
getAttributesByPrimaryKeyAndStoreId() 0 1 ?
getSystemLogger() 0 1 ?
getPrimaryKey() 0 1 ?
getPrimaryKeyMemberName() 0 1 ?
getPrimaryKeyColumnName() 0 1 ?
storeViewHasBeenProcessed() 0 1 ?
persistVarcharAttribute() 0 1 ?
persistIntAttribute() 0 1 ?
persistDecimalAttribute() 0 1 ?
persistDatetimeAttribute() 0 1 ?
persistTextAttribute() 0 1 ?
deleteDatetimeAttribute() 0 1 ?
deleteDecimalAttribute() 0 1 ?
deleteIntAttribute() 0 1 ?
deleteTextAttribute() 0 1 ?
deleteVarcharAttribute() 0 1 ?
hasChanges() 0 1 ?
hasValue() 0 1 ?
A getEmptyAttributeValueConstant() 0 4 1
C clearRow() 0 56 13
C process() 0 158 14
A prepareAttributes() 0 20 1
A loadRawEntity() 0 23 5
A initializeEntity() 0 16 4
A mergeEntity() 0 29 5
A getCallbacksByType() 0 4 1
A getBackendTypes() 0 4 1
A getAttributes() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like AttributeObserverTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AttributeObserverTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * TechDivision\Import\Observers\AttributeObserverTrait
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 2020 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\LoggerKeys;
24
use TechDivision\Import\Utils\MemberNames;
25
use TechDivision\Import\Utils\StoreViewCodes;
26
use TechDivision\Import\Utils\OperationNames;
27
use TechDivision\Import\Utils\BackendTypeKeys;
28
use TechDivision\Import\Utils\ConfigurationKeys;
29
30
/**
31
 * Observer that creates/updates the EAV attributes.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2020 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
trait AttributeObserverTrait
40
{
41
42
    /**
43
     * The ID of the attribute to create the values for.
44
     *
45
     * @var integer
46
     */
47
    protected $attributeId;
48
49
    /**
50
     * The attribute code of the attribute to create the values for.
51
     *
52
     * @var string
53
     */
54
    protected $attributeCode;
55
56
    /**
57
     * The backend type of the attribute to create the values for.
58
     *
59
     * @var string
60
     */
61
    protected $backendType;
62
63
    /**
64
     * The attribute value in process.
65
     *
66
     * @var mixed
67
     */
68
    protected $attributeValue;
69
70
    /**
71
     * The array with the column keys that has to be cleaned up when their values are empty.
72
     *
73
     * @var array
74
     */
75
    protected $cleanUpEmptyColumnKeys;
76
77
    /**
78
     * The array with the default column values.
79
     *
80
     * @var array
81
     */
82
    protected $defaultColumnValues;
83
84
    /**
85
     * The attribute we're actually processing.
86
     *
87
     * @var array
88
     */
89
    protected $attribute;
90
91
    /**
92
     * The entity's existing attribues.
93
     *
94
     * @var array
95
     */
96
    protected $attributes;
97
98
    /**
99
     * The operation that has to be executed to update the attribute.
100
     *
101
     * @var string
102
     */
103
    protected $operation;
104
105
    /**
106
     * The attribute code that has to be processed.
107
     *
108
     * @return string The attribute code
109
     */
110 1
    public function getAttributeCode()
111
    {
112 1
        return $this->attributeCode;
113
    }
114
115
    /**
116
     * The attribute value that has to be processed.
117
     *
118
     * @return string The attribute value
119
     */
120 1
    public function getAttributeValue()
121
    {
122 1
        return $this->attributeValue;
123
    }
124
125
    /**
126
     * Get empty attribute value constant from global konfiguration
127
     *
128
     * @return string
129
     */
130 7
    private function getEmptyAttributeValueConstant()
131
    {
132 7
        return $this->getSubject()->getConfiguration()->getConfiguration()->getEmptyAttributeValueConstant();
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
133
    }
134
135
    /**
136
     * Remove all the empty values from the row and return the cleared row.
137
     *
138
     * @return array The cleared row
139
     */
140 7
    protected function clearRow()
141
    {
142
143
        // initialize the array with the column keys that has to be cleaned-up
144 7
        $this->cleanUpEmptyColumnKeys = array();
145
146
        // query whether or not column names that has to be cleaned up have been configured
147 7
        if ($this->getSubject()->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS)) {
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
148
            // if yes, load the column names
149
            $cleanUpEmptyColumns = $this->getSubject()->getCleanUpColumns();
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
150
151
            // translate the column names into column keys
152
            foreach ($cleanUpEmptyColumns as $cleanUpEmptyColumn) {
153
                if ($this->hasHeader($cleanUpEmptyColumn)) {
0 ignored issues
show
Bug introduced by
It seems like hasHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
154
                    $this->cleanUpEmptyColumnKeys[$cleanUpEmptyColumn] = $this->getHeader($cleanUpEmptyColumn);
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
155
                }
156
            }
157
        }
158
159
        // initialize the array with the default column values
160 7
        $this->defaultColumnValues = array();
161
162
        // iterate over the default column values to figure out whether or not the column exists
163 7
        $defaultColumnValues = $this->getSubject()->getDefaultColumnValues();
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
164
165
        // prepare the array with the default column values, BUT we only take
166
        // care of default columns WITHOUT any value, because in only in this
167
        // case the default EAV value from the DB should be used when a empty
168
        // column value has been found to create a NEW attribute value
169 7
        foreach ($defaultColumnValues as $columnName => $defaultColumnValue) {
170
            if ($defaultColumnValue === '') {
171
                $this->defaultColumnValues[$columnName] = $this->getHeader($columnName);
0 ignored issues
show
Bug introduced by
It seems like getHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
172
            }
173
        }
174
175 7
        $emptyValueDefinition = $this->getEmptyAttributeValueConstant();
176
        // load the header keys
177 7
        $headers = in_array($emptyValueDefinition, $this->row, true) ? array_flip($this->getHeaders()) : [];
0 ignored issues
show
Bug introduced by
The property row does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like getHeaders() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
178
        // remove all the empty values from the row, expected the columns has to be cleaned-up
179 7
        foreach ($this->row as $key => $value) {
180 7
            if ($value === $emptyValueDefinition) {
181
                $this->cleanUpEmptyColumnKeys[$headers[$key]] = $key;
182
                $this->row[$key] = '';
183
            }
184
            // query whether or not the value is empty AND the column has NOT to be cleaned-up
185 7
            if (($value === null || $value === '') &&
186 7
                in_array($key, $this->cleanUpEmptyColumnKeys) === false &&
187 7
                in_array($key, $this->defaultColumnValues) === false
188
            ) {
189 1
                unset($this->row[$key]);
190
            }
191
        }
192
193
        // finally return the clean row
194 7
        return $this->row;
195
    }
196
197
    /**
198
     * Returns the value(s) of the primary key column(s). As the primary key column can
199
     * also consist of two columns, the return value can be an array also.
200
     *
201
     * @return mixed The primary key value(s)
202
     */
203 7
    protected function getPrimaryKeyValue()
204
    {
205 7
        return $this->getValue($this->getPrimaryKeyColumnName());
0 ignored issues
show
Bug introduced by
It seems like getValue() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
206
    }
207
208
    /**
209
     * Process the observer's business logic.
210
     *
211
     * @return void
212
     */
213 7
    protected function process()
214
    {
215
216
        // initialize the store view code
217 7
        $this->prepareStoreViewCode();
0 ignored issues
show
Bug introduced by
It seems like prepareStoreViewCode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
218
219
        // load the store ID, use the admin store if NO store view code has been set
220 7
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
0 ignored issues
show
Bug introduced by
It seems like getRowStoreId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
221
222
        // load the entity's existing attributes
223 7
        $this->getAttributesByPrimaryKeyAndStoreId($this->getPrimaryKey(), $storeId);
224
225
        // load the store view - if no store view has been set, we assume the admin
226
        // store view, which will contain the default (fallback) attribute values
227 7
        $storeViewCode = $this->getSubject()->getStoreViewCode(StoreViewCodes::ADMIN);
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
228
229
        // query whether or not the row has already been processed
230 7
        if ($this->storeViewHasBeenProcessed($pk = $this->getPrimaryKeyValue(), $storeViewCode)) {
231
            // log a message
232
            $this->getSystemLogger()->warning(
233
                $this->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like appendExceptionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
234
                    sprintf(
235
                        'Attributes for "%s" "%s" + store view code "%s" has already been processed',
236
                        $this->getPrimaryKeyColumnName(),
237
                        $pk,
238
                        $storeViewCode
239
                    )
240
                )
241
            );
242
243
            // return immediately
244
            return;
245
        }
246
247
        // load the attributes by the found attribute set and the backend types
248 7
        $attributes = $this->getAttributes();
249 7
        $backendTypes = $this->getBackendTypes();
250
251
        // load the header keys
252 7
        $headers = array_flip($this->getHeaders());
0 ignored issues
show
Bug introduced by
It seems like getHeaders() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
253
254
        // remove all the empty values from the row
255 7
        $row = $this->clearRow();
256
257
        // iterate over the attributes and append them to the row
258 7
        foreach ($row as $key => $attributeValue) {
259
            // query whether or not attribute with the found code exists
260 6
            if (!isset($attributes[$attributeCode = $headers[$key]])) {
261
                // log a message in debug mode
262 1
                if ($this->isDebugMode()) {
0 ignored issues
show
Bug introduced by
It seems like isDebugMode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
263 1
                    $this->getSystemLogger()->debug(
264 1
                        $this->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like appendExceptionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
265 1
                            sprintf(
266 1
                                'Can\'t find attribute with attribute code "%s"',
267 1
                                $attributeCode
268
                            )
269
                        )
270
                    );
271
                }
272
273
                // stop processing
274 1
                continue;
275
            } else {
276
                // log a message in debug mode
277 5
                if ($this->isDebugMode()) {
0 ignored issues
show
Bug introduced by
It seems like isDebugMode() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
278
                // log a message in debug mode
279 2
                    $this->getSystemLogger()->debug(
280 2
                        $this->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like appendExceptionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
281 2
                            sprintf(
282 2
                                'Found attribute with attribute code "%s"',
283 2
                                $attributeCode
284
                            )
285
                        )
286
                    );
287
                }
288
            }
289
290
            // if yes, load the attribute by its code
291 5
            $this->attribute = $attributes[$attributeCode];
292
293
            // load the backend type => to find the apropriate entity
294 5
            $backendType = $this->attribute[MemberNames::BACKEND_TYPE];
295 5
            if ($backendType === null) {
296
                // log a message in debug mode
297 1
                $this->getSystemLogger()->warning(
298 1
                    $this->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like appendExceptionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
299 1
                        sprintf(
300 1
                            'Found EMTPY backend type for attribute "%s"',
301 1
                            $attributeCode
302
                        )
303
                    )
304
                );
305
                // stop processing
306 1
                continue;
307
            }
308
309
            // do nothing on static backend type
310 4
            if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) {
311 1
                continue;
312
            }
313
314
            // query whether or not we've found a supported backend type
315 3
            if (isset($backendTypes[$backendType])) {
316
                // initialize attribute ID/code and backend type
317 2
                $this->backendType = $backendType;
318 2
                $this->attributeCode = $attributeCode;
319 2
                $this->attributeId = $this->attribute[MemberNames::ATTRIBUTE_ID];
320
321
                // set the attribute value as well as the original attribute value
322 2
                $this->attributeValue = $attributeValue;
323
324
                // initialize the persist method for the found backend type
325 2
                list ($persistMethod, , $deleteMethod) = $backendTypes[$backendType];
326
327
                // prepare the attribute vale and query whether or not it has to be persisted
328 2
                if ($this->hasChanges($value = $this->initializeAttribute($this->prepareAttributes()))) {
0 ignored issues
show
Bug introduced by
It seems like $this->prepareAttributes() targeting TechDivision\Import\Obse...it::prepareAttributes() can also be of type null; however, TechDivision\Import\Obse...::initializeAttribute() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
329
                    // query whether or not the entity's value has to be persisted or deleted. if the value is
330
                    // an empty string and the status is UPDATE, then the value exists and has to be deleted
331
                    // We need to user $attributeValue instead of $value[MemberNames::VALUE] in cases where
332
                    // value was casted by attribute type. E.g. special_price = 0 if value is empty string in CSV
333 2
                    switch ($this->operation) {
334
                        // create/update the attribute
335 2
                        case OperationNames::CREATE:
336 1
                        case OperationNames::UPDATE:
337 1
                            $this->$persistMethod($value);
338 1
                            break;
339
                        // delete the attribute
340 1
                        case OperationNames::DELETE:
341
                            $this->$deleteMethod(array(MemberNames::VALUE_ID => $value[MemberNames::VALUE_ID]));
342
                            break;
343
                        // skip the attribute
344 1
                        case OperationNames::SKIP:
345 1
                            $this->getSubject()->getSystemLogger()->debug(sprintf('Skipped processing attribute "%s"', $attributeCode));
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
346 1
                            break;
347
                        // should never happen
348
                        default:
349 2
                            $this->getSubject()->getSystemLogger()->debug(sprintf('Found invalid entity status "%s" for attribute "%s"', $value[MemberNames::VALUE] ?? 'NULL', $attributeCode));
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
350
                    }
351
                } else {
352
                    $this->getSubject()->getSystemLogger()->debug(sprintf('Skip to persist value for attribute "%s"', $attributeCode));
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
353
                }
354
355
                // continue with the next value
356 2
                continue;
357
            }
358
359
            // log the debug message
360 1
            $this->getSystemLogger()->debug(
361 1
                $this->getSubject()->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
362 1
                    sprintf(
363 1
                        'Found invalid backend type %s for attribute "%s"',
364 1
                        $backendType,
365 1
                        $attributeCode
366
                    )
367
                )
368
            );
369
        }
370 7
    }
371
372
    /**
373
     * Prepare the attributes of the entity that has to be persisted.
374
     *
375
     * @return array|null The prepared attributes
376
     */
377 2
    protected function prepareAttributes()
378
    {
379
380
        // load the ID of the product that has been created recently
381 2
        $lastEntityId = $this->getPrimaryKey();
382
383
        // load the store ID, use the admin store if NO store view code has been set
384 2
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
0 ignored issues
show
Bug introduced by
It seems like getRowStoreId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
385
386
        // prepare the attribute values
387 2
        return $this->initializeEntity(
388 2
            $this->loadRawEntity(
389
                array(
390 2
                   $this->getPrimaryKeyMemberName() => $lastEntityId,
391 2
                    MemberNames::ATTRIBUTE_ID       => $this->attributeId,
392 2
                    MemberNames::STORE_ID           => $storeId
393
                )
394
            )
395
        );
396
    }
397
398
    /**
399
     * Initialize the category product with the passed attributes and returns an instance.
400
     *
401
     * @param array $attr The category product attributes
402
     *
403
     * @return array The initialized category product
404
     */
405 2
    protected function initializeAttribute(array $attr)
406
    {
407 2
        return $attr;
408
    }
409
410
    /**
411
     * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values.
412
     *
413
     * @param array $data An array with data that will be used to initialize the raw entity with
414
     *
415
     * @return array The initialized entity
416
     */
417 2
    protected function loadRawEntity(array $data = array())
418
    {
419
420
        // laod the callbacks for the actual attribute code
421 2
        $callbacks = $this->getCallbacksByType($this->attributeCode);
422
423
        // invoke the pre-cast callbacks
424 2
        foreach ($callbacks as $callback) {
425 1
            $this->attributeValue = $callback->handle($this);
426
        }
427
428
        // load the default value
429 2
        $defaultValue = isset($this->attribute[MemberNames::DEFAULT_VALUE]) ? $this->attribute[MemberNames::DEFAULT_VALUE] : '';
430
431
        // load the value that has to be casted
432 2
        $value = $this->attributeValue === '' || $this->attributeValue === null ? $defaultValue : $this->attributeValue;
433
434
        // cast the value
435 2
        $castedValue = $this->castValueByBackendType($this->backendType, $value);
0 ignored issues
show
Bug introduced by
It seems like castValueByBackendType() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
436
437
        // merge the casted value into the passed data and return it
438 2
        return array_merge(array(MemberNames::VALUE => $castedValue), $data);
439
    }
440
441
    /**
442
     * Initialize's and return's a new entity with the status 'create'.
443
     *
444
     * @param array $attr The attributes to merge into the new entity
445
     *
446
     * @return array The initialized entity
447
     */
448 2
    protected function initializeEntity(array $attr = array())
449
    {
450
451
        // initialize the operation name
452 2
        $this->operation = OperationNames::CREATE;
453
454
        // query whether or not the colunm IS empty and it is NOT in the
455
        // array with the default column values, because in that case we
456
        // want to skip processing the attribute
457 2
        if (array_key_exists($this->attributeCode, $this->defaultColumnValues) === false && ($this->attributeValue === '' || $this->attributeValue == null)) {
458 1
            $this->operation = OperationNames::SKIP;
459
        }
460
461
        // initialize the entity with the passed data
462 2
        return parent::initializeEntity($attr);
463
    }
464
465
    /**
466
     * Merge's and return's the entity with the passed attributes and set's the
467
     * passed status.
468
     *
469
     * @param array       $entity        The entity to merge the attributes into
470
     * @param array       $attr          The attributes to be merged
471
     * @param string|null $changeSetName The change set name to use
472
     *
473
     * @return array The merged entity
474
     */
475
    protected function mergeEntity(array $entity, array $attr, $changeSetName = null)
476
    {
477
478
        // we want to update the attribute, if we're here
479
        $this->operation = OperationNames::UPDATE;
480
481
        // query whether or not the column is EMPTY
482
        if ($this->attributeValue === '' || $this->attributeValue === null) {
483
            // if the value is empty AND it is IN the array with default column values
484
            // BUT it is NOT in the array with columns we want to clean-up the default
485
            // column value has to be removed, because we do NOT want to override the
486
            // value existing in the database
487
            if (array_key_exists($this->attributeCode, $this->defaultColumnValues) &&
488
                array_key_exists($this->attributeCode, $this->cleanUpEmptyColumnKeys) === false
489
            ) {
490
                // remove the value from the array with the column values, because
491
                // this is the default value from the database and it should NOT
492
                // override the value from the entity in that case
493
                unset($attr[MemberNames::VALUE]);
494
            } else {
495
                // otherwise keep the value and DELETE the whole attribute from
496
                // the database
497
                $this->operation = OperationNames::DELETE;
498
            }
499
        }
500
501
        // merge and return the data
502
        return parent::mergeEntity($entity, $attr, $changeSetName);
503
    }
504
505
    /**
506
     * Return's the array with callbacks for the passed type.
507
     *
508
     * @param string $type The type of the callbacks to return
509
     *
510
     * @return array The callbacks
511
     */
512 2
    protected function getCallbacksByType($type)
513
    {
514 2
        return $this->getSubject()->getCallbacksByType($type);
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
515
    }
516
517
    /**
518
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
519
     *
520
     * @return array The mapping for the supported backend types
521
     */
522 7
    protected function getBackendTypes()
523
    {
524 7
        return $this->getSubject()->getBackendTypes();
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
525
    }
526
527
    /**
528
     * Return's the attributes for the attribute set of the product that has to be created.
529
     *
530
     * @return array The attributes
531
     * @throws \Exception
532
     */
533 7
    protected function getAttributes()
534
    {
535 7
        return $this->getSubject()->getAttributes();
0 ignored issues
show
Bug introduced by
It seems like getSubject() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
536
    }
537
538
    /**
539
     * Intializes the existing attributes for the entity with the passed primary key.
540
     *
541
     * @param string  $pk      The primary key of the entity to load the attributes for
542
     * @param integer $storeId The ID of the store view to load the attributes for
543
     *
544
     * @return array The entity attributes
545
     */
546
    abstract protected function getAttributesByPrimaryKeyAndStoreId($pk, $storeId);
547
548
    /**
549
     * Return's the logger with the passed name, by default the system logger.
550
     *
551
     * @param string $name The name of the requested system logger
552
     *
553
     * @return \Psr\Log\LoggerInterface The logger instance
554
     * @throws \Exception Is thrown, if the requested logger is NOT available
555
     */
556
    abstract protected function getSystemLogger($name = LoggerKeys::SYSTEM);
557
558
    /**
559
     * Return's the PK to create the product => attribute relation.
560
     *
561
     * @return integer The PK to create the relation with
562
     */
563
    abstract protected function getPrimaryKey();
564
565
    /**
566
     * Return's the PK column name to create the product => attribute relation.
567
     *
568
     * @return string The PK column name
569
     */
570
    abstract protected function getPrimaryKeyMemberName();
571
572
    /**
573
     * Return's the column name that contains the primary key.
574
     *
575
     * @return string the column name that contains the primary key
576
     */
577
    abstract protected function getPrimaryKeyColumnName();
578
579
    /**
580
     * Queries whether or not the passed PK and store view code has already been processed.
581
     *
582
     * @param string $pk            The PK to check been processed
583
     * @param string $storeViewCode The store view code to check been processed
584
     *
585
     * @return boolean TRUE if the PK and store view code has been processed, else FALSE
586
     */
587
    abstract protected function storeViewHasBeenProcessed($pk, $storeViewCode);
588
589
    /**
590
     * Persist's the passed varchar attribute.
591
     *
592
     * @param array $attribute The attribute to persist
593
     *
594
     * @return void
595
     */
596
    abstract protected function persistVarcharAttribute($attribute);
597
598
    /**
599
     * Persist's the passed integer attribute.
600
     *
601
     * @param array $attribute The attribute to persist
602
     *
603
     * @return void
604
     */
605
    abstract protected function persistIntAttribute($attribute);
606
607
    /**
608
     * Persist's the passed decimal attribute.
609
     *
610
     * @param array $attribute The attribute to persist
611
     *
612
     * @return void
613
     */
614
    abstract protected function persistDecimalAttribute($attribute);
615
616
    /**
617
     * Persist's the passed datetime attribute.
618
     *
619
     * @param array $attribute The attribute to persist
620
     *
621
     * @return void
622
     */
623
    abstract protected function persistDatetimeAttribute($attribute);
624
625
    /**
626
     * Persist's the passed text attribute.
627
     *
628
     * @param array $attribute The attribute to persist
629
     *
630
     * @return void
631
     */
632
    abstract protected function persistTextAttribute($attribute);
633
634
    /**
635
     * Delete's the datetime attribute with the passed value ID.
636
     *
637
     * @param array       $row  The attributes of the entity to delete
638
     * @param string|null $name The name of the prepared statement that has to be executed
639
     *
640
     * @return void
641
     */
642
    abstract protected function deleteDatetimeAttribute(array $row, $name = null);
643
644
    /**
645
     * Delete's the decimal attribute with the passed value ID.
646
     *
647
     * @param array       $row  The attributes of the entity to delete
648
     * @param string|null $name The name of the prepared statement that has to be executed
649
     *
650
     * @return void
651
     */
652
    abstract protected function deleteDecimalAttribute(array $row, $name = null);
653
654
    /**
655
     * Delete's the integer attribute with the passed value ID.
656
     *
657
     * @param array       $row  The attributes of the entity to delete
658
     * @param string|null $name The name of the prepared statement that has to be executed
659
     *
660
     * @return void
661
     */
662
    abstract protected function deleteIntAttribute(array $row, $name = null);
663
664
    /**
665
     * Delete's the text attribute with the passed value ID.
666
     *
667
     * @param array       $row  The attributes of the entity to delete
668
     * @param string|null $name The name of the prepared statement that has to be executed
669
     *
670
     * @return void
671
     */
672
    abstract protected function deleteTextAttribute(array $row, $name = null);
673
674
    /**
675
     * Delete's the varchar attribute with the passed value ID.
676
     *
677
     * @param array       $row  The attributes of the entity to delete
678
     * @param string|null $name The name of the prepared statement that has to be executed
679
     *
680
     * @return void
681
     */
682
    abstract protected function deleteVarcharAttribute(array $row, $name = null);
683
684
    /**
685
     * Query whether or not the entity has to be processed.
686
     *
687
     * @param array $entity The entity to query for
688
     *
689
     * @return boolean TRUE if the entity has to be processed, else FALSE
690
     */
691
    abstract protected function hasChanges(array $entity);
692
693
    /**
694
     * Query whether or not a value for the column with the passed name exists.
695
     *
696
     * @param string $name The column name to query for a valid value
697
     *
698
     * @return boolean TRUE if the value is set, else FALSE
699
     */
700
    abstract protected function hasValue($name);
701
}
702