Completed
Push — 13.x ( dc1925 )
by Tim
03:49
created

getEavAttributeByAttributeCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Subjects\AbstractSubject
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 2016 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Subjects;
22
23
use TechDivision\Import\Utils\MemberNames;
24
use TechDivision\Import\Utils\RegistryKeys;
25
use TechDivision\Import\Utils\BackendTypeKeys;
26
use TechDivision\Import\Utils\EntityTypeCodes;
27
28
/**
29
 * An abstract EAV subject implementation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 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
35
 * @link      http://www.techdivision.com
36
 */
37
abstract class AbstractEavSubject extends AbstractSubject implements EavSubjectInterface, NumberConverterSubjectInterface
38
{
39
40
    /**
41
     * The trait that provides number converting functionality.
42
     *
43
     * @var \TechDivision\Import\Subjects\NumberConverterTrait
44
     */
45
    use NumberConverterTrait;
46
47
    /**
48
     * The available EAV attributes, grouped by their attribute set and the attribute set name as keys.
49
     *
50
     * @var array
51
     */
52
    protected $attributes = array();
53
54
    /**
55
     * The available user defined EAV attributes, grouped by their entity type.
56
     *
57
     * @var array
58
     */
59
    protected $userDefinedAttributes = array();
60
61
    /**
62
     * The attribute set of the entity that has to be created.
63
     *
64
     * @var array
65
     */
66
    protected $attributeSet = array();
67
68
    /**
69
     * The available EAV attribute sets.
70
     *
71
     * @var array
72
     */
73
    protected $attributeSets = array();
74
75
    /**
76
     * The mapping for the supported backend types (for the EAV entity) => persist methods.
77
     *
78
     * @var array
79
     */
80
    protected $backendTypes = array(
81
        BackendTypeKeys::BACKEND_TYPE_DATETIME => array('persistDatetimeAttribute', 'loadDatetimeAttribute', 'deleteDatetimeAttribute'),
82
        BackendTypeKeys::BACKEND_TYPE_DECIMAL  => array('persistDecimalAttribute', 'loadDecimalAttribute', 'deleteDecimalAttribute'),
83
        BackendTypeKeys::BACKEND_TYPE_INT      => array('persistIntAttribute', 'loadIntAttribute', 'deleteIntAttribute'),
84
        BackendTypeKeys::BACKEND_TYPE_TEXT     => array('persistTextAttribute', 'loadTextAttribute', 'deleteTextAttribute'),
85
        BackendTypeKeys::BACKEND_TYPE_VARCHAR  => array('persistVarcharAttribute', 'loadVarcharAttribute', 'deleteVarcharAttribute')
86
    );
87
88
    /**
89
     * The mappings for the entity type code to attribute set.
90
     *
91
     * @var array
92
     */
93
    protected $entityTypeCodeToAttributeSetMappings = array(
94
        EntityTypeCodes::CATALOG_PRODUCT           => EntityTypeCodes::CATALOG_PRODUCT,
95
        EntityTypeCodes::CATALOG_PRODUCT_PRICE     => EntityTypeCodes::CATALOG_PRODUCT,
96
        EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => EntityTypeCodes::CATALOG_PRODUCT,
97
        EntityTypeCodes::CATALOG_CATEGORY          => EntityTypeCodes::CATALOG_CATEGORY,
98
        EntityTypeCodes::EAV_ATTRIBUTE             => EntityTypeCodes::EAV_ATTRIBUTE,
99
        EntityTypeCodes::NONE                      => EntityTypeCodes::NONE
100
    );
101
102
    /**
103
     * The default mappings for the user defined attributes, based on the attributes frontend input type.
104
     *
105
     * @var array
106
     */
107
    protected $defaultFrontendInputCallbackMappings = array();
108
109
    /**
110
     * Return's the default callback frontend input mappings for the user defined attributes.
111
     *
112
     * @return array The default frontend input callback mappings
113
     */
114 22
    public function getDefaultFrontendInputCallbackMappings()
115
    {
116 22
        return $this->defaultFrontendInputCallbackMappings;
117
    }
118
119
    /**
120
     * Intializes the previously loaded global data for exactly one bunch.
121
     *
122
     * @param string $serial The serial of the actual import
123
     *
124
     * @return void
125
     */
126 22
    public function setUp($serial)
127
    {
128
129
        // load the status of the actual import
130 22
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
131
132
        // load the global data we've prepared initially
133 22
        $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
134 22
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
135 22
        $this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES];
136
137
        // load the default frontend callback mappings from the child instance and merge with the one from the configuration
138 22
        $defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings();
139
140
        // load the available frontend input callbacks from the configuration
141 22
        $availableFrontendInputCallbacks = $this->getConfiguration()->getFrontendInputCallbacks();
142
143
        // merge the default mappings with the one's found in the configuration
144 22
        foreach ($availableFrontendInputCallbacks as $frontendInputCallbackMappings) {
145
            foreach ($frontendInputCallbackMappings as $frontendInput => $frontentInputMappings) {
146
                $defaultFrontendInputCallbackMappings[$frontendInput] = $frontentInputMappings;
147
            }
148
        }
149
150
        // load the user defined EAV attributes
151 22
        $eavUserDefinedAttributes = $this->getEavUserDefinedAttributes();
152
153
        // load the user defined attributes and add the callback mappings
154 22
        foreach ($eavUserDefinedAttributes as $eavAttribute) {
155
            // load attribute code and frontend input type
156 1
            $attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE];
157 1
            $frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT];
