Passed
Push — 14.x ( b3445b )
by Tim
04:11
created

AttributeGroupObserver::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 2
nop 0
crap 12
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Observers\AttributeGroupObserver
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\Utils\BackendTypeKeys;
24
use TechDivision\Import\Observers\AttributeLoaderInterface;
25
use TechDivision\Import\Observers\DynamicAttributeObserverInterface;
26
use TechDivision\Import\Attribute\Set\Utils\ColumnKeys;
27
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
28
use TechDivision\Import\Attribute\Set\Utils\EntityTypeCodes;
29
use TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface;
30
31
/**
32
 * Observer that create's the EAV attribute group itself.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2019 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import-attribute-set
38
 * @link      http://www.techdivision.com
39
 */
40
class AttributeGroupObserver extends AbstractAttributeSetObserver implements DynamicAttributeObserverInterface
41
{
42
43
    /**
44
     * The attribute loader instance.
45
     *
46
     * @var \TechDivision\Import\Observers\AttributeLoaderInterface
47
     */
48
    protected $attributeLoader;
49
50
    /**
51
     * Initialize the dedicated column.
52
     *
53
     * @var array
54
     */
55
    protected $columns = array(MemberNames::SORT_ORDER => array(ColumnKeys::ATTRIBUTE_GROUP_SORT_ORDER, BackendTypeKeys::BACKEND_TYPE_INT));
56
57
    /**
58
     * Initializes the observer with the passed subject instance.
59
     *
60
     * @param \TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface $attributeSetBunchProcessor The attribute set bunch processor instance
61
     * @param \TechDivision\Import\Observers\AttributeLoaderInterface                         $attributeLoader            The attribute loader instance
62
     */
63
    public function __construct(
64
        AttributeSetBunchProcessorInterface $attributeSetBunchProcessor,
65
        AttributeLoaderInterface $attributeLoader = null
66
    ) {
67
68
        // set the attribute loader
69
        $this->attributeLoader = $attributeLoader;
70
71
        // pass the processor to th eparend constructor
72
        parent::__construct($attributeSetBunchProcessor);
73
    }
74
75
    /**
76
     * Process the observer's business logic.
77
     *
78
     * @return void
79
     */
80
    protected function process()
81
    {
82
83
        // query whether or not at least attribute group name & code has been set
84
        if ($this->hasValue(ColumnKeys::ATTRIBUTE_GROUP_NAME) &&
85
            $this->hasValue(ColumnKeys::ATTRIBUTE_GROUP_CODE)
86
        ) {
87
            // prepare the attribue set values
88
            $attributeSetGroup = $this->initializeAttribute($this->prepareAttributes());
89
            // persist the entity
90
            $this->persistAttributeGroup($attributeSetGroup);
91
        }
92
    }
93
94
    /**
95
     * Appends the dynamic to the static attributes for the EAV attribute
96
     * and returns them.
97
     *
98
     * @return array The array with all available attributes
99
     */
100
    protected function prepareDynamicAttributes()
101
    {
102
        return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array());
103
    }
104
105
    /**
106
     * Prepare the attributes of the entity that has to be persisted.
107
     *
108
     * @return array The prepared attributes
109
     */
110
    protected function prepareAttributes()
111
    {
112
113
        // load the last attribute set
114
        $attributeSet = $this->getLastAttributeSet();
115
116
        // load the attribute set values from the column
117
        $defaultId = $this->getValue(ColumnKeys::DEFAULT_ID, 0);
118
        $tabGroupCode = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_TAB_GROUP_CODE, 'basic');
119
        $attributeGroupName = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_NAME);
120
        $attributeGroupCode = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_CODE);
121
122
        // return the prepared product
123
        return $this->initializeEntity(
124
            $this->loadRawEntity(
125
                array(
126
                    MemberNames::ATTRIBUTE_SET_ID     => $attributeSet[MemberNames::ATTRIBUTE_SET_ID],
127
                    MemberNames::ATTRIBUTE_GROUP_NAME => $attributeGroupName,
128
                    MemberNames::DEFAULT_ID           => $defaultId,
129
                    MemberNames::ATTRIBUTE_GROUP_CODE => $attributeGroupCode,
130
                    MemberNames::TAB_GROUP_CODE       => $tabGroupCode
131
                )
132
            )
133
        );
134
    }
135
136
    /**
137
     * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values.
138
     *
139
     * @param array $data An array with data that will be used to initialize the raw entity with
140
     *
141
     * @return array The initialized entity
142
     */
143
    protected function loadRawEntity(array $data = array())
144
    {
145
        return $this->getAttributeSetBunchProcessor()->loadRawEntity(EntityTypeCodes::EAV_ATTRIBUTE_GROUP, $data);
146
    }
147
148
    /**
149
     * Initialize the attribute with the passed attributes and returns an instance.
150
     *
151
     * @param array $attr The attribute attributes
152
     *
153
     * @return array The initialized attribute
154
     */
155
    protected function initializeAttribute(array $attr)
156
    {
157
        return $attr;
158
    }
159
160
    /**
161
     * Return's the entity type code to be used.
162
     *
163
     * @return string The entity type code to be used
164
     */
165
    protected function getEntityTypeCode()
166
    {
167
        return $this->getSubject()->getEntityTypeCode();
168
    }
169
170
    /**
171
     * Persist the passed attribute group.
172
     *
173
     * @param array $attributeGroup The attribute group to persist
174
     *
175
     * @return void
176
     */
177
    protected function persistAttributeGroup(array $attributeGroup)
178
    {
179
        return $this->getAttributeSetBunchProcessor()->persistAttributeGroup($attributeGroup);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttribu...eGroup($attributeGroup) returns the type string which is incompatible with the documented return type void.
Loading history...
180
    }
181
}
182