Completed
Push — 15.x ( 53b7d0...60340d )
by Tim
04:40
created

AbstractEavSubject   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 354
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 354
c 0
b 0
f 0
wmc 32
lcom 1
cbo 7
ccs 0
cts 132
cp 0
rs 9.84

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFrontendInputCallbackMappings() 0 4 1
B setUp() 0 51 8
A getBackendTypes() 0 4 1
A setAttributeSet() 0 4 1
A getAttributeSet() 0 4 1
A castValueByBackendType() 0 25 5
A getEntityTypeCode() 0 14 2
A getAttributeSetByAttributeSetName() 0 28 3
B getAttributes() 0 36 6
A getEavUserDefinedAttributes() 0 14 2
A getEavAttributeByAttributeCode() 0 18 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
    public function getDefaultFrontendInputCallbackMappings()
115
    {
116
        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
    public function setUp($serial)
127
    {
128
129
        // load the status of the actual import
130
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
131
132
        // load the global data, if prepared initially
133
        if (isset($status[RegistryKeys::GLOBAL_DATA])) {
134
            $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
135
            $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
136
            $this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES];
137
        }
138
139
        // load the default frontend callback mappings from the child instance and merge with the one from the configuration
140
        $defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings();
141
142
        // load the available frontend input callbacks from the configuration
143
        $availableFrontendInputCallbacks = $this->getConfiguration()->getFrontendInputCallbacks();
144
145
        // merge the default mappings with the one's found in the configuration
146
        foreach ($availableFrontendInputCallbacks as $frontendInputCallbackMappings) {
147
            foreach ($frontendInputCallbackMappings as $frontendInput => $frontentInputMappings) {
148
                $defaultFrontendInputCallbackMappings[$frontendInput] = $frontentInputMappings;
149
            }
150
        }
151
152
        // load the user defined EAV attributes
153
        $eavUserDefinedAttributes = $this->getEavUserDefinedAttributes();
154
155
        // load the user defined attributes and add the callback mappings
156
        foreach ($eavUserDefinedAttributes as $eavAttribute) {
157
            // load attribute code and frontend input type
158
            $attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE];
159
            $frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT];
160
161
            // query whether or not the array for the mappings has been initialized
162
            if (!isset($this->callbackMappings[$attributeCode])) {
163
                $this->callbackMappings[$attributeCode] = array();
164
            }
165
166
            // set the appropriate callback mapping for the attributes input type
167
            if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) {
168
                foreach ($defaultFrontendInputCallbackMappings[$frontendInput] as $defaultFrontendInputCallbackMapping) {
169
                    $this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMapping;
170
                }
171
            }
172
        }
173
174
        // prepare the callbacks
175
        parent::setUp($serial);
176
    }
177
178
    /**
179
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
180
     *
181
     * @return array The mapping for the supported backend types
182
     */
183
    public function getBackendTypes()
184
    {
185
        return $this->backendTypes;
186
    }
187
188
    /**
189
     * Set's the attribute set of the product that has to be created.
190
     *
191
     * @param array $attributeSet The attribute set
192
     *
193
     * @return void
194
     */
195
    public function setAttributeSet(array $attributeSet)
196
    {
197
        $this->attributeSet = $attributeSet;
198
    }
199
200
    /**
201
     * Return's the attribute set of the product that has to be created.
202
     *
203
     * @return array The attribute set
204
     */
205
    public function getAttributeSet()
206
    {
207
        return $this->attributeSet;
208
    }
209
210
    /**
211
     * Cast's the passed value based on the backend type information.
212
     *
213
     * @param string $backendType The backend type to cast to
214
     * @param mixed  $value       The value to be casted
215
     *
216
     * @return mixed The casted value
217
     */
218
    public function castValueByBackendType($backendType, $value)
219
    {
220
221
        // cast the value to a valid timestamp
222
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) {
223
            return $this->getDateConverter()->convert($value);
224
        }
225
226
        // cast the value to a string that represents the float/decimal value, because
227
        // PHP will cast float values implicitly to the system locales format when
228
        // rendering as string, e. g. with echo
229
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT ||
230
            $backendType === BackendTypeKeys::BACKEND_TYPE_DECIMAL
231
        ) {
232
            return (string) $this->getNumberConverter()->parse($value);
233
        }
234
235
        // cast the value to an integer
236
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) {
237
            return (integer) $value;
238
        }
239
240
        // we don't need to cast strings
241
        return $value;
242
    }
243
244
    /**
245
     * Return's the entity type code to be used.
246
     *
247
     * @return string The entity type code to be used
248
     */
249
    public function getEntityTypeCode()
250
    {
251
252
        // load the entity type code from the configuration
253
        $entityTypeCode = $this->getConfiguration()->getConfiguration()->getEntityTypeCode();
254
255
        // try to map the entity type code
256
        if (isset($this->entityTypeCodeToAttributeSetMappings[$entityTypeCode])) {
257
            $entityTypeCode = $this->entityTypeCodeToAttributeSetMappings[$entityTypeCode];
258
        }
259
260
        // return the (mapped) entity type code
261
        return $entityTypeCode;
262
    }
263
264
    /**
265
     * Return's the attribute set with the passed attribute set name.
266
     *
267
     * @param string $attributeSetName The name of the requested attribute set
268
     *
269
     * @return array The attribute set data
270
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
271
     */
272
    public function getAttributeSetByAttributeSetName($attributeSetName)
273
    {
274
275
        // query whether or not attribute sets for the actualy entity type code are available
276
        if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) {
277
            // load the attribute sets for the actualy entity type code
278
            $attributSets = $this->attributeSets[$entityTypeCode];
279
280
            // query whether or not, the requested attribute set is available
281
            if (isset($attributSets[$attributeSetName])) {
282
                return $attributSets[$attributeSetName];
283
            }
284
285
            // throw an exception, if not
286
            throw new \Exception(
287
                $this->appendExceptionSuffix(
288
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
289
                )
290
            );
291
        }
292
293
        // throw an exception, if not
294
        throw new \Exception(
295
            $this->appendExceptionSuffix(
296
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
297
            )
298
        );
299
    }
300
301
    /**
302
     * Return's the attributes for the attribute set of the product that has to be created.
303
     *
304
     * @return array The attributes
305
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
306
     */
307
    public function getAttributes()
308
    {
309
310
        // query whether or not, the requested EAV attributes are available
311
        if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) {
312
            // load the attributes for the entity type code
313
            $attributes = $this->attributes[$entityTypeCode];
314
315
            // query whether or not an attribute set has been loaded from the source file
316
            if (is_array($this->attributeSet) && isset($this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME])) {
317
                // load the attribute set name
318
                $attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME];
319
320
                // query whether or not attributes for the actual attribute set name
321
                if ($attributeSetName && isset($attributes[$attributeSetName])) {
322
                    return $attributes[$attributeSetName];
323
                }
324
325
                // throw an exception, if not
326
                throw new \Exception(
327
                    $this->appendExceptionSuffix(
328
                        sprintf('Found invalid attribute set name "%s"', $attributeSetName)
329
                    )
330
                );
331
            } else {
332
                return call_user_func_array('array_merge', $attributes);
333
            }
334
        }
335
336
        // throw an exception, if not
337
        throw new \Exception(
338
            $this->appendExceptionSuffix(
339
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
340
            )
341
        );
342
    }
343
344
    /**
345
     * Return's an array with the available user defined EAV attributes for the actual entity type.
346
     *
347
     * @return array The array with the user defined EAV attributes
348
     */
349
    public function getEavUserDefinedAttributes()
350
    {
351
352
        // initialize the array with the user defined EAV attributes
353
        $eavUserDefinedAttributes = array();
354
355
        // query whether or not user defined EAV attributes for the actualy entity type are available
356
        if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) {
357
            $eavUserDefinedAttributes = $this->userDefinedAttributes[$entityTypeCode];
358
        }
359
360
        // return the array with the user defined EAV attributes
361
        return $eavUserDefinedAttributes;
362
    }
363
364
    /**
365
     * Return's the EAV attribute with the passed attribute code.
366
     *
367
     * @param string $attributeCode The attribute code
368
     *
369
     * @return array The array with the EAV attribute
370
     * @throws \Exception Is thrown if the attribute with the passed code is not available
371
     */
372
    public function getEavAttributeByAttributeCode($attributeCode)
373
    {
374
375
        // load the attributes
376
        $attributes = $this->getAttributes();
377
378
        // query whether or not the attribute exists
379
        if (isset($attributes[$attributeCode])) {
380
            return $attributes[$attributeCode];
381
        }
382
383
        // throw an exception if the requested attribute is not available
384
        throw new \Exception(
385
            $this->appendExceptionSuffix(
386
                sprintf('Can\'t load attribute with code "%s"', $attributeCode)
387
            )
388
        );
389
    }
390
}
391