Passed
Pull Request — master (#4)
by Tim
04:21
created

CopyParentAttributeSetUpdateObserver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 13
dl 0
loc 73
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadEntityAttributeByAttributeIdAndAttributeSetId() 0 3 1
A initializeAttributeForAttributeGroup() 0 14 2
A initializeAttributeForEntityAttribute() 0 14 2
A loadAttributeGroupByAttributeSetIdAndAttributeGroupCode() 0 3 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Observers\CopyParentAttributeSetUpdateObserver
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 copies the EAV attribute groups and attribute relations from the parent in update mode.
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 CopyParentAttributeSetUpdateObserver extends CopyParentAttributeSetObserver
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 initializeAttributeForAttributeGroup(array $attr)
45
    {
46
47
        // load the attribute set + attribute group code
48
        $attributeSetId = $attr[MemberNames::ATTRIBUTE_SET_ID];
49
        $attributeGroupCode = $attr[MemberNames::ATTRIBUTE_GROUP_CODE];
50
51
        // load the attribute group entity
52
        if ($entity = $this->loadAttributeGroupByAttributeSetIdAndAttributeGroupCode($attributeSetId, $attributeGroupCode)) {
53
            return $this->mergeEntity($entity, $attr);
0 ignored issues
show
Bug introduced by
It seems like $entity can also be of type true; however, parameter $entity of TechDivision\Import\Obse...Observer::mergeEntity() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            return $this->mergeEntity(/** @scrutinizer ignore-type */ $entity, $attr);
Loading history...
54
        }
55
56
        // simply return the attributes
57
        return $attr;
58
    }
59
60
    /**
61
     * Initialize the attribute with the passed attributes and returns an instance.
62
     *
63
     * @param array $attr The attribute attributes
64
     *
65
     * @return array The initialized attribute
66
     */
67
    protected function initializeAttributeForEntityAttribute(array $attr)
68
    {
69
70
        // load the attribute + attribute set ID from the passed attributes
71
        $attributeId = $attr[MemberNames::ATTRIBUTE_ID];
72
        $attributeSetId = $attr[MemberNames::ATTRIBUTE_SET_ID];
73
74
        // load the attribute entity attribute
75
        if ($entity = $this->loadEntityAttributeByAttributeIdAndAttributeSetId($attributeId, $attributeSetId)) {
76
            return $this->mergeEntity($entity, $attr);
77
        }
78
79
        // simply return the attributes
80
        return $attr;
81
    }
82
83
    /**
84
     * Return's the EAV entity attribute with the passed attribute and attribute set ID.
85
     *
86
     * @param integer $attributeId    The ID of the EAV entity attribute's attribute to return
87
     * @param integer $attributeSetId The ID of the EAV entity attribute's attribute set to return
88
     *
89
     * @return array The EAV entity attribute
90
     */
91
    protected function loadEntityAttributeByAttributeIdAndAttributeSetId($attributeId, $attributeSetId)
92
    {
93
        return $this->getAttributeSetBunchProcessor()->loadEntityAttributeByAttributeIdAndAttributeSetId($attributeId, $attributeSetId);
94
    }
95
96
    /**
97
     * Return's the attribute group for the passed attribute set ID and attribute group code.
98
     *
99
     * @param integer $attributeSetId     The EAV attribute set ID to return the attribute group for
100
     * @param string  $attributeGroupCode The EAV attribute group code to load the attribute group for
101
     *
102
     * @return array|boolean The EAV attribute group for the passed attribute set ID and attribute group code
103
     */
104
    protected function loadAttributeGroupByAttributeSetIdAndAttributeGroupCode($attributeSetId, $attributeGroupCode)
105
    {
106
        return $this->getAttributeSetBunchProcessor()->loadAttributeGroupByAttributeSetIdAndAttributeGroupCode($attributeSetId, $attributeGroupCode);
107
    }
108
}
109