Completed
Pull Request — master (#101)
by Tim
02:50
created

AttributeObserverTrait::initializeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 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\LoggerKeys;
24
use TechDivision\Import\Utils\MemberNames;
25
use TechDivision\Import\Utils\StoreViewCodes;
26
use TechDivision\Import\Utils\BackendTypeKeys;
27
28
/**
29
 * Observer that creates/updates the EAV 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
trait AttributeObserverTrait
38
{
39
40
    /**
41
     * The ID of the attribute to create the values for.
42
     *
43
     * @var integer
44
     */
45
    protected $attributeId;
46
47
    /**
48
     * The attribute code of the attribute to create the values for.
49
     *
50
     * @var string
51
     */
52
    protected $attributeCode;
53
54
    /**
55
     * The backend type of the attribute to create the values for.
56
     *
57
     * @var string
58
     */
59
    protected $backendType;
60
61
    /**
62
     * The attribute value to process.
63
     *
64
     * @var mixed
65
     */
66
    protected $attributeValue;
67
68
    /**
69
     * The attribute code that has to be processed.
70
     *
71
     * @return string The attribute code
72
     */
73 1
    public function getAttributeCode()
74
    {
75 1
        return $this->attributeCode;
76
    }
77
78
    /**
79
     * The attribute value that has to be processed.
80
     *
81
     * @return string The attribute value
82
     */
83 1
    public function getAttributeValue()
84
    {
85 1
        return $this->attributeValue;
86
    }
87
88
    /**
89
     * Remove all the empty values from the row and return the cleared row.
90
     *
91
     * @return array The cleared row
92
     */
93 7
    protected function clearRow()
94
    {
95
96
        // remove all the empty values from the row
97 7
        return array_filter(
98 7
            $this->row,
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...
99 7
            function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100 7
                return ($value !== null && $value !== '');
101 7
            },
102 7
            ARRAY_FILTER_USE_BOTH
103
        );
104
    }
105
106
    /**
107
     * Process the observer's business logic.
108
     *
109
     * @return void
110
     */
111 7
    protected function process()
112
    {
113
114
        // initialize the store view code
115 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...
116
117
        // load the PK and the store view code
118 7
        $pk = $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...
119 7
        $storeViewCode = $this->getSubject()->getStoreViewCode();
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...
120
121
        // query whether or not the row has already been processed
122 7
        if ($this->storeViewHasBeenProcessed($pk, $storeViewCode)) {
123
            // log a message
124
            $this->getSystemLogger()
125
                 ->warning(
126
                     sprintf(
127
                         'Attributes for %s + store view code "%s" + "%s" has already been processed',
128
                         $this->getPrimaryKeyColumnName(),
129
                         $pk,
130
                         $storeViewCode
131
                     )
132
                 );
133
134
            // return immediately
135
            return;
136
        }
137
138
        // load the attributes by the found attribute set and the backend types
139 7
        $attributes = $this->getAttributes();
140 7
        $backendTypes = $this->getBackendTypes();
141
142
        // load the header keys
143 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...
144
145
        // remove all the empty values from the row
146 7
        $row = $this->clearRow();
147
148
        // iterate over the attributes and append them to the row
149 7
        foreach ($row as $key => $attributeValue) {
150
            // query whether or not attribute with the found code exists
151 6
            if (!isset($attributes[$attributeCode = $headers[$key]])) {
152
                // log a message in debug mode
153 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...
154 1
                    $this->getSystemLogger()->debug(
155 1
                        sprintf(
156 1
                            'Can\'t find attribute with attribute code %s in file %s on line %d',
157 1
                            $attributeCode,
158 1
                            $this->getFilename(),
0 ignored issues
show
Bug introduced by
It seems like getFilename() 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...
159 1
                            $this->getLineNumber()
0 ignored issues
show
Bug introduced by
It seems like getLineNumber() 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...
160
                        )
161
                    );
162
                }
163
164
                // stop processing
165 1
                continue;
166
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
167
            } else {
168
                // log a message in debug mode
169 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...
170 2
                    $this->getSystemLogger()->debug(
171 2
                        sprintf(
172 2
                            'Found attribute with attribute code %s in file %s on line %d',
173 2
                            $attributeCode,
174 2
                            $this->getFilename(),
0 ignored issues
show
Bug introduced by
It seems like getFilename() 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...
175 2
                            $this->getLineNumber()
0 ignored issues
show
Bug introduced by
It seems like getLineNumber() 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...
176
                        )
177
                    );
178
                }
179
            }
180
181
            // if yes, load the attribute by its code
182 5
            $attribute = $attributes[$attributeCode];
183
184
            // load the backend type => to find the apropriate entity
185 5
            $backendType = $attribute[MemberNames::BACKEND_TYPE];
186 5
            if ($backendType == null) {
187 1
                $this->getSystemLogger()->warning(
188 1
                    sprintf(
189 1
                        'Found EMTPY backend type for attribute %s in file %s on line %d',
190 1
                        $attributeCode,
191 1
                        $this->getFilename(),
0 ignored issues
show
Bug introduced by
It seems like getFilename() 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...
192 1
                        $this->getLineNumber()
0 ignored issues
show
Bug introduced by
It seems like getLineNumber() 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...
193
                    )
194
                );
195 1
                continue;
196
            }
197
198
            // do nothing on static backend type
199 4
            if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) {
200 1
                continue;
201
            }
202
203
            // query whether or not we've found a supported backend type
204 3
            if (isset($backendTypes[$backendType])) {
205
                // initialize attribute ID/code and backend type
206 2
                $this->attributeId = $attribute[MemberNames::ATTRIBUTE_ID];
207 2
                $this->attributeCode = $attributeCode;
208 2
                $this->backendType = $backendType;
209
210
                // initialize the persist method for the found backend type
211 2
                list ($persistMethod, ) = $backendTypes[$backendType];
212
213
                // set the attribute value
214 2
                $this->attributeValue = $attributeValue;
215
216
                // try to prepare the attribute values
217 2
                if ($attr = $this->prepareAttributes()) {
218
                    // initialize and persist the attribute
219 1
                    $entity = $this->initializeAttribute($attr);
220 1
                    $this->$persistMethod($entity);
221
                }
222
223
                // continue with the next value
224 2
                continue;
225
            }
226
227
            // log the debug message
228 1
            $this->getSystemLogger()->debug(
229 1
                sprintf(
230 1
                    'Found invalid backend type %s for attribute %s in file %s on line %s',
231 1
                    $backendType,
232 1
                    $attributeCode,
233 1
                    $this->getFilename(),
0 ignored issues
show
Bug introduced by
It seems like getFilename() 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 1
                    $this->getLineNumber()
0 ignored issues
show
Bug introduced by
It seems like getLineNumber() 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...
235
                )
236
            );
237
        }
238 7
    }
239
240
    /**
241
     * Prepare the attributes of the entity that has to be persisted.
242
     *
243
     * @return array|null The prepared attributes
244
     */
245 2
    protected function prepareAttributes()
246
    {
247
248
        // laod the callbacks for the actual attribute code
249 2
        $callbacks = $this->getCallbacksByType($this->attributeCode);
250
251
        // invoke the pre-cast callbacks
252
        /** @var \TechDivision\Import\Callbacks\CallbackInterface $callback */
253 2
        foreach ($callbacks as $callback) {
254 1
            $this->attributeValue = $callback->handle($this);
255
        }
256
257
        // query whether or not the attribute has been be processed by the callbacks
258 2
        if ($this->attributeValue === null) {
259 1
            return;
260
        }
261
262
        // load the ID of the product that has been created recently
263 1
        $lastEntityId = $this->getPrimaryKey();
264
265
        // load the store ID
266 1
        $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...
267
268
        // cast the value based on the backend type
269 1
        $castedValue = $this->castValueByBackendType($this->backendType, $this->attributeValue);
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...
270
271
        // prepare the attribute values
272 1
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
It seems like initializeEntity() 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...
273
            array(
274 1
                MemberNames::ENTITY_ID    => $lastEntityId,
275 1
                MemberNames::ATTRIBUTE_ID => $this->attributeId,
276 1
                MemberNames::STORE_ID     => $storeId,
277 1
                MemberNames::VALUE        => $castedValue
278
            )
279
        );
280
    }
281
282
    /**
283
     * Initialize the category product with the passed attributes and returns an instance.
284
     *
285
     * @param array $attr The category product attributes
286
     *
287
     * @return array The initialized category product
288
     */
289 1
    protected function initializeAttribute(array $attr)
290
    {
291 1
        return $attr;
292
    }
293
294
    /**
295
     * Return's the PK to create the product => attribute relation.
296
     *
297
     * @return integer The PK to create the relation with
298
     */
299 1
    protected function getPrimaryKey()
300
    {
301 1
        return $this->getLastEntityId();
0 ignored issues
show
Bug introduced by
It seems like getLastEntityId() 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...
302
    }
303
304
    /**
305
     * Return's the array with callbacks for the passed type.
306
     *
307
     * @param string $type The type of the callbacks to return
308
     *
309
     * @return array The callbacks
310
     */
311 2
    protected function getCallbacksByType($type)
312
    {
313 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...
314
    }
315
316
    /**
317
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
318
     *
319
     * @return array The mapping for the supported backend types
320
     */
321 7
    protected function getBackendTypes()
322
    {
323 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...
324
    }
325
326
    /**
327
     * Return's the attributes for the attribute set of the product that has to be created.
328
     *
329
     * @return array The attributes
330
     * @throws \Exception
331
     */
332 7
    protected function getAttributes()
333
    {
334 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...
335
    }
336
337
    /**
338
     * Return's the logger with the passed name, by default the system logger.
339
     *
340
     * @param string $name The name of the requested system logger
341
     *
342
     * @return \Psr\Log\LoggerInterface The logger instance
343
     * @throws \Exception Is thrown, if the requested logger is NOT available
344
     */
345
    abstract protected function getSystemLogger($name = LoggerKeys::SYSTEM);
346
347
    /**
348
     * Return's the column name that contains the primary key.
349
     *
350
     * @return string the column name that contains the primary key
351
     */
352
    abstract protected function getPrimaryKeyColumnName();
353
354
    /**
355
     * Queries whether or not the passed PK and store view code has already been processed.
356
     *
357
     * @param string $pk            The PK to check been processed
358
     * @param string $storeViewCode The store view code to check been processed
359
     *
360
     * @return boolean TRUE if the PK and store view code has been processed, else FALSE
361
     */
362
    abstract protected function storeViewHasBeenProcessed($pk, $storeViewCode);
363
}
364