158
159
            // query whether or not the array for the mappings has been initialized
160 1
            if (!isset($this->callbackMappings[$attributeCode])) {
161 1
                $this->callbackMappings[$attributeCode] = array();
162
            }
163
164
            // set the appropriate callback mapping for the attributes input type
165 1
            if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) {
166 1
                foreach ($defaultFrontendInputCallbackMappings[$frontendInput] as $defaultFrontendInputCallbackMapping) {
167 1
                    $this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMapping;
168
                }
169
            }
170
        }
171
172
        // prepare the callbacks
173 22
        parent::setUp($serial);
174 22
    }
175
176
    /**
177
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
178
     *
179
     * @return array The mapping for the supported backend types
180
     */
181 1
    public function getBackendTypes()
182
    {
183 1
        return $this->backendTypes;
184
    }
185
186
    /**
187
     * Set's the attribute set of the product that has to be created.
188
     *
189
     * @param array $attributeSet The attribute set
190
     *
191
     * @return void
192
     */
193 5
    public function setAttributeSet(array $attributeSet)
194
    {
195 5
        $this->attributeSet = $attributeSet;
196 5
    }
197
198
    /**
199
     * Return's the attribute set of the product that has to be created.
200
     *
201
     * @return array The attribute set
202
     */
203 1
    public function getAttributeSet()
204
    {
205 1
        return $this->attributeSet;
206
    }
207
208
    /**
209
     * Cast's the passed value based on the backend type information.
210
     *
211
     * @param string $backendType The backend type to cast to
212
     * @param mixed  $value       The value to be casted
213
     *
214
     * @return mixed The casted value
215
     */
216 9
    public function castValueByBackendType($backendType, $value)
217
    {
218
219
        // cast the value to a valid timestamp
220 9
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) {
221 2
            return $this->getDateConverter()->convert($value);
222
        }
223
224
        // cast the value to a float/deicmal value
225 7
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT ||
226 7
            $backendType === BackendTypeKeys::BACKEND_TYPE_DECIMAL
227
        ) {
228 2
            return (float) $this->getNumberConverter()->parse($value);
229
        }
230
231
        // cast the value to an integer
232 5
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) {
233 3
            return (integer) $value;
234
        }
235
236
        // we don't need to cast strings
237 2
        return $value;
238
    }
239
240
    /**
241
     * Return's the entity type code to be used.
242
     *
243
     * @return string The entity type code to be used
244
     */
245 22
    public function getEntityTypeCode()
246
    {
247
248
        // load the entity type code from the configuration
249 22
        $entityTypeCode = $this->getConfiguration()->getConfiguration()->getEntityTypeCode();
250
251
        // try to map the entity type code
252 22
        if (isset($this->entityTypeCodeToAttributeSetMappings[$entityTypeCode])) {
253 8
            $entityTypeCode = $this->entityTypeCodeToAttributeSetMappings[$entityTypeCode];
254
        }
255
256
        // return the (mapped) entity type code
257 22
        return $entityTypeCode;
258
    }
