Completed
Pull Request — master (#89)
by Tim
03:51
created

AbstractEavSubject::getEavUserDefinedAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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
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 22
    public function getDefaultFrontendInputCallbackMappings()
93
    {
94 22
        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 22
    public function setUp($serial)
106
    {
107
108
        // load the status of the actual import
109 22
        $status = $this->getRegistryProcessor()->getAttribute($serial);
110
111
        // load the global data we've prepared initially
112 22
        $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
113 22
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
114 22
        $this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES];
115
116
        // load the default frontend callback mappings from the child instance
117 22
        $defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings();
118
119
        // load the user defined attributes and add the callback mappings
120 22
        foreach ($this->getEavUserDefinedAttributes() as $eavAttribute) {
121
            // load attribute code and frontend input type
122 1
            $attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE];
123 1
            $frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT];
124
125
            // query whether or not the array for the mappings has been initialized
126 1
            if (!isset($this->callbackMappings[$attributeCode])) {
127 1
                $this->callbackMappings[$attributeCode] = array();
128
            }
129
130
            // set the appropriate callback mapping for the attributes input type
131 1
            if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) {
132 1
                $this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMappings[$frontendInput];
133
            }
134
        }
135
136
        // prepare the callbacks
137 22
        parent::setUp($serial);
138 22
    }
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 1
    public function getBackendTypes()
146
    {
147 1
        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 5
    public function setAttributeSet(array $attributeSet)
158
    {
159 5
        $this->attributeSet = $attributeSet;
160 5
    }
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 1
    public function getAttributeSet()
168
    {
169 1
        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 9
    public function castValueByBackendType($backendType, $value)
181
    {
182
183
        // cast the value to a valid timestamp
184 9
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) {
185 2
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
186
        }
187
188
        // cast the value to a float value
189 7
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT) {
190 2
            return (float) $value;
191
        }
192
193
        // cast the value to an integer
194 5
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) {
195 3
            return (int) $value;
196
        }
197
198
        // we don't need to cast strings
199 2
        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 22
    public function getEntityTypeCode()
208
    {
209 22
        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 or the given entity type with the passed name is not available
219
     */
220 3 View Code Duplication
    public function getAttributeSetByAttributeSetName($attributeSetName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
    {
222
223
        // query whether or not attribute sets for the actualy entity type code are available
224 3
        if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) {
225
            // load the attribute sets for the actualy entity type code
226 2
            $attributSets = $this->attributeSets[$entityTypeCode];
227
228
            // query whether or not, the requested attribute set is available
229 2
            if (isset($attributSets[$attributeSetName])) {
230 1
                return $attributSets[$attributeSetName];
231
            }
232
233
            // throw an exception, if not
234 1
            throw new \Exception(
235 1
                $this->appendExceptionSuffix(
236 1
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
237
                )
238
            );
239
        }
240
241
        // throw an exception, if not
242 1
        throw new \Exception(
243 1
            $this->appendExceptionSuffix(
244 1
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
245
            )
246
        );
247
    }
248
249
    /**
250
     * Return's the attributes for the attribute set of the product that has to be created.
251
     *
252
     * @return array The attributes
253
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
254
     */
255 5 View Code Duplication
    public function getAttributes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
256
    {
257
258
        // query whether or not, the requested EAV attributes are available
259 5
        if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) {
260
            // load the attributes for the entity type code
261 4
            $attributes = $this->attributes[$entityTypeCode];
262
263
            // query whether or not attributes for the actual attribute set name
264 4
            if (isset($attributes[$attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
265 3
                return $attributes[$attributeSetName];
266
            }
267
268
            // throw an exception, if not
269 1
            throw new \Exception(
270 1
                $this->appendExceptionSuffix(
271 1
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
272
                )
273
            );
274
        }
275
276
        // throw an exception, if not
277 1
        throw new \Exception(
278 1
            $this->appendExceptionSuffix(
279 1
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
280
            )
281
        );
282
    }
283
284
    /**
285
     * Return's an array with the available user defined EAV attributes for the actual entity type.
286
     *
287
     * @return array The array with the user defined EAV attributes
288
     */
289 22
    public function getEavUserDefinedAttributes()
290
    {
291
292
        // initialize the array with the user defined EAV attributes
293 22
        $eavUserDefinedAttributes = array();
294
295
        // query whether or not user defined EAV attributes for the actualy entity type are available
296 22
        if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) {
297 2
            $eavUserDefinedAttributes = $this->userDefinedAttributes[$entityTypeCode];
298
        }
299
300
        // return the array with the user defined EAV attributes
301 22
        return $eavUserDefinedAttributes;
302
    }
303
304
    /**
305
     * Return's the EAV attribute with the passed attribute code.
306
     *
307
     * @param string $attributeCode The attribute code
308
     *
309
     * @return array The array with the EAV attribute
310
     * @throws \Exception Is thrown if the attribute with the passed code is not available
311
     */
312 2
    public function getEavAttributeByAttributeCode($attributeCode)
313
    {
314
315
        // load the attributes
316 2
        $attributes = $this->getAttributes();
317
318
        // query whether or not the attribute exists
319 2
        if (isset($attributes[$attributeCode])) {
320 1
            return $attributes[$attributeCode];
321
        }
322
323
        // throw an exception if the requested attribute is not available
324 1
        throw new \Exception(
325 1
            $this->appendExceptionSuffix(
326 1
                sprintf('Can\'t load attribute with code "%s"', $attributeCode)
327
            )
328
        );
329
    }
330
}
331