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

AbstractEavSubject   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 294
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 56
loc 294
ccs 82
cts 82
cp 1
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() 28 28 3
B getAttributes() 28 28 3
A getEavUserDefinedAttributes() 0 14 2
A getEavAttributeByAttributeCode() 0 18 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 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
     */
104 22
    public function setUp($serial)
105
    {
106
107
        // load the status of the actual import
108 22
        $status = $this->getRegistryProcessor()->getAttribute($serial);
109
110
        // load the global data we've prepared initially
111 22
        $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
112 22
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
113 22
        $this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES];
114
115
        // load the default frontend callback mappings from the child instance
116 22
        $defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings();
117
118
        // load the user defined attributes and add the callback mappings
119 22
        foreach ($this->getEavUserDefinedAttributes() as $eavAttribute) {
120
            // load attribute code and frontend input type
121 1
            $attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE];
122 1
            $frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT];
123
124
            // query whether or not the array for the mappings has been initialized
125 1
            if (!isset($this->callbackMappings[$attributeCode])) {
126 1
                $this->callbackMappings[$attributeCode] = array();
127 1
            }
128
129
            // set the appropriate callback mapping for the attributes input type
130 1
            if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) {
131 1
                $this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMappings[$frontendInput];
132 1
            }
133 22
        }
134
135
        // prepare the callbacks
136 22
        parent::setUp($serial);
137 22
    }
138
139
    /**
140
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
141
     *
142
     * @return array The mapping for the supported backend types
143
     */
144 1
    public function getBackendTypes()
145
    {
146 1
        return $this->backendTypes;
147
    }
148
149
    /**
150
     * Set's the attribute set of the product that has to be created.
151
     *
152
     * @param array $attributeSet The attribute set
153
     *
154
     * @return void
155
     */
156 5
    public function setAttributeSet(array $attributeSet)
157
    {
158 5
        $this->attributeSet = $attributeSet;
159 5
    }
160
161
    /**
162
     * Return's the attribute set of the product that has to be created.
163
     *
164
     * @return array The attribute set
165
     */
166 1
    public function getAttributeSet()
167
    {
168 1
        return $this->attributeSet;
169
    }
170
171
    /**
172
     * Cast's the passed value based on the backend type information.
173
     *
174
     * @param string $backendType The backend type to cast to
175
     * @param mixed  $value       The value to be casted
176
     *
177
     * @return mixed The casted value
178
     */
179 9
    public function castValueByBackendType($backendType, $value)
180
    {
181
182
        // cast the value to a valid timestamp
183 9
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) {
184 2
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
185
        }
186
187
        // cast the value to a float value
188 7
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT) {
189 2
            return (float) $value;
190
        }
191
192
        // cast the value to an integer
193 5
        if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) {
194 3
            return (int) $value;
195
        }
196
197
        // we don't need to cast strings
198 2
        return $value;
199
    }
200
201
    /**
202
     * Return's the entity type code to be used.
203
     *
204
     * @return string The entity type code to be used
205
     */
206 22
    public function getEntityTypeCode()
207
    {
208 22
        return $this->getConfiguration()->getConfiguration()->getEntityTypeCode();
209
    }
210
211
    /**
212
     * Return's the attribute set with the passed attribute set name.
213
     *
214
     * @param string $attributeSetName The name of the requested attribute set
215
     *
216
     * @return array The attribute set data
217
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
218
     */
219 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...
220
    {
221
222
        // query whether or not attribute sets for the actualy entity type code are available
223 3
        if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) {
224
            // load the attribute sets for the actualy entity type code
225 2
            $attributSets = $this->attributeSets[$entityTypeCode];
226
227
            // query whether or not, the requested attribute set is available
228 2
            if (isset($attributSets[$attributeSetName])) {
229 1
                return $attributSets[$attributeSetName];
230
            }
231
232
            // throw an exception, if not
233 1
            throw new \Exception(
234 1
                $this->appendExceptionSuffix(
235 1
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
236 1
                )
237 1
            );
238
        }
239
240
        // throw an exception, if not
241 1
        throw new \Exception(
242 1
            $this->appendExceptionSuffix(
243 1
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
244 1
            )
245 1
        );
246
    }
247
248
    /**
249
     * Return's the attributes for the attribute set of the product that has to be created.
250
     *
251
     * @return array The attributes
252
     * @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available
253
     */
254 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...
255
    {
256
257
        // query whether or not, the requested EAV attributes are available
258 5
        if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) {
259
            // load the attributes for the entity type code
260 4
            $attributes = $this->attributes[$entityTypeCode];
261
262
            // query whether or not attributes for the actual attribute set name
263 4
            if (isset($attributes[$attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
264 3
                return $attributes[$attributeSetName];
265
            }
266
267
            // throw an exception, if not
268 1
            throw new \Exception(
269 1
                $this->appendExceptionSuffix(
270 1
                    sprintf('Found invalid attribute set name "%s"', $attributeSetName)
271 1
                )
272 1
            );
273
        }
274
275
        // throw an exception, if not
276 1
        throw new \Exception(
277 1
            $this->appendExceptionSuffix(
278 1
                sprintf('Found invalid entity type code "%s"', $entityTypeCode)
279 1
            )
280 1
        );
281
    }
282
283
    /**
284
     * Return's an array with the available user defined EAV attributes for the actual entity type.
285
     *
286
     * @return array The array with the user defined EAV attributes
287
     */
288 22
    public function getEavUserDefinedAttributes()
289
    {
290
291
        // initialize the array with the user defined EAV attributes
292 22
        $eavUserDefinedAttributes = array();
293
294
        // query whether or not user defined EAV attributes for the actualy entity type are available
295 22
        if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) {
296 2
            $eavUserDefinedAttributes = $this->userDefinedAttributes[$entityTypeCode];
297 2
        }
298
299
        // return the array with the user defined EAV attributes
300 22
        return $eavUserDefinedAttributes;
301
    }
302
303
    /**
304
     * Return's the EAV attribute with the passed attribute code.
305
     *
306
     * @param string $attributeCode The attribute code
307
     *
308
     * @return array The array with the EAV attribute
309
     * @throws \Exception Is thrown if the attribute with the passed code is not available
310
     */
311 2
    public function getEavAttributeByAttributeCode($attributeCode)
312
    {
313
314
        // load the attributes
315 2
        $attributes = $this->getAttributes();
316
317
        // query whether or not the attribute exists
318 2
        if (isset($attributes[$attributeCode])) {
319 1
            return $attributes[$attributeCode];
320
        }
321
322
        // throw an exception if the requested attribute is not available
323 1
        throw new \Exception(
324 1
            $this->appendExceptionSuffix(
325 1
                sprintf('Can\'t load attribute with code "%s"', $attributeCode)
326 1
            )
327 1
        );
328
    }
329
}
330