259
260
    /**
261
     * Return's the attribute set with the passed attribute set name.
262
     *
263
     * @param string $attributeSetName The name of the requested attribute set
264
     *
265
     * @return array The attribute set data
266
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
267
     */
268 3
    public function getAttributeSetByAttributeSetName($attributeSetName)
269
    {
270
271
        // query whether or not attribute sets for the actualy entity type code are available
272 3
        if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) {
273
            // load the attribute sets for the actualy entity type code
274 2
            $attributSets = $this->attributeSets[$entityTypeCode];
275
276
            // query whether or not, the requested attribute set is available
277 2
            if (isset($attributSets[$attributeSetName])) {
278 1
                return $attributSets[$attributeSetName];
279
            }
280
281
            // throw an exception, if not
282 1
            throw new \Exception(
283 1
                $this->appendExceptionSuffix(
284 1
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
285
                )
286
            );
287
        }
288
289
        // throw an exception, if not
290 1
        throw new \Exception(
291 1
            $this->appendExceptionSuffix(
292 1
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
293
            )
294
        );
295
    }
296
297
    /**
298
     * Return's the attributes for the attribute set of the product that has to be created.
299
     *
300
     * @return array The attributes
301
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
302
     */
303 5
    public function getAttributes()
304
    {
305
306
        // query whether or not, the requested EAV attributes are available
307 5
        if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) {
308
            // load the attributes for the entity type code
309 4
            $attributes = $this->attributes[$entityTypeCode];
310
311
            // query whether or not an attribute set has been loaded from the source file
312 4
            if (is_array($this->attributeSet) && isset($this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME])) {
313
                // load the attribute set name
314 4
                $attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME];
315
316
                // query whether or not attributes for the actual attribute set name
317 4
                if ($attributeSetName && isset($attributes[$attributeSetName])) {
318 3
                    return $attributes[$attributeSetName];
319
                }
320
321
                // throw an exception, if not
322 1
                throw new \Exception(
323 1
                    $this->appendExceptionSuffix(
324 1
                        sprintf('Found invalid attribute set name "%s"', $attributeSetName)
325
                    )
326
                );
327
            } else {
328
                return call_user_func_array('array_merge', $attributes);
329
            }
330
        }
331
332
        // throw an exception, if not
333 1
        throw new \Exception(
334 1
            $this->appendExceptionSuffix(
335 1
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
336
            )
337
        );
338
    }
339
340
    /**
341
     * Return's an array with the available user defined EAV attributes for the actual entity type.
342
     *
343
     * @return array The array with the user defined EAV attributes
344
     */
345 22
    public function getEavUserDefinedAttributes()
346
    {
347
348
        // initialize the array with the user defined EAV attributes
349 22
        $eavUserDefinedAttributes = array();
350
351
        // query whether or not user defined EAV attributes for the actualy entity type are available
352 22
        if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) {
353 2
            $eavUserDefinedAttributes = $this->userDefinedAttributes[$entityTypeCode];
354
        }
355
356
        // return the array with the user defined EAV attributes
357 22
        return $eavUserDefinedAttributes;
358
    }
359
360
    /**
361
     * Return's the EAV attribute with the passed attribute code.
362
     *
363
     * @param string $attributeCode The attribute code
364
     *
365
     * @return array The array with the EAV attribute
366
     * @throws \Exception Is thrown if the attribute with the passed code is not available
367
     */
368 2
    public function getEavAttributeByAttributeCode($attributeCode)
369
    {
370
371
        // load the attributes
372 2
        $attributes = $this->getAttributes();
373
374
        // query whether or not the attribute exists
375 2
        if (isset($attributes[$attributeCode])) {
376 1
            return $attributes[$attributeCode];
377
        }
378
379
        // throw an exception if the requested attribute is not available
380 1
        throw new \Exception(
381 1
            $this->appendExceptionSuffix(
382 1
                sprintf('Can\'t load attribute with code "%s"', $attributeCode)
383
            )
384
        );
385
    }
386
}
387