Completed
Push — 17.x ( baef73...bc0415 )
by Tim
05:19
created

CatalogAttributeUpdateObserver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 49
ccs 8
cts 12
cp 0.6667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B initializeAttribute() 0 26 6
A loadCatalogAttribute() 0 4 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\CatalogAttributeUpdateObserver
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\Attribute\Utils\MemberNames;
24
25
/**
26
 * Observer that add/update's the EAV catalog attribute itself.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-attribute
32
 * @link      http://www.techdivision.com
33
 */
34
class CatalogAttributeUpdateObserver extends CatalogAttributeObserver
35
{
36
37
    /**
38
     * Initialize the attribute with the passed attributes and returns an instance.
39
     *
40
     * @param array $attr The attribute attributes
41
     *
42
     * @return array The initialized attribute
43
     */
44 2
    protected function initializeAttribute(array $attr)
45
    {
46
47
        // try to load the EAV catalog attribute with the attribute code
48 2
        if ($attribute = $this->loadCatalogAttribute($attr[MemberNames::ATTRIBUTE_ID])) {
49
            // unserialize the additional data value if available
50 2
            if (is_array($attribute[MemberNames::ADDITIONAL_DATA]) && $attribute[MemberNames::ADDITIONAL_DATA] !== null) {
51
                // merge the additional data if available
52
                $attribute[MemberNames::ADDITIONAL_DATA] = array_merge(
53
                    $attr[MemberNames::ADDITIONAL_DATA],
54
                    json_decode($attribute[MemberNames::ADDITIONAL_DATA])
55
                );
56 2
            } elseif (!is_array($attribute[MemberNames::ADDITIONAL_DATA]) && $attribute[MemberNames::ADDITIONAL_DATA] !== null) {
57
                // unserialize and override the additional data
58 2
                $attribute[MemberNames::ADDITIONAL_DATA] = json_decode($attribute[MemberNames::ADDITIONAL_DATA]);
59
            } else {
60
                // nothing here
61
            }
62
63
            // merge the attributes into the entity
64 2
            return $this->serializeAdditionalData($this->mergeEntity($attribute, $attr));
65
        }
66
67
        //  serialize the additional data and return the attributes
68
        return parent::initializeAttribute($attr);
69
    }
70
71
    /**
72
     * Load's and return's the EAV catalog attribute with the passed attribute ID.
73
     *
74
     * @param string $attributeId The ID of the EAV catalog attribute to load
75
     *
76
     * @return array The EAV catalog attribute
77
     */
78 2
    protected function loadCatalogAttribute($attributeId)
79
    {
80 2
        return $this->getAttributeBunchProcessor()->loadCatalogAttribute($attributeId);
81
    }
82
}
83