Completed
Pull Request — master (#16)
by Tim
07:10
created

AttributeUpdateObserver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 34
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAttribute() 0 11 2
A loadAttributeByAttributeCode() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeUpdateObserver
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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Observers;
22
23
use TechDivision\Import\Attribute\Utils\MemberNames;
24
25
/**
26
 * Observer that add/update's the EAV attribute itself.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-attribute
32
 * @link      http://www.techdivision.com
33
 */
34
class AttributeUpdateObserver extends AttributeObserver
35
{
36
37
    /**
38
     * Initialize the attribute with the passed attributes and returns an instance.
39
     *
40
     * @param array $attr The attribute attributes
41
     *
42
     * @return array The initialized attribute
43
     */
44
    protected function initializeAttribute(array $attr)
45
    {
46
47
        // try to load the EAV attribute with the attribute code
48
        if ($attribute = $this->loadAttributeByAttributeCode($attr[MemberNames::ATTRIBUTE_CODE])) {
49
            return $this->mergeEntity($attribute, $attr);
50
        }
51
52
        // simply return the attributes
53
        return $attr;
54
    }
55
56
    /**
57
     * Load's and return's the EAV attribute with the passed code.
58
     *
59
     * @param string $attributeCode The code of the EAV attribute to load
60
     *
61
     * @return array The EAV attribute
62
     */
63
    protected function loadAttributeByAttributeCode($attributeCode)
64
    {
65
        return $this->getAttributeBunchProcessor()->loadAttributeByAttributeCode($attributeCode);
66
    }
67
}
68