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

AttributeSetObserver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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\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 set 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 AttributeSetObserver 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::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
        // load the entity type code and attribute set name
84
        $entityTypeCode = $this->getValue(ColumnKeys::ENTITY_TYPE_CODE);
85
        $attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME);
86
87
        // query whether or not we've found a line with a attribute group definition
88
        if ($entityTypeCode === null && $attributeSetName === null) {
89
            return;
90
        }
91
92
        // query whether or not, we've found a new entity type code/attribute set name => means we've found a new attribute set
93
        if ($this->hasBeenProcessed($entityTypeCode, $attributeSetName)) {
94
            return;
95
        }
96
97
        // prepare the attribue set values
98
        $attributeSet = $this->initializeAttribute($this->prepareAttributes());
99
100
        // persist the values and set the new attribute set ID
101
        $attributeSet[MemberNames::ATTRIBUTE_SET_ID] = $this->persistAttributeSet($this->initializeAttribute($this->prepareDynamicAttributes()));
102
103
        // temporarily persist the attribute set for processing the attribute groups
104
        $this->setLastAttributeSet($attributeSet);
105
    }
106
107
    /**
108
     * Appends the dynamic to the static attributes for the EAV attribute
109
     * and returns them.
110
     *
111
     * @return array The array with all available attributes
112
     */
113
    protected function prepareDynamicAttributes()
114
    {
115
        return array_merge($this->prepareAttributes(), $this->attributeLoader ? $this->attributeLoader->load($this, $this->columns) : array());
116
    }
117
118
    /**
119
     * Prepare the attributes of the entity that has to be persisted.
120
     *
121
     * @return array The prepared attributes
122
     */
123
    protected function prepareAttributes()
124
    {
125
126
        // map the entity type code to the ID
127
        $entityType = $this->getEntityType($this->getValue(ColumnKeys::ENTITY_TYPE_CODE));
128
        $entityTypeId = $entityType[MemberNames::ENTITY_TYPE_ID];
129
130
        // load the attribute set names from the column
131
        $attributeSetName = $this->getValue(ColumnKeys::ATTRIBUTE_SET_NAME);
132
133
        // return the prepared product
134
        return $this->initializeEntity(
135
            $this->loadRawEntity(
136
                array(
137
                    MemberNames::ENTITY_TYPE_ID     => $entityTypeId,
138
                    MemberNames::ATTRIBUTE_SET_NAME => $attributeSetName
139
                )
140
            )
141
        );
142
    }
143
144
    /**
145
     * Load's and return's a raw customer entity without primary key but the mandatory members only and nulled values.
146
     *
147
     * @param array $data An array with data that will be used to initialize the raw entity with
148
     *
149
     * @return array The initialized entity
150
     */
151
    protected function loadRawEntity(array $data = array())
152
    {
153
        return $this->getAttributeSetBunchProcessor()->loadRawEntity(EntityTypeCodes::EAV_ATTRIBUTE_SET, $data);
154
    }
155
156
    /**
157
     * Initialize the attribute with the passed attributes and returns an instance.
158
     *
159
     * @param array $attr The attribute attributes
160
     *
161
     * @return array The initialized attribute
162
     */
163
    protected function initializeAttribute(array $attr)
164
    {
165
        return $attr;
166
    }
167
168
    /**
169
     * Return's the entity type for the passed code.
170
     *
171
     * @param string $entityTypeCode The entity type code
172
     *
173
     * @return array The requested entity type
174
     * @throws \Exception Is thrown, if the entity type with the passed code is not available
175
     */
176
    protected function getEntityType($entityTypeCode)
177
    {
178
        return $this->getSubject()->getEntityType($entityTypeCode);
179
    }
180
181
    /**
182
     * Persist the passed attribute set.
183
     *
184
     * @param array $attributeSet The attribute set to persist
185
     *
186
     * @return string The ID of the persisted attribute set
187
     */
188
    protected function persistAttributeSet(array $attributeSet)
189
    {
190
        return $this->getAttributeSetBunchProcessor()->persistAttributeSet($attributeSet);
191
    }
192
}
193