Completed
Push — 17.x ( ae7520...5bcc83 )
by Tim
01:43
created

AttributeOptionUpdateObserver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 27.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 19
loc 68
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeEntity() 0 12 2
A initializeAttribute() 19 19 2
A loadAttributeOptionByEntityTypeIdAndAttributeCodeAndStoreIdAndValue() 0 4 1

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\Attribute\Observers\AttributeOptionUpdateObserver
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-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Attribute\Utils\ColumnKeys;
25
use TechDivision\Import\Attribute\Utils\MemberNames;
26
27
/**
28
 * Observer that update's the attribute options found in the additional CSV file.
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-attribute
34
 * @link      http://www.techdivision.com
35
 */
36
class AttributeOptionUpdateObserver extends AttributeOptionObserver
37
{
38
39
    /**
40
     * Merge's and return's the entity with the passed attributes and set's the
41
     * passed status.
42
     *
43
     * @param array       $entity        The entity to merge the attributes into
44
     * @param array       $attr          The attributes to be merged
45
     * @param string|null $changeSetName The change set name to use
46
     *
47
     * @return array The merged entity
48
     */
49
    protected function mergeEntity(array $entity, array $attr, $changeSetName = null)
50
    {
51
52
        // query whether or not the sort order has been specified, if not use the value of the
53
        // existing entity. This allows the customer to change the order in the Magento backend
54
        if ($this->hasValue(ColumnKeys::SORT_ORDER) === false) {
55
            $attr[MemberNames::SORT_ORDER] = $entity[MemberNames::SORT_ORDER];
56
        }
57
58
        // invoke the parent method and return the merged entity
59
        return parent::mergeEntity($entity, $attr, $changeSetName);
60
    }
61
62
    /**
63
     * Initialize the EAV attribute option with the passed attributes and returns an instance.
64
     *
65
     * @param array $attr The EAV attribute option attributes
66
     *
67
     * @return array The initialized EAV attribute option
68
     */
69 View Code Duplication
    protected function initializeAttribute(array $attr)
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...
70
    {
71
72
        // load the entity type ID for the value from the system configuration
73
        $entityTypeId = $this->getEntityTypeId();
74
75
        // initialize the data to load the EAV attribute option
76
        $value = $this->getValue(ColumnKeys::VALUE);
77
        $storeId = $this->getRowStoreId(StoreViewCodes::ADMIN);
78
        $attributeCode = $this->getValue(ColumnKeys::ATTRIBUTE_CODE);
79
80
        // try to load the EAV attribute option
81
        if ($attributeOption = $this->loadAttributeOptionByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value)) {
82
            return $this->mergeEntity($attributeOption, $attr);
83
        }
84
85
        // simply return the attributes
86
        return $attr;
87
    }
88
89
    /**
90
     * Load's and return's the EAV attribute option with the passed entity type ID, code, store ID and value.
91
     *
92
     * @param string  $entityTypeId  The entity type ID of the EAV attribute to load the option for
93
     * @param string  $attributeCode The code of the EAV attribute option to load
94
     * @param integer $storeId       The store ID of the attribute option to load
95
     * @param string  $value         The value of the attribute option to load
96
     *
97
     * @return array The EAV attribute option
98
     */
99
    protected function loadAttributeOptionByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value)
100
    {
101
        return $this->getAttributeBunchProcessor()->loadAttributeOptionByEntityTypeIdAndAttributeCodeAndStoreIdAndValue($entityTypeId, $attributeCode, $storeId, $value);
102
    }
103
}
104