Completed
Pull Request — master (#5)
by Tim
02:21
created

initializeAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\EntityAttributeUpdateObserver
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 update's the EAV entity 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 EntityAttributeUpdateObserver extends EntityAttributeObserver
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
        // load the attributes
48
        $entityTypeId = $attr[MemberNames::ENTITY_TYPE_ID];
49
        $attributeId = $attr[MemberNames::ATTRIBUTE_ID];
50
        $attributeSetId = $attr[MemberNames::ATTRIBUTE_SET_ID];
51
        $attributeGroupId = $attr[MemberNames::ATTRIBUTE_GROUP_ID];
52
53
        // try to load the entity attribute with the IDs
54
        if ($entityAttribute = $this->loadEntityAttributeByEntityTypeAndAttributeIdAndAttributeSetIdAndAttributeGroupId($entityTypeId, $attributeId, $attributeSetId, $attributeGroupId)) {
55
            return $this->mergeEntity($entityAttribute, $attr);
56
        }
57
58
        // simply return the attributes
59
        return $attr;
60
    }
61
62
    /**
63
     * Return's the EAV entity attribute with the passed entity type, attribute, attribute set and attribute group ID.
64
     *
65
     * @param integer $entityTypeId     The ID of the EAV entity attribute's entity type to return
66
     * @param integer $attributeId      The ID of the EAV entity attribute's attribute to return
67
     * @param integer $attributeSetId   The ID of the EAV entity attribute's attribute set to return
68
     * @param integer $attributeGroupId The ID of the EAV entity attribute's attribute group to return
69
     *
70
     * @return array The EAV entity attribute
71
     */
72
    protected function loadEntityAttributeByEntityTypeAndAttributeIdAndAttributeSetIdAndAttributeGroupId($entityTypeId, $attributeId, $attributeSetId, $attributeGroupId)
73
    {
74
        return $this->getAttributeBunchProcessor()->loadEntityAttributeByEntityTypeAndAttributeIdAndAttributeSetIdAndAttributeGroupId($entityTypeId, $attributeId, $attributeSetId, $attributeGroupId);
75
    }
76
}
77