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

AttributeSetObserver::mergeEntity()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
cc 2
nc 1
nop 3
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Set\Observers\AttributeSetObserver
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\EntityStatus;
24
use TechDivision\Import\Utils\BackendTypeKeys;
25
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...
26
use TechDivision\Import\Observers\AttributeLoaderInterface;
27
use TechDivision\Import\Observers\DynamicAttributeObserverInterface;
28
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...
29
use TechDivision\Import\Attribute\Set\Utils\ColumnKeys;
30
use TechDivision\Import\Attribute\Set\Utils\MemberNames;
31
use TechDivision\Import\Attribute\Set\Utils\EntityTypeCodes;
32
use TechDivision\Import\Attribute\Set\Services\AttributeSetBunchProcessorInterface;
33
34
/**
35
 * Observer that create's the EAV attribute set 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 AttributeSetObserver 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::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
        // load the entity type code and attribute set name
99
        $entityTypeCode = $this->getValue(ColumnKeys::ENTITY_TYPE_CODE);
100
        $attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME);
101
102
        // query whether or not we've found a line with a attribute group definition
103
        if ($entityTypeCode === null && $attributeSetName === null) {
104
            return;
105
        }
106
107
        // query whether or not, we've found a new entity type code/attribute set name => means we've found a new attribute set
108
        if ($this->hasBeenProcessed($entityTypeCode, $attributeSetName)) {
109
            return;
110
        }
111
112
        // prepare the attribue set values
113
        $attributeSet = $this->initializeAttribute($this->prepareAttributes());
114
115
        // persist the values and set the new attribute set ID
116
        $attributeSet[MemberNames::ATTRIBUTE_SET_ID] = $this->persistAttributeSet($this->initializeAttribute($this->prepareDynamicAttributes()));
117
118
        // temporarily persist the attribute set for processing the attribute groups
119
        $this->setLastAttributeSet($attributeSet);
120
    }
121
122
    /**
123
     * Merge's and return's the entity with the passed attributes and set's the
124
     * passed status.
125
     *
126
     * @param array       $entity        The entity to merge the attributes into
127
     * @param array       $attr          The attributes to be merged
128
     * @param string|null $changeSetName The change set name to use
129
     *
130
     * @return array The merged entity
131
     * @todo https://github.com/techdivision/import/issues/179
132
     */
133
    protected function mergeEntity(array $entity, array $attr, $changeSetName = null)
134
    {
135
        return array_merge(
136
            $entity,
137
            $this->entityMerger ? $this->entityMerger->merge($this, $entity, $attr) : $attr,
138
            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...rs\AttributeSetObserver. ( Ignorable by Annotation )

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

138
            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...
139
        );
140
    }
141
142
    /**
143
     * Appends the dynamic to the static attributes for the EAV attribute
144
     * and returns them.
145
     *
146
     * @return array The array with all available attributes
147
     */
148
    protected function prepareDynamicAttributes()
149
    {
150
        return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array());
151
    }
152
153
    /**
154
     * Prepare the attributes of the entity that has to be persisted.
155
     *
156
     * @return array The prepared attributes
157
     */
158
    protected function prepareAttributes()
159
    {
160
161
        // map the entity type code to the ID
162
        $entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE));
163
        $entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID];
164
165
        // load the attribute set names from the column
166
        $attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME);
167
168
        // return the prepared product
169
        return $this->initializeEntity(
170
            $this->loadRawEntity(
171
                array(
172
                    MemberNames::ENTITY_TYPE_ID     => $entityTypeId,
173
                    MemberNames::ATTRIBUTE_SET_NAME => $attributeSetName
174
                )
175
            )
176
        );
177
    }
178
179
    /**
180
     * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values.
181
     *
182
     * @param array $data An array with data that will be used to initialize the raw entity with
183
     *
184
     * @return array The initialized entity
185
     */
186
    protected function loadRawEntity(array $data = array())
187
    {
188
        return $this->getAttributeSetBunchProcessor()->loadRawEntity(EntityTypeCodes::EAV_ATTRIBUTE_SET, $data);
189
    }
190
191
    /**
192
     * Initialize the attribute with the passed attributes and returns an instance.
193
     *
194
     * @param array $attr The attribute attributes
195
     *
196
     * @return array The initialized attribute
197
     */
198
    protected function initializeAttribute(array $attr)
199
    {
200
        return $attr;
201
    }
202
203
    /**
204
     * Return's the entity type for the passed code.
205
     *
206
     * @param string $entityTypeCode The entity type code
207
     *
208
     * @return array The requested entity type
209
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
210
     */
211
    protected function getEntityType($entityTypeCode)
212
    {
213
        return $this->getSubject()->getEntityType($entityTypeCode);
214
    }
215
216
    /**
217
     * Persist the passed attribute set.
218
     *
219
     * @param array $attributeSet The attribute set to persist
220
     *
221
     * @return string The ID of the persisted attribute set
222
     */
223
    protected function persistAttributeSet(array $attributeSet)
224
    {
225
        return $this->getAttributeSetBunchProcessor()->persistAttributeSet($attributeSet);
226
    }
227
}
228