Completed
Push — master ( 7e2127...77c655 )
by Marcus
04:10
created

AttributeGroupObserver::prepareDynamicAttributes()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 2
nc 1
nop 0
crap 6
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
use TechDivision\Import\Observers\EntityMergers\EntityMergerInterface;
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Obse...s\EntityMergerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use TechDivision\Import\Observers\StateDetectorInterface;
0 ignored issues
show
Bug introduced by
The type TechDivision\Import\Obse...\StateDetectorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use TechDivision\Import\Utils\EntityStatus;
33
34
/**
35
 * Observer that create's the EAV attribute group itself.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2019 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import-attribute-set
41
 * @link      http://www.techdivision.com
42
 */
43
class AttributeGroupObserver extends AbstractAttributeSetObserver implements DynamicAttributeObserverInterface
44
{
45
46
    /**
47
     * The attribute loader instance.
48
     *
49
     * @var \TechDivision\Import\Observers\AttributeLoaderInterface
50
     */
51
    protected $attributeLoader;
52
53
    /**
54
     * The entity merger instance.
55
     *
56
     * @var \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface
57
     */
58
    protected $entityMerger;
59
60
    /**
61
     * Initialize the dedicated column.
62
     *
63
     * @var array
64
     */
65
    protected $columns = array(MemberNames::SORT_ORDER => array(ColumnKeys::ATTRIBUTE_GROUP_SORT_ORDER, BackendTypeKeys::BACKEND_TYPE_INT));
66
67
    /**
68
     * Initializes the observer with the passed subject instance.
69
     *
70
     * @param \TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface $attributeSetBunchProcessor The attribute set bunch processor instance
71
     * @param \TechDivision\Import\Observers\AttributeLoaderInterface|null                    $attributeLoader            The attribute loader instance
72
     * @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface|null         $entityMerger               The entity merger instance
73
     * @param \TechDivision\Import\Observers\StateDetectorInterface|null                      $stateDetector              The state detector instance to use
74
     */
75
    public function __construct(
76
        AttributeSetBunchProcessorInterface $attributeSetBunchProcessor,
77
        AttributeLoaderInterface $attributeLoader = null,
78
        EntityMergerInterface $entityMerger = null,
79
        StateDetectorInterface $stateDetector = null
80
    ) {
81
82
        // set the attribute loader
83
        $this->attributeLoader = $attributeLoader;
84
        $this->entityMerger = $entityMerger;
85
86
        // pass the processor to th eparend constructor
87
        parent::__construct($attributeSetBunchProcessor, $stateDetector);
88
    }
89
90
    /**
91
     * Process the observer's business logic.
92
     *
93
     * @return void
94
     */
95
    protected function process()
96
    {
97
98
        // query whether or not at least attribute group name & code has been set
99
        if ($this->hasValue(ColumnKeys::ATTRIBUTE_GROUP_NAME) &&
100
            $this->hasValue(ColumnKeys::ATTRIBUTE_GROUP_CODE)
101
        ) {
102
            // prepare the attribue set values
103
            $attributeSetGroup = $this->initializeAttribute($this->prepareAttributes());
104
            // persist the entity
105
            $this->persistAttributeGroup($attributeSetGroup);
106
        }
107
    }
108
109
    /**
110
     * Merge's and return's the entity with the passed attributes and set's the
111
     * passed status.
112
     *
113
     * @param array       $entity        The entity to merge the attributes into
114
     * @param array       $attr          The attributes to be merged
115
     * @param string|null $changeSetName The change set name to use
116
     *
117
     * @return array The merged entity
118
     * @todo https://github.com/techdivision/import/issues/179
119
     */
120
    protected function mergeEntity(array $entity, array $attr, $changeSetName = null)
121
    {
122
        return array_merge(
123
            $entity,
124
            $this->entityMerger ? $this->entityMerger->merge($this, $entity, $attr) : $attr,
125
            array(EntityStatus::MEMBER_NAME => $this->detectState($entity, $attr, $changeSetName))
0 ignored issues
show
Bug introduced by
The method detectState() does not exist on TechDivision\Import\Attr...\AttributeGroupObserver. ( Ignorable by Annotation )

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

125
            array(EntityStatus::MEMBER_NAME => $this->/** @scrutinizer ignore-call */ detectState($entity, $attr, $changeSetName))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
        );
127
    }
128
129
    /**
130
     * Appends the dynamic to the static attributes for the EAV attribute
131
     * and returns them.
132
     *
133
     * @return array The array with all available attributes
134
     */
135
    protected function prepareDynamicAttributes()
136
    {
137
        return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array());
138
    }
139
140
    /**
141
     * Prepare the attributes of the entity that has to be persisted.
142
     *
143
     * @return array The prepared attributes
144
     */
145
    protected function prepareAttributes()
146
    {
147
148
        // load the last attribute set
149
        $attributeSet = $this->getLastAttributeSet();
150
151
        // load the attribute set values from the column
152
        $defaultId = $this->getValue(ColumnKeys::DEFAULT_ID, 0);
153
        $tabGroupCode = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_TAB_GROUP_CODE, 'basic');
154
        $attributeGroupName = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_NAME);
155
        $attributeGroupCode = $this->getValue(ColumnKeys::ATTRIBUTE_GROUP_CODE);
156
157
        // return the prepared product
158
        return $this->initializeEntity(
159
            $this->loadRawEntity(
160
                array(
161
                    MemberNames::ATTRIBUTE_SET_ID     => $attributeSet[MemberNames::ATTRIBUTE_SET_ID],
162
                    MemberNames::ATTRIBUTE_GROUP_NAME => $attributeGroupName,
163
                    MemberNames::DEFAULT_ID           => $defaultId,
164
                    MemberNames::ATTRIBUTE_GROUP_CODE => $attributeGroupCode,
165
                    MemberNames::TAB_GROUP_CODE       => $tabGroupCode
166
                )
167
            )
168
        );
169
    }
170
171
    /**
172
     * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values.
173
     *
174
     * @param array $data An array with data that will be used to initialize the raw entity with
175
     *
176
     * @return array The initialized entity
177
     */
178
    protected function loadRawEntity(array $data = array())
179
    {
180
        return $this->getAttributeSetBunchProcessor()->loadRawEntity(EntityTypeCodes::EAV_ATTRIBUTE_GROUP, $data);
181
    }
182
183
    /**
184
     * Initialize the attribute with the passed attributes and returns an instance.
185
     *
186
     * @param array $attr The attribute attributes
187
     *
188
     * @return array The initialized attribute
189
     */
190
    protected function initializeAttribute(array $attr)
191
    {
192
        return $attr;
193
    }
194
195
    /**
196
     * Return's the entity type code to be used.
197
     *
198
     * @return string The entity type code to be used
199
     */
200
    protected function getEntityTypeCode()
201
    {
202
        return $this->getSubject()->getEntityTypeCode();
203
    }
204
205
    /**
206
     * Persist the passed attribute group.
207
     *
208
     * @param array $attributeGroup The attribute group to persist
209
     *
210
     * @return void
211
     */
212
    protected function persistAttributeGroup(array $attributeGroup)
213
    {
214
        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...
215
    }
216
}
217