Completed
Push — master ( bce332...18fde1 )
by Tim
9s
created

AbstractProductSubject   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 412
Duplicated Lines 8.74 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 9.3%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 2
dl 36
loc 412
ccs 8
cts 86
cp 0.093
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A setProductProcessor() 0 4 1
A getProductProcessor() 0 4 1
A setLastSku() 0 4 1
A getLastSku() 0 4 1
A setLastEntityId() 0 4 1
A getLastEntityId() 0 4 1
A setStoreViewCode() 0 4 1
A getStoreViewCode() 0 14 3
A getDefaultStore() 0 4 1
A setUp() 0 19 1
A tearDown() 0 16 1
A getAttributes() 0 14 2
A getRowStoreId() 14 14 2
A getTaxClassIdByTaxClassName() 11 11 2
A getStoreWebsiteIdByCode() 11 11 2
A getAttributeSetByAttributeSetName() 0 10 2
A getCategoryByPath() 0 11 2
A getCategory() 0 13 3
A getRootCategory() 0 17 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\Product\Subjects\AbstractProductSubject
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-product
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Subjects;
22
23
use TechDivision\Import\Subjects\AbstractSubject;
24
use TechDivision\Import\Utils\RegistryKeys;
25
use TechDivision\Import\Utils\StoreViewCodes;
26
use TechDivision\Import\Product\Utils\MemberNames;
27
use TechDivision\Import\Product\Services\ProductProcessorInterface;
28
29
/**
30
 * The abstract product subject implementation that provides basic product
31
 * handling business logic.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-product
37
 * @link      http://www.techdivision.com
38
 */
39
abstract class AbstractProductSubject extends AbstractSubject
40
{
41
42
    /**
43
     * The processor to read/write the necessary product data.
44
     *
45
     * @var \TechDivision\Import\Product\Services\ProductProcessorInterface
46
     */
47
    protected $productProcessor;
48
49
    /**
50
     * The available EAV attribute sets.
51
     *
52
     * @var array
53
     */
54
    protected $attributeSets = array();
55
56
    /**
57
     * The available stores.
58
     *
59
     * @var array
60
     */
61
    protected $stores = array();
62
63
    /**
64
     * The available store websites.
65
     *
66
     * @var array
67
     */
68
    protected $storeWebsites = array();
69
70
    /**
71
     * The available EAV attributes, grouped by their attribute set and the attribute set name as keys.
72
     *
73
     * @var array
74
     */
75
    protected $attributes = array();
76
77
    /**
78
     * The available tax classes.
79
     *
80
     * @var array
81
     */
82
    protected $taxClasses = array();
83
84
    /**
85
     * The available categories.
86
     *
87
     * @var array
88
     */
89
    protected $categories = array();
90
91
    /**
92
     * The available root categories.
93
     *
94
     * @var array
95
     */
96
    protected $rootCategories = array();
97
98
    /**
99
     * The ID of the product that has been created recently.
100
     *
101
     * @var string
102
     */
103
    protected $lastEntityId;
104
105
    /**
106
     * The SKU of the product that has been created recently.
107
     *
108
     * @var string
109
     */
110
    protected $lastSku;
111
112
    /**
113
     * The store view code the create the product/attributes for.
114
     *
115
     * @var string
116
     */
117
    protected $storeViewCode;
118
119
    /**
120
     * The default store.
121
     *
122
     * @var array
123
     */
124
    protected $defaultStore;
125
126
    /**
127
     * Set's the product processor instance.
128
     *
129
     * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance
130
     *
131
     * @return void
132
     */
133 3
    public function setProductProcessor(ProductProcessorInterface $productProcessor)
134
    {
135 3
        $this->productProcessor = $productProcessor;
136 3
    }
137
138
    /**
139
     * Return's the product processor instance.
140
     *
141
     * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance
142
     */
143 3
    public function getProductProcessor()
144
    {
145 3
        return $this->productProcessor;
146
    }
147
148
    /**
149
     * Set's the SKU of the last imported product.
150
     *
151
     * @param string $lastSku The SKU
152
     *
153
     * @return void
154
     */
155
    public function setLastSku($lastSku)
156
    {
157
        $this->lastSku = $lastSku;
158
    }
159
160
    /**
161
     * Return's the SKU of the last imported product.
162
     *
163
     * @return string|null The SKU
164
     */
165
    public function getLastSku()
166
    {
167
        return $this->lastSku;
168
    }
169
170
    /**
171
     * Set's the ID of the product that has been created recently.
172
     *
173
     * @param string $lastEntityId The entity ID
174
     *
175
     * @return void
176
     */
177
    public function setLastEntityId($lastEntityId)
178
    {
179
        $this->lastEntityId = $lastEntityId;
180
    }
181
182
    /**
183
     * Return's the ID of the product that has been created recently.
184
     *
185
     * @return string The entity Id
186
     */
187
    public function getLastEntityId()
188
    {
189
        return $this->lastEntityId;
190
    }
191
192
    /**
193
     * Set's the store view code the create the product/attributes for.
194
     *
195
     * @param string $storeViewCode The store view code
196
     *
197
     * @return void
198
     */
199 3
    public function setStoreViewCode($storeViewCode)
200
    {
201 3
        $this->storeViewCode = $storeViewCode;
202 3
    }
203
204
    /**
205
     * Return's the store view code the create the product/attributes for.
206
     *
207
     * @param string|null $default The default value to return, if the store view code has not been set
208
     *
209
     * @return string The store view code
210
     */
211
    public function getStoreViewCode($default = null)
212
    {
213
214
        // return the store view code, if available
215
        if ($this->storeViewCode != null) {
216
            return $this->storeViewCode;
217
        }
218
219
        // if NOT and a default code is available
220
        if ($default != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $default of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
221
            // return the default value
222
            return $default;
223
        }
224
    }
225
226
    /**
227
     * Return's the default store.
228
     *
229
     * @return array The default store
230
     */
231
    public function getDefaultStore()
232
    {
233
        return $this->defaultStore;
234
    }
235
236
    /**
237
     * Intializes the previously loaded global data for exactly one bunch.
238
     *
239
     * @return void
240
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
241
     */
242
    public function setUp()
243
    {
244
245
        // load the status of the actual import
246
        $status = $this->getRegistryProcessor()->getAttribute($this->getSerial());
247
248
        // load the global data we've prepared initially
249
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
250
        $this->storeWebsites =  $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES];
251
        $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
252
        $this->stores = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORES];
253
        $this->taxClasses = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::TAX_CLASSES];
254
        $this->categories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::CATEGORIES];
255
        $this->rootCategories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ROOT_CATEGORIES];
256
        $this->defaultStore = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::DEFAULT_STORE];
257
258
        // prepare the callbacks
259
        parent::setUp();
260
    }
261
262
    /**
263
     * Clean up the global data after importing the bunch.
264
     *
265
     * @return void
266
     */
267
    public function tearDown()
268
    {
269
270
        // load the registry processor
271
        $registryProcessor = $this->getRegistryProcessor();
272
273
        // update the status
274
        $registryProcessor->mergeAttributesRecursive(
275
            $this->getSerial(),
276
            array(
277
                RegistryKeys::FILES => array(
278
                    $this->getFilename() => array(RegistryKeys::STATUS => 1)
0 ignored issues
show
Bug introduced by
The method getFilename() does not seem to exist on object<TechDivision\Impo...AbstractProductSubject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
279
                )
280
            )
281
        );
282
    }
283
284
    /**
285
     * Return's the attributes for the attribute set of the product that has to be created.
286
     *
287
     * @return array The attributes
288
     * @throws \Exception Is thrown if the attributes for the actual attribute set are not available
289
     */
290
    public function getAttributes()
291
    {
292
293
        // load the attribute set of the product that has to be created.
294
        $attributeSet = $this->getAttributeSet();
0 ignored issues
show
Bug introduced by
The method getAttributeSet() does not exist on TechDivision\Import\Prod...\AbstractProductSubject. Did you maybe mean getAttributes()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
295
296
        // query whether or not, the requested EAV attributes are available
297
        if (isset($this->attributes[$attributeSetName = $attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
298
            return $this->attributes[$attributeSetName];
299
        }
300
301
        // throw an exception, if not
302
        throw new \Exception(sprintf('Found invalid attribute set name %s', $attributeSetName));
303
    }
304
305
    /**
306
     * Return's the store ID of the actual row.
307
     *
308
     * @return integer The ID of the actual store
309
     * @throws \Exception Is thrown, if the store with the actual code is not available
310
     */
311 View Code Duplication
    public function getRowStoreId()
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...
312
    {
313
314
        // load the store view code the create the product/attributes for
315
        $storeViewCode = $this->getStoreViewCode(StoreViewCodes::ADMIN);
316
317
        // query whether or not, the requested store is available
318
        if (isset($this->stores[$storeViewCode])) {
319
            return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID];
320
        }
321
322
        // throw an exception, if not
323
        throw new \Exception(sprintf('Found invalid store view code %s', $storeViewCode));
324
    }
325
326
    /**
327
     * Return's the tax class ID for the passed tax class name.
328
     *
329
     * @param string $taxClassName The tax class name to return the ID for
330
     *
331
     * @return integer The tax class ID
332
     * @throws \Exception Is thrown, if the tax class with the requested name is not available
333
     */
334 View Code Duplication
    public function getTaxClassIdByTaxClassName($taxClassName)
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...
335
    {
336
337
        // query whether or not, the requested tax class is available
338
        if (isset($this->taxClasses[$taxClassName])) {
339
            return (integer) $this->taxClasses[$taxClassName][MemberNames::CLASS_ID];
340
        }
341
342
        // throw an exception, if not
343
        throw new \Exception(sprintf('Found invalid tax class name %s', $taxClassName));
344
    }
345
346
    /**
347
     * Return's the store website for the passed code.
348
     *
349
     * @param string $code The code of the store website to return the ID for
350
     *
351
     * @return integer The store website ID
352
     * @throws \Exception Is thrown, if the store website with the requested code is not available
353
     */
354 View Code Duplication
    public function getStoreWebsiteIdByCode($code)
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...
355
    {
356
357
        // query whether or not, the requested store website is available
358
        if (isset($this->storeWebsites[$code])) {
359
            return (integer) $this->storeWebsites[$code][MemberNames::WEBSITE_ID];
360
        }
361
362
        // throw an exception, if not
363
        throw new \Exception(sprintf('Found invalid website code %s', $code));
364
    }
365
366
    /**
367
     * Return's the attribute set with the passed attribute set name.
368
     *
369
     * @param string $attributeSetName The name of the requested attribute set
370
     *
371
     * @return array The attribute set data
372
     * @throws \Exception Is thrown, if the attribute set with the passed name is not available
373
     */
374
    public function getAttributeSetByAttributeSetName($attributeSetName)
375
    {
376
        // query whether or not, the requested attribute set is available
377
        if (isset($this->attributeSets[$attributeSetName])) {
378
            return $this->attributeSets[$attributeSetName];
379
        }
380
381
        // throw an exception, if not
382
        throw new \Exception(sprintf('Found invalid attribute set name %s', $attributeSetName));
383
    }
384
385
    /**
386
     * Return's the category with the passed path.
387
     *
388
     * @param string $path The path of the category to return
389
     *
390
     * @return array The category
391
     * @throws \Exception Is thrown, if the requested category is not available
392
     */
393
    public function getCategoryByPath($path)
394
    {
395
396
        // query whether or not the category with the passed path exists
397
        if (isset($this->categories[$path])) {
398
            return $this->categories[$path];
399
        }
400
401
        // throw an exception, if not
402
        throw new \Exception(sprintf('Found invalid category path %s', $path));
403
    }
404
405
    /**
406
     * Return's the category with the passed ID.
407
     *
408
     * @param integer $categoryId The ID of the category to return
409
     *
410
     * @return array The category data
411
     * @throws \Exception Is thrown, if the category is not available
412
     */
413
    public function getCategory($categoryId)
414
    {
415
416
        // try to load the category with the passed ID
417
        foreach ($this->categories as $category) {
418
            if ($category[MemberNames::ENTITY_ID] == $categoryId) {
419
                return $category;
420
            }
421
        }
422
423
        // throw an exception if the category is NOT available
424
        throw new \Exception(sprintf('Can\'t load category with ID %d', $categoryId));
425
    }
426
427
    /**
428
     * Return's the root category for the actual view store.
429
     *
430
     * @return array The store's root category
431
     * @throws \Exception Is thrown if the root category for the passed store code is NOT available
432
     */
433
    public function getRootCategory()
434
    {
435
436
        // load the default store
437
        $defaultStore = $this->getDefaultStore();
438
439
        // load the actual store view code
440
        $storeViewCode = $this->getStoreViewCode($defaultStore[MemberNames::CODE]);
441
442
        // query weather or not we've a root category or not
443
        if (isset($this->rootCategories[$storeViewCode])) {
444
            return $this->rootCategories[$storeViewCode];
445
        }
446
447
        // throw an exception if the root category is NOT available
448
        throw new \Exception(sprintf('Root category for %s is not available', $storeViewCode));
449
    }
450
}
451