Completed
Push — master ( 66fddb...991eda )
by Tim
15s
created

getAttributeSetByAttributeSetName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 9.3142
cc 3
eloc 8
nc 3
nop 1
crap 12
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
27
/**
28
 * An abstract EAV subject implementation.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
abstract class AbstractEavSubject extends AbstractSubject implements EavSubjectInterface
37
{
38
39
    /**
40
     * The available EAV attributes, grouped by their attribute set and the attribute set name as keys.
41
     *
42
     * @var array
43
     */
44
    protected $attributes = array();
45
46
    /**
47
     * The available user defined EAV attributes, grouped by their entity type.
48
     *
49
     * @var array
50
     */
51
    protected $userDefinedAttributes = array();
52
53
    /**
54
     * The attribute set of the entity that has to be created.
55
     *
56
     * @var array
57
     */
58
    protected $attributeSet = array();
59
60
    /**
61
     * The available EAV attribute sets.
62
     *
63
     * @var array
64
     */
65
    protected $attributeSets = array();
66
67
    /**
68
     * The mapping for the supported backend types (for the EAV entity) => persist methods.
69
     *
70
     * @var array
71
     */
72
    protected $backendTypes = array(
73
        BackendTypeKeys::BACKEND_TYPE_DATETIME => array('persistDatetimeAttribute', 'loadDatetimeAttribute'),
74
        BackendTypeKeys::BACKEND_TYPE_DECIMAL  => array('persistDecimalAttribute', 'loadDecimalAttribute'),
75
        BackendTypeKeys::BACKEND_TYPE_INT      => array('persistIntAttribute', 'loadIntAttribute'),
76
        BackendTypeKeys::BACKEND_TYPE_TEXT     => array('persistTextAttribute', 'loadTextAttribute'),
77
        BackendTypeKeys::BACKEND_TYPE_VARCHAR  => array('persistVarcharAttribute', 'loadVarcharAttribute')
78
    );
79
80
    /**
81
     * The default mappings for the user defined attributes, based on the attributes frontend input type.
82
     *
83
     * @var array
84
     */
85
    protected $defaultFrontendInputCallbackMappings = array();
86
87
    /**
88
     * Return's the default callback frontend input mappings for the user defined attributes.
89
     *
90
     * @return array The default frontend input callback mappings
91
     */
92
    public function getDefaultFrontendInputCallbackMappings()
93
    {
94
        return $this->defaultFrontendInputCallbackMappings;
95
    }
96
97
    /**
98
     * Intializes the previously loaded global data for exactly one bunch.
99
     *
100
     * @param string $serial The serial of the actual import
101
     *
102
     * @return void
103
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
104
     */
105
    public function setUp($serial)
106
    {
107
108
        // load the status of the actual import
109
        $status = $this->getRegistryProcessor()->getAttribute($serial);
110
111
        // load the global data we've prepared initially
112
        $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
113
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
114
        $this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES];
115
116
        // load the default frontend callback mappings from the child instance
117
        $defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings();
118
119
        // load the user defined attributes and add the callback mappings
120
        foreach ($this->getEavUserDefinedAttributes() as $eavAttribute) {
121
            // load attribute code and frontend input type
122
            $attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE];
123
            $frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT];
124
125
            // query whether or not the array for the mappings has been initialized
126
            if (!isset($this->callbackMappings[$attributeCode])) {
127
                $this->callbackMappings[$attributeCode] = array();
128
            }
129
130
            // set the appropriate callback mapping for the attributes input type
131
            if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) {
132
                $this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMappings[$frontendInput];
133
            }
134
        }
135
136
        // prepare the callbacks
137
        parent::setUp($serial);
138
    }
139
140
    /**
141
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
142
     *
143
     * @return array The mapping for the supported backend types
144
     */
145
    public function getBackendTypes()
146
    {
147
        return $this->backendTypes;
148
    }
149
150
    /**
151
     * Set's the attribute set of the product that has to be created.
152
     *
153
     * @param array $attributeSet The attribute set
154
     *
155
     * @return void
156
     */
157
    public function setAttributeSet(array $attributeSet)
158
    {
159
        $this->attributeSet = $attributeSet;
160
    }
161
162
    /**
163
     * Return's the attribute set of the product that has to be created.
164
     *
165
     * @return array The attribute set
166
     */
167
    public function getAttributeSet()
168
    {
169
        return $this->attributeSet;
170
    }
171
172
    /**
173
     * Cast's the passed value based on the backend type information.
174
     *
175
     * @param string $backendType The backend type to cast to
176
     * @param mixed  $value       The value to be casted
177
     *
178
     * @return mixed The casted value
179
     */
180
    public function castValueByBackendType($backendType, $value)
181
    {
182
183
        // cast the value to a valid timestamp
184
        if ($backendType === 'datetime') {
185
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
186
        }
187
188
        // cast the value to a float value
189
        if ($backendType === 'float') {
190
            return (float) $value;
191
        }
192
193
        // cast the value to an integer
194
        if ($backendType === 'int') {
195
            return (int) $value;
196
        }
197
198
        // we don't need to cast strings
199
        return $value;
200
    }
201
202
    /**
203
     * Return's the entity type code to be used.
204
     *
205
     * @return string The entity type code to be used
206
     */
207
    public function getEntityTypeCode()
208
    {
209
        return $this->getConfiguration()->getConfiguration()->getEntityTypeCode();
210
    }
211
212
    /**
213
     * Return's the attribute set with the passed attribute set name.
214
     *
215
     * @param string $attributeSetName The name of the requested attribute set
216
     *
217
     * @return array The attribute set data
218
     * @throws \Exception Is thrown, if the attribute set with the passed name is not available
219
     */
220
    public function getAttributeSetByAttributeSetName($attributeSetName)
221
    {
222
223
        // query whether or not attribute sets for the actualy entity type code are available
224
        if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) {
225
            // load the attribute sets for the actualy entity type code
226
            $attributSets = $this->attributeSets[$entityTypeCode];
227
228
            // query whether or not, the requested attribute set is available
229
            if (isset($attributSets[$attributeSetName])) {
230
                return $attributSets[$attributeSetName];
231
            }
232
        }
233
234
        // throw an exception, if not
235
        throw new \Exception(
236
            $this->appendExceptionSuffix(
237
                sprintf('Found invalid attribute set name "%s"', $attributeSetName)
238
            )
239
        );
240
    }
241
242
    /**
243
     * Return's the attributes for the attribute set of the product that has to be created.
244
     *
245
     * @return array The attributes
246
     * @throws \Exception Is thrown if the attributes for the actual attribute set are not available
247
     */
248
    public function getAttributes()
249
    {
250
251
        // query whether or not, the requested EAV attributes are available
252
        if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) {
253
            // load the attributes for the entity type code
254
            $attributes = $this->attributes[$entityTypeCode];
255
256
            // query whether or not attributes for the actual attribute set name
257
            if (isset($attributes[$attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
258
                return $attributes[$attributeSetName];
259
            }
260
261
            // throw an exception, if not
262
            throw new \Exception(
263
                $this->appendExceptionSuffix(
264
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
265
                )
266
            );
267
        }
268
269
        // throw an exception, if not
270
        throw new \Exception(
271
            $this->appendExceptionSuffix(
272
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
273
            )
274
        );
275
    }
276
277
    /**
278
     * Return's an array with the available user defined EAV attributes for the actual entity type.
279
     *
280
     * @return array The array with the user defined EAV attributes
281
     */
282
    public function getEavUserDefinedAttributes()
283
    {
284
285
        // query whether or not user defined attributes for the actualy entity type are available
286
        if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) {
287
            return $this->userDefinedAttributes[$entityTypeCode];
288
        }
289
290
        // throw an exception if an unknown entity type has been passed
291
        throw new \Exception(
292
            $this->appendExceptionSuffix(
293
                sprintf('Unknown entity type code "%s"', $entityTypeCode)
294
            )
295
        );
296
    }
297
298
    /**
299
     * Return's the EAV attribute with the passed attribute code.
300
     *
301
     * @param string $attributeCode The attribute code
302
     *
303
     * @return array The array with the EAV attribute
304
     * @throws \Exception Is thrown if the attribute with the passed code is not available
305
     */
306
    public function getEavAttributeByAttributeCode($attributeCode)
307
    {
308
309
        // load the attributes
310
        $attributes = $this->getAttributes();
311
312
        // query whether or not the attribute exists
313
        if (isset($attributes[$attributeCode])) {
314
            return $attributes[$attributeCode];
315
        }
316
317
        // throw an exception if the requested attribute is not available
318
        throw new \Exception(
319
            $this->appendExceptionSuffix(
320
                sprintf('Can\'t load attribute with code "%s"', $attributeCode)
321
            )
322
        );
323
    }
324
}
325