Passed
Pull Request — master (#1)
by Tim
03:10
created

AttributeGroupUpdateObserver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
eloc 9
dl 0
loc 44
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAttribute() 0 20 2
A loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Observers\AttributeGroupUpdateObserver
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 2019 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-set
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Set\Observers;
22
23
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
24
25
/**
26
 * Observer that add/update's the EAV attribute group itself.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 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-set
32
 * @link      http://www.techdivision.com
33
 */
34
class AttributeGroupUpdateObserver extends AttributeGroupObserver
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 entity type code
48
        $entityTypeCode = $this->getEntityTypeCode();
49
50
        // load the last attribute set name
51
        $attributeSet = $this->getLastAttributeSet();
52
        $attributeSetName = $attributeSet[MemberNames::ATTRIBUTE_SET_NAME];
53
54
        // load the attribute group name
55
        $attributeGroupName = $attr[MemberNames::ATTRIBUTE_GROUP_NAME];
56
57
        // query whether or not an EAV attribute group with the given params already exists
58
        if ($entity = $this->loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName)) {
59
            return $this->mergeEntity($entity, $attr);
60
        }
61
62
        // if not, simply return the attributes
63
        return $attr;
64
    }
65
66
    /**
67
     * Return's the EAV attribute group with the passed entity type code, attribute set and attribute group name.
68
     *
69
     * @param string $entityTypeCode     The entity type code of the EAV attribute group to return
70
     * @param string $attributeSetName   The attribute set name of the EAV attribute group to return
71
     * @param string $attributeGroupName The attribute group name of the EAV attribute group to return
72
     *
73
     * @return array The EAV attribute group
74
     */
75
    protected function loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName)
76
    {
77
        return $this->getAttributeSetBunchProcessor()->loadAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName);
78
    }
79
}
80