CopyParentAttributeSetUpdateObserver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 73
ccs 0
cts 16
cp 0
rs 10
wmc 6

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
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2019 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-attribute-set
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Attribute\Set\Observers;
16
17
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
18
19
/**
20
 * Observer that copies the EAV attribute groups and attribute relations from the parent in update mode.
21
 *
22
 * @author    Tim Wagner <[email protected]>
23
 * @copyright 2019 TechDivision GmbH <[email protected]>
24
 * @license   https://opensource.org/licenses/MIT
25
 * @link      https://github.com/techdivision/import-attribute-set
26
 * @link      http://www.techdivision.com
27
 */
28
class CopyParentAttributeSetUpdateObserver extends CopyParentAttributeSetObserver
29
{
30
31
    /**
32
     * Initialize the attribute with the passed attributes and returns an instance.
33
     *
34
     * @param array $attr The attribute attributes
35
     *
36
     * @return array The initialized attribute
37
     */
38
    protected function initializeAttributeForAttributeGroup(array $attr)
39
    {
40
41
        // load the attribute set + attribute group code
42
        $attributeSetId = $attr[MemberNames::ATTRIBUTE_SET_ID];
43
        $attributeGroupCode = $attr[MemberNames::ATTRIBUTE_GROUP_CODE];
44
45
        // load the attribute group entity
46
        if ($entity = $this->loadAttributeGroupByAttributeSetIdAndAttributeGroupCode($attributeSetId, $attributeGroupCode)) {
47
            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

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