Completed
Pull Request — master (#76)
by Tim
02:59
created

AttributeObserverTrait::getAttributeValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\MemberNames;
24
use TechDivision\Import\Utils\StoreViewCodes;
25
use TechDivision\Import\Utils\BackendTypeKeys;
26
27
/**
28
 * Observer that creates/updates the EAV attributes.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
trait AttributeObserverTrait
37
{
38
39
    /**
40
     * The ID of the attribute to create the values for.
41
     *
42
     * @var integer
43
     */
44
    protected $attributeId;
45
46
    /**
47
     * The attribute code of the attribute to create the values for.
48
     *
49
     * @var string
50
     */
51
    protected $attributeCode;
52
53
    /**
54
     * The backend type of the attribute to create the values for.
55
     *
56
     * @var string
57
     */
58
    protected $backendType;
59
60
    /**
61
     * The attribute value to process.
62
     *
63
     * @var mixed
64
     */
65
    protected $attributeValue;
66
67
    /**
68
     * The attribute code that has to be processed.
69
     *
70
     * @return string The attribute code
71
     */
72
    public function getAttributeCode()
73
    {
74
        return $this->attributeCode;
75
    }
76
77
    /**
78
     * The attribute value that has to be processed.
79
     *
80
     * @return string The attribute value
81
     */
82
    public function getAttributeValue()
83
    {
84
        return $this->attributeValue;
85
    }
86
87
    /**
88
     * Process the observer's business logic.
89
     *
90
     * @return void
91
     */
92
    protected function process()
93
    {
94
95
        // initialize the store view code
96
        $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...
97
98
        // load the attributes by the found attribute set and the backend types
99
        $attributes = $this->getAttributes();
100
        $backendTypes = $this->getBackendTypes();
101
102
        // remove all the empty values from the row
103
        $row = array_filter(
104
            $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...
105
            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...
106
                return ($value !== null && $value !== '');
107
            },
108
            ARRAY_FILTER_USE_BOTH
109
        );
110
111
        // load the header keys
112
        $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...
113
114
        // iterate over the attributes and append them to the row
115
        foreach ($row as $key => $attributeValue) {
116
            // query whether or not attribute with the found code exists
117
            if (!isset($attributes[$attributeCode = $headers[$key]])) {
118
                // log a message in debug mode
119
                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...
120
                    $this->getSystemLogger()->debug(
0 ignored issues
show
Bug introduced by
It seems like getSystemLogger() 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...
121
                        sprintf(
122
                            'Can\'t find attribute with attribute code %s in file %s on line %d',
123
                            $attributeCode,
124
                            $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...
125
                            $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...
126
                        )
127
                    );
128
                }
129
130
                // stop processing
131
                continue;
132
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
133
            } else {
134
                // log a message in debug mode
135
                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...
136
                    $this->getSystemLogger()->debug(
0 ignored issues
show
Bug introduced by
It seems like getSystemLogger() 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...
137
                        sprintf(
138
                            'Found attribute with attribute code %s in file %s on line %d',
139
                            $attributeCode,
140
                            $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...
141
                            $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...
142
                        )
143
                    );
144
                }
145
            }
146
147
            // if yes, load the attribute by its code
148
            $attribute = $attributes[$attributeCode];
149
150
            // load the backend type => to find the apropriate entity
151
            $backendType = $attribute[MemberNames::BACKEND_TYPE];
152
            if ($backendType == null) {
153
                $this->getSystemLogger()->warning(
0 ignored issues
show
Bug introduced by
It seems like getSystemLogger() 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
                    sprintf(
155
                        'Found EMTPY backend type for attribute %s in file %s on line %d',
156
                        $attributeCode,
157
                        $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...
158
                        $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...
159
                    )
160
                );
161
                continue;
162
            }
163
164
            // do nothing on static backend type
165
            if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) {
166
                continue;
167
            }
168
169
            // query whether or not we've found a supported backend type
170
            if (isset($backendTypes[$backendType])) {
171
                // initialize attribute ID/code and backend type
172
                $this->attributeId = $attribute[MemberNames::ATTRIBUTE_ID];
173
                $this->attributeCode = $attributeCode;
174
                $this->backendType = $backendType;
175
176
                // initialize the persist method for the found backend type
177
                list ($persistMethod, ) = $backendTypes[$backendType];
178
179
                // set the attribute value
180
                $this->attributeValue = $attributeValue;
181
182
                // try to prepare the attribute values
183
                if ($attr = $this->prepareAttributes()) {
184
                    // initialize and persist the attribute
185
                    $entity = $this->initializeAttribute($attr);
186
                    $this->$persistMethod($entity);
187
                }
188
189
                // continue with the next value
190
                continue;
191
            }
192
193
            // log the debug message
194
            $this->getSystemLogger()->debug(
0 ignored issues
show
Bug introduced by
It seems like getSystemLogger() 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...
195
                sprintf(
196
                    'Found invalid backend type %s for attribute %s in file %s on line %s',
197
                    $this->backendType,
198
                    $this->attributeCode,
199
                    $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...
200
                    $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...
201
                )
202
            );
203
        }
204
    }
205
206
    /**
207
     * Prepare the attributes of the entity that has to be persisted.
208
     *
209
     * @return array|null The prepared attributes
210
     */
211
    protected function prepareAttributes()
212
    {
213
214
        // laod the callbacks for the actual attribute code
215
        $callbacks = $this->getCallbacksByType($this->attributeCode);
216
217
        // invoke the pre-cast callbacks
218
        /** @var \TechDivision\Import\Callbacks\CallbackInterface $callback */
219
        foreach ($callbacks as $callback) {
220
            $this->attributeValue = $callback->handle($this);
221
        }
222
223
        // query whether or not the attribute has been be processed by the callbacks
224
        if ($this->attributeValue === null) {
225
            return;
226
        }
227
228
        // load the ID of the product that has been created recently
229
        $lastEntityId = $this->getPrimaryKey();
230
231
        // load the store ID
232
        $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...
233
234
        // cast the value based on the backend type
235
        $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...
236
237
        // prepare the attribute values
238
        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...
239
            array(
240
                MemberNames::ENTITY_ID    => $lastEntityId,
241
                MemberNames::ATTRIBUTE_ID => $this->attributeId,
242
                MemberNames::STORE_ID     => $storeId,
243
                MemberNames::VALUE        => $castedValue
244
            )
245
        );
246
    }
247
248
    /**
249
     * Initialize the category product with the passed attributes and returns an instance.
250
     *
251
     * @param array $attr The category product attributes
252
     *
253
     * @return array The initialized category product
254
     */
255
    protected function initializeAttribute(array $attr)
256
    {
257
        return $attr;
258
    }
259
260
    /**
261
     * Return's the PK to create the product => attribute relation.
262
     *
263
     * @return integer The PK to create the relation with
264
     */
265
    protected function getPrimaryKey()
266
    {
267
        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...
268
    }
269
270
    /**
271
     * Map the passed attribute code, if a header mapping exists and return the
272
     * mapped mapping.
273
     *
274
     * @param string $attributeCode The attribute code to map
275
     *
276
     * @return string The mapped attribute code, or the original one
277
     */
278
    protected function mapAttributeCodeByHeaderMapping($attributeCode)
279
    {
280
        return $this->getSubject()->mapAttributeCodeByHeaderMapping($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...
281
    }
282
283
    /**
284
     * Return's the array with callbacks for the passed type.
285
     *
286
     * @param string $type The type of the callbacks to return
287
     *
288
     * @return array The callbacks
289
     */
290
    protected function getCallbacksByType($type)
291
    {
292
        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...
293
    }
294
295
    /**
296
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
297
     *
298
     * @return array The mapping for the supported backend types
299
     */
300
    protected function getBackendTypes()
301
    {
302
        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...
303
    }
304
305
    /**
306
     * Return's the attributes for the attribute set of the product that has to be created.
307
     *
308
     * @return array The attributes
309
     * @throws \Exception
310
     */
311
    protected function getAttributes()
312
    {
313
        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...
314
    }
315
}
316