Completed
Pull Request — master (#89)
by Tim
07:38
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\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 1
    public function getAttributeCode()
73
    {
74 1
        return $this->attributeCode;
75
    }
76
77
    /**
78
     * The attribute value that has to be processed.
79
     *
80
     * @return string The attribute value
81
     */
82 1
    public function getAttributeValue()
83
    {
84 1
        return $this->attributeValue;
85
    }
86
87
    /**
88
     * Remove all the empty values from the row and return the cleared row.
89
     *
90
     * @return array The cleared row
91
     */
92 7
    protected function clearRow()
93
    {
94
95
        // remove all the empty values from the row
96 7
        return array_filter(
97 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...
98 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...
99 7
                return ($value !== null && $value !== '');
100 7
            },
101
            ARRAY_FILTER_USE_BOTH
102 7
        );
103
    }
104
105
    /**
106
     * Process the observer's business logic.
107
     *
108
     * @return void
109
     */
110 7
    protected function process()
111
    {
112
113
        // initialize the store view code
114 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...
115
116
        // load the attributes by the found attribute set and the backend types
117 7
        $attributes = $this->getAttributes();
118 7
        $backendTypes = $this->getBackendTypes();
119
120
        // load the header keys
121 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...
122
123
        // remove all the empty values from the row
124 7
        $row = $this->clearRow();
125
126
        // iterate over the attributes and append them to the row
127 7
        foreach ($row as $key => $attributeValue) {
128
            // query whether or not attribute with the found code exists
129 6
            if (!isset($attributes[$attributeCode = $headers[$key]])) {
130
                // log a message in debug mode
131 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...
132 1
                    $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...
133 1
                        sprintf(
134 1
                            'Can\'t find attribute with attribute code %s in file %s on line %d',
135 1
                            $attributeCode,
136 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...
137 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...
138 1
                        )
139 1
                    );
140 1
                }
141
142
                // stop processing
143 1
                continue;
144
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
145
            } else {
146
                // log a message in debug mode
147 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...
148 2
                    $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...
149 2
                        sprintf(
150 2
                            'Found attribute with attribute code %s in file %s on line %d',
151 2
                            $attributeCode,
152 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...
153 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...
154 2
                        )
155 2
                    );
156 2
                }
157
            }
158
159
            // if yes, load the attribute by its code
160 5
            $attribute = $attributes[$attributeCode];
161
162
            // load the backend type => to find the apropriate entity
163 5
            $backendType = $attribute[MemberNames::BACKEND_TYPE];
164 5
            if ($backendType == null) {
165 1
                $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...
166 1
                    sprintf(
167 1
                        'Found EMTPY backend type for attribute %s in file %s on line %d',
168 1
                        $attributeCode,
169 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...
170 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...
171 1
                    )
172 1
                );
173 1
                continue;
174
            }
175
176
            // do nothing on static backend type
177 4
            if ($backendType === BackendTypeKeys::BACKEND_TYPE_STATIC) {
178 1
                continue;
179
            }
180
181
            // query whether or not we've found a supported backend type
182 3
            if (isset($backendTypes[$backendType])) {
183
                // initialize attribute ID/code and backend type
184 2
                $this->attributeId = $attribute[MemberNames::ATTRIBUTE_ID];
185 2
                $this->attributeCode = $attributeCode;
186 2
                $this->backendType = $backendType;
187
188
                // initialize the persist method for the found backend type
189 2
                list ($persistMethod, ) = $backendTypes[$backendType];
190
191
                // set the attribute value
192 2
                $this->attributeValue = $attributeValue;
193
194
                // try to prepare the attribute values
195 2
                if ($attr = $this->prepareAttributes()) {
196
                    // initialize and persist the attribute
197 1
                    $entity = $this->initializeAttribute($attr);
198 1
                    $this->$persistMethod($entity);
199 1
                }
200
201
                // continue with the next value
202 2
                continue;
203
            }
204
205
            // log the debug message
206 1
            $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...
207 1
                sprintf(
208 1
                    'Found invalid backend type %s for attribute %s in file %s on line %s',
209 1
                    $backendType,
210 1
                    $attributeCode,
211 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...
212 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...
213 1
                )
214 1
            );
215 7
        }
216 7
    }
217
218
    /**
219
     * Prepare the attributes of the entity that has to be persisted.
220
     *
221
     * @return array|null The prepared attributes
222
     */
223 2
    protected function prepareAttributes()
224
    {
225
226
        // laod the callbacks for the actual attribute code
227 2
        $callbacks = $this->getCallbacksByType($this->attributeCode);
228
229
        // invoke the pre-cast callbacks
230
        /** @var \TechDivision\Import\Callbacks\CallbackInterface $callback */
231 2
        foreach ($callbacks as $callback) {
232 1
            $this->attributeValue = $callback->handle($this);
233 2
        }
234
235
        // query whether or not the attribute has been be processed by the callbacks
236 2
        if ($this->attributeValue === null) {
237 1
            return;
238
        }
239
240
        // load the ID of the product that has been created recently
241 1
        $lastEntityId = $this->getPrimaryKey();
242
243
        // load the store ID
244 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...
245
246
        // cast the value based on the backend type
247 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...
248
249
        // prepare the attribute values
250 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...
251
            array(
252 1
                MemberNames::ENTITY_ID    => $lastEntityId,
253 1
                MemberNames::ATTRIBUTE_ID => $this->attributeId,
254 1
                MemberNames::STORE_ID     => $storeId,
255 1
                MemberNames::VALUE        => $castedValue
256 1
            )
257 1
        );
258
    }
259
260
    /**
261
     * Initialize the category product with the passed attributes and returns an instance.
262
     *
263
     * @param array $attr The category product attributes
264
     *
265
     * @return array The initialized category product
266
     */
267 1
    protected function initializeAttribute(array $attr)
268
    {
269 1
        return $attr;
270
    }
271
272
    /**
273
     * Return's the PK to create the product => attribute relation.
274
     *
275
     * @return integer The PK to create the relation with
276
     */
277 1
    protected function getPrimaryKey()
278
    {
279 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...
280
    }
281
282
    /**
283
     * Return's the array with callbacks for the passed type.
284
     *
285
     * @param string $type The type of the callbacks to return
286
     *
287
     * @return array The callbacks
288
     */
289 2
    protected function getCallbacksByType($type)
290
    {
291 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...
292
    }
293
294
    /**
295
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
296
     *
297
     * @return array The mapping for the supported backend types
298
     */
299 7
    protected function getBackendTypes()
300
    {
301 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...
302
    }
303
304
    /**
305
     * Return's the attributes for the attribute set of the product that has to be created.
306
     *
307
     * @return array The attributes
308
     * @throws \Exception
309
     */
310 7
    protected function getAttributes()
311
    {
312 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...
313
    }
314
}
315