Completed
Push — master ( 7dfda5...6b6b2c )
by Tim
10s
created

AbstractEavSubject   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 287
Duplicated Lines 16.72 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 48
loc 287
ccs 0
cts 108
cp 0
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFrontendInputCallbackMappings() 0 4 1
B setUp() 0 34 4
A getBackendTypes() 0 4 1
A setAttributeSet() 0 4 1
A getAttributeSet() 0 4 1
A castValueByBackendType() 0 21 4
A getEntityTypeCode() 0 4 1
B getAttributeSetByAttributeSetName() 24 24 3
B getAttributes() 24 24 3
A getEavUserDefinedAttributes() 0 11 2
A getEavAttributeByAttributeCode() 0 21 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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
        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
            sprintf(
237
                'Found invalid attribute set name %s in file %s on line %d',
238
                $attributeSetName,
239
                $this->getFilename(),
240
                $this->getLineNumber()
241
            )
242
        );
243
    }
244
245
    /**
246
     * Return's the attributes for the attribute set of the product that has to be created.
247
     *
248
     * @return array The attributes
249
     * @throws \Exception Is thrown if the attributes for the actual attribute set are not available
250
     */
251 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...
252
    {
253
254
        // query whether or not, the requested EAV attributes are available
255
        if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) {
256
            // load the attributes for the entity type code
257
            $attributes = $this->attributes[$entityTypeCode];
258
259
            // query whether or not attributes for the actual attribute set name
260
            if (isset($attributes[$attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
261
                return $attributes[$attributeSetName];
262
            }
263
        }
264
265
        // throw an exception, if not
266
        throw new \Exception(
267
            sprintf(
268
                'Found invalid attribute set name "%s" in file %s on line %d',
269
                $attributeSetName,
270
                $this->getFilename(),
271
                $this->getLineNumber()
272
            )
273
        );
274
    }
275
276
    /**
277
     * Return's an array with the available user defined EAV attributes for the actual entity type.
278
     *
279
     * @return array The array with the user defined EAV attributes
280
     */
281
    public function getEavUserDefinedAttributes()
282
    {
283
284
        // query whether or not user defined attributes for the actualy entity type are available
285
        if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) {
286
            return $this->userDefinedAttributes[$entityTypeCode];
287
        }
288
289
        // throw an exception if an unknown entity type has been passed
290
        throw new \Exception(sprintf('Unknown entity type code %s', $entityTypeCode));
291
    }
292
293
    /**
294
     * Return's the EAV attribute with the passed attribute code.
295
     *
296
     * @param string $attributeCode The attribute code
297
     *
298
     * @return array The array with the EAV attribute
299
     * @throws \Exception Is thrown if the attribute with the passed code is not available
300
     */
301
    public function getEavAttributeByAttributeCode($attributeCode)
302
    {
303
304
        // load the attributes
305
        $attributes = $this->getAttributes();
306
307
        // query whether or not the attribute exists
308
        if (isset($attributes[$attributeCode])) {
309
            return $attributes[$attributeCode];
310
        }
311
312
        // throw an exception if the requested attribute is not available
313
        throw new \Exception(
314
            sprintf(
315
                'Can\'t load attribute with code "%s" in file %s and line %d',
316
                $attributeCode,
317
                $this->getFilename(),
318
                $this->getLineNumber()
319
            )
320
        );
321
    }
322
}
323