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

persistEntityAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Observers\CopyParentAttributeSetObserver
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\ColumnKeys;
24
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
25
use TechDivision\Import\Utils\EntityStatus;
26
use TechDivision\Import\Attribute\Set\Utils\ConfigurationKeys;
27
28
/**
29
 * Observer that copies the EAV attribute groups and attribute relations from the parent.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2019 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-attribute-set
35
 * @link      http://www.techdivision.com
36
 */
37
class CopyParentAttributeSetObserver extends AbstractAttributeSetObserver
38
{
39
40
    /**
41
     * Process the observer's business logic.
42
     *
43
     * @return void
44
     */
45
    protected function process()
46
    {
47
48
        // load the last attribute set
49
        $attributeSet = $this->getLastAttributeSet();
50
51
        // query whether or not the a given attribute set has to be extended
52
        if ($this->shouldCopy()) {
53
            // load the attribute set the new one is based on
54
            $basedOn = $this->loadAttributeSetByEntityTypeCodeAndAttributeSetName(
55
                $this->getValue(ColumnKeys::ENTITY_TYPE_CODE),
56
                $this->getValue(ColumnKeys::BASED_ON)
57
            );
58
59
            // load the attribute groups of the attribute set the new one should be based on
60
            $attributeGroupsBasedOn = $this->loadAttributeGroupsByAttributeSetId($basedOn[MemberNames::ATTRIBUTE_SET_ID]);
61
62
            // copy the attribute groups from the based on
63
            foreach ($attributeGroupsBasedOn as $attributeGroupBasedOn) {
64
                // create the attribute group
65
                $attributeGroupId = $this->persistAttributeGroup(
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $attributeGroupId is correct as $this->persistAttributeG...ttributeGroupBasedOn))) targeting TechDivision\Import\Attr...persistAttributeGroup() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
                    $attributeGroup = $this->initializeAttributeForAttributeGroup(
67
                        $this->prepareAttributesForAttributeGroup($attributeSet, $attributeGroupBasedOn)
68
                    )
69
                );
70
71
                // set the new attribute group ID
72
                $attributeGroup[MemberNames::ATTRIBUTE_GROUP_ID] = $attributeGroupId;
73
74
                // load the entity attributes of the attribute group
75
                $entityAttributes = $this->loadEntityAttributesByAttributeGroupId($attributeGroupBasedOn[MemberNames::ATTRIBUTE_GROUP_ID]);
76
77
                // copy the attribute group => attribute relation
78
                foreach ($entityAttributes as $entityAttribute) {
79
                    $this->persistEntityAttribute(
80
                        $this->initializeAttributeForEntityAttribute(
81
                            $this->prepareAttributesForEntityAttribute($attributeSet, $attributeGroup, $entityAttribute)
82
                        )
83
                    );
84
                }
85
            }
86
        }
87
    }
88
89
    /**
90
     * Queries whether or not the attribute groups and attribute relations of the attribute
91
     * set the actual one is based on, should be copied or not.
92
     *
93
     * They SHOULD be copied if the attribute set is new and has been created recently or
94
     * if the attribute set already exists and the `copy-parent-on-update` flag parameter
95
     * has been set to TRUE.
96
     *
97
     * @return boolean TRUE if the attribute groups and attribute relations should be copied, else FALSE
98
     */
99
    protected function shouldCopy()
100
    {
101
102
        // load the last attribute set
103
        $attributeSet = $this->getLastAttributeSet();
104
105
        // query whether or not the a given attribute set has to be extended
106
        return $this->hasValue(ColumnKeys::BASED_ON) && (
107
            $attributeSet[EntityStatus::MEMBER_NAME] === EntityStatus::STATUS_CREATE || (
108
                $attributeSet[EntityStatus::MEMBER_NAME] === EntityStatus::STATUS_UPDATE && $this->shouldCopyParentOnUpdate())
109
            );
110
    }
111
112
    /**
113
     * Queries whether or not the configuration param `copy-parent-on-update` has been set,
114
     * if yes it returns the value.
115
     *
116
     * @return boolean TRUE if the param is set and the value is TRUE, else FALSE
117
     */
118
    protected function shouldCopyParentOnUpdate()
119
    {
120
121
        // load the subject's configuration
122
        $configuration = $this->getSubject()->getConfiguration();
123
124
        // return the configuration value
125
        if ($configuration->hasParam(ConfigurationKeys::COPY_PARENT_ON_UPDATE)) {
126
            return $configuration->getParam(ConfigurationKeys::COPY_PARENT_ON_UPDATE);
127
        }
128
129
        // return FALSE
130
        return false;
131
    }
132
133
    /**
134
     * Prepare the attributes of the entity that has to be persisted.
135
     *
136
     * @param array $attributeSet    The attribute set with the data used to prepare the entity attribute
137
     * @param array $attributeGroup  The attribute group with the data used to prepare the entity attribute
138
     * @param array $entityAttribute The entity attribute with the data used to prepare the entity attribute
139
     *
140
     * @return array The prepared attributes
141
     */
142
    protected function prepareAttributesForEntityAttribute(array $attributeSet, array $attributeGroup, array $entityAttribute)
143
    {
144
145
        // return the prepared product
146
        return $this->initializeEntity(
147
            array(
148
                MemberNames::ATTRIBUTE_SET_ID   => $attributeSet[MemberNames::ATTRIBUTE_SET_ID],
149
                MemberNames::ATTRIBUTE_GROUP_ID => $attributeGroup[MemberNames::ATTRIBUTE_GROUP_ID],
150
                MemberNames::ATTRIBUTE_ID       => $entityAttribute[MemberNames::ATTRIBUTE_ID],
151
                MemberNames::ENTITY_TYPE_ID     => $entityAttribute[MemberNames::ENTITY_TYPE_ID],
152
                MemberNames::SORT_ORDER         => $entityAttribute[MemberNames::SORT_ORDER]
153
            )
154
        );
155
    }
156
157
    /**
158
     * Prepare the attributes of the entity that has to be persisted.
159
     *
160
     * @param array $attributeSet   The attribute set with the data used to prepare the attribute group
161
     * @param array $attributeGroup The attribute group with the data used to prepare the attribute group
162
     *
163
     * @return array The prepared attributes
164
     */
165
    protected function prepareAttributesForAttributeGroup(array $attributeSet, array $attributeGroup)
166
    {
167
168
        // return the prepared product
169
        return $this->initializeEntity(
170
            array(
171
                MemberNames::ATTRIBUTE_SET_ID     => $attributeSet[MemberNames::ATTRIBUTE_SET_ID],
172
                MemberNames::ATTRIBUTE_GROUP_NAME => $attributeGroup[MemberNames::ATTRIBUTE_GROUP_NAME],
173
                MemberNames::SORT_ORDER           => $attributeGroup[MemberNames::SORT_ORDER],
174
                MemberNames::DEFAULT_ID           => $attributeGroup[MemberNames::DEFAULT_ID],
175
                MemberNames::ATTRIBUTE_GROUP_CODE => $attributeGroup[MemberNames::ATTRIBUTE_GROUP_CODE],
176
                MemberNames::TAB_GROUP_CODE       => $attributeGroup[MemberNames::TAB_GROUP_CODE]
177
            )
178
        );
179
    }
180
181
    /**
182
     * Initialize the attribute with the passed attributes and returns an instance.
183
     *
184
     * @param array $attr The attribute attributes
185
     *
186
     * @return array The initialized attribute
187
     */
188
    protected function initializeAttributeForAttributeGroup(array $attr)
189
    {
190
        return $attr;
191
    }
192
193
    /**
194
     * Initialize the attribute with the passed attributes and returns an instance.
195
     *
196
     * @param array $attr The attribute attributes
197
     *
198
     * @return array The initialized attribute
199
     */
200
    protected function initializeAttributeForEntityAttribute(array $attr)
201
    {
202
        return $attr;
203
    }
204
205
    /**
206
     * Return's the entity type for the passed code, of if no entity type code has
207
     * been passed, the default one from the configuration will be used.
208
     *
209
     * @param string|null $entityTypeCode The entity type code
210
     *
211
     * @return array The requested entity type
212
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
213
     */
214
    protected function getEntityType($entityTypeCode = null)
215
    {
216
        return $this->getSubject()->getEntityType($entityTypeCode);
217
    }
218
219
    /**
220
     * Load's and return's the EAV attribute set with the passed entity type code and attribute set name.
221
     *
222
     * @param string $entityTypeCode   The entity type code of the EAV attribute set to load
223
     * @param string $attributeSetName The attribute set name of the EAV attribute set to return
224
     *
225
     * @return array The EAV attribute set
226
     */
227
    protected function loadAttributeSetByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName)
228
    {
229
        return $this->getAttributeSetBunchProcessor()->loadAttributeSetByEntityTypeCodeAndAttributeSetName($entityTypeCode, $attributeSetName);
230
    }
231
232
    /**
233
     * Return's the attribute groups for the passed attribute set ID, whereas the array
234
     * is prepared with the attribute group names as keys.
235
     *
236
     * @param mixed $attributeSetId The EAV attribute set ID to return the attribute groups for
237
     *
238
     * @return array|boolean The EAV attribute groups for the passed attribute ID
239
     */
240
    protected function loadAttributeGroupsByAttributeSetId($attributeSetId)
241
    {
242
        return $this->getAttributeSetBunchProcessor()->loadAttributeGroupsByAttributeSetId($attributeSetId);
243
    }
244
245
    /**
246
     * Returns the EAV entity attributes for the attribute group with the passed ID.
247
     *
248
     * @param integer $attributeGroupId The attribute group ID to load the EAV entity attributes for
249
     *
250
     * @return array|null The EAV attributes with for the passed attribute group ID
251
     */
252
    protected function loadEntityAttributesByAttributeGroupId($attributeGroupId)
253
    {
254
        return $this->getAttributeSetBunchProcessor()->loadEntityAttributesByAttributeGroupId($attributeGroupId);
255
    }
256
257
    /**
258
     * Persist the passed attribute group.
259
     *
260
     * @param array $attributeGroup The attribute group to persist
261
     *
262
     * @return void
263
     */
264
    protected function persistAttributeGroup(array $attributeGroup)
265
    {
266
        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...
267
    }
268
269
    /**
270
     * Persist the passed entity attribute.
271
     *
272
     * @param array $entityAttribute The entity attribute to persist
273
     *
274
     * @return void
275
     */
276
    protected function persistEntityAttribute(array $entityAttribute)
277
    {
278
        return $this->getAttributeSetBunchProcessor()->persistEntityAttribute($entityAttribute);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAttributeSetBu...ibute($entityAttribute) targeting TechDivision\Import\Attr...ersistEntityAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
279
    }
280
}
281