Completed
Pull Request — master (#4)
by Tim
02:29
created

AttributeOptionObserver::initializeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeOptionObserver
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\ColumnKeys;
24
use TechDivision\Import\Attribute\Utils\MemberNames;
25
use TechDivision\Import\Subjects\SubjectInterface;
26
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface;
27
28
/**
29
 * Observer that create's the attribute options found in the additional CSV file.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-attribute
35
 * @link      http://www.techdivision.com
36
 */
37
class AttributeOptionObserver extends AbstractAttributeImportObserver
38
{
39
40
    /**
41
     * The attribute processor instance.
42
     *
43
     * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface
44
     */
45
    protected $attributeBunchProcessor;
46
47
    /**
48
     * Initializes the observer with the passed subject instance.
49
     *
50
     * @param \TechDivision\Import\Subjects\SubjectInterface                           $subject                 The observer's subject instance
51
     * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance
52
     */
53
    public function __construct(
54
        SubjectInterface $subject,
55
        AttributeBunchProcessorInterface $attributeBunchProcessor
56
    ) {
57
58
        // pass the subject through to the parend observer
59
        parent::__construct($subject);
60
61
        // initialize the attribute bunch processor
62
        $this->attributeBunchProcessor = $attributeBunchProcessor;
63
    }
64
65
    /**
66
     * Process the observer's business logic.
67
     *
68
     * @return void
69
     */
70
    protected function process()
71
    {
72
73
        // query whether or not, we've found a new attribute code => means we've found a new attribute
74
        if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) {
75
            return;
76
        }
77
78
        // prepare the attribue values
79
        $attributeOption = $this->initializeAttribute($this->prepareAttributes());
80
81
        // insert the attribute option and set the option ID
82
        $this->setLastOptionId($this->persistAttributeOption($attributeOption));
83
    }
84
85
    /**
86
     * Prepare the attributes of the entity that has to be persisted.
87
     *
88
     * @return array The prepared attributes
89
     */
90 View Code Duplication
    protected function prepareAttributes()
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...
91
    {
92
93
        // load the attribute ID
94
        $attribute = $this->loadAttributeByAttributeCode($this->getValue(ColumnKeys::ATTRIBUTE_CODE));
95
        $attributeId = $attribute[MemberNames::ATTRIBUTE_ID];
96
97
        // load the sort order
98
        $sortOrder = $this->getValue(ColumnKeys::POSITION);
99
100
        // return the prepared attribute option
101
        return $this->initializeEntity(
102
            array(
103
                MemberNames::ATTRIBUTE_ID  => $attributeId,
104
                MemberNames::SORT_ORDER    => $sortOrder
105
            )
106
        );
107
    }
108
109
    /**
110
     * Initialize the attribute with the passed attributes and returns an instance.
111
     *
112
     * @param array $attr The attribute attributes
113
     *
114
     * @return array The initialized attribute
115
     */
116
    protected function initializeAttribute(array $attr)
117
    {
118
        return $attr;
119
    }
120
121
    /**
122
     * Return's the attribute bunch processor instance.
123
     *
124
     * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance
125
     */
126
    protected function getAttributeBunchProcessor()
127
    {
128
        return $this->attributeBunchProcessor;
129
    }
130
131
    /**
132
     * Set's the ID of the option that has been created recently.
133
     *
134
     * @param integer $lastOptionId The option ID
135
     *
136
     * @return void
137
     */
138
    protected function setLastOptionId($lastOptionId)
139
    {
140
        $this->getSubject()->setLastOptionId($lastOptionId);
141
    }
142
143
    /**
144
     * Load's and return's the EAV attribute with the passed code.
145
     *
146
     * @param string $attributeCode The code of the EAV attribute to load
147
     *
148
     * @return array The EAV attribute
149
     */
150
    protected function loadAttributeByAttributeCode($attributeCode)
151
    {
152
        return $this->getAttributeBunchProcessor()->loadAttributeByAttributeCode($attributeCode);
153
    }
154
155
    /**
156
     * Persist the passed attribute option.
157
     *
158
     * @param array $attributeOption The attribute option to persist
159
     *
160
     * @return void
161
     */
162
    protected function persistAttributeOption(array $attributeOption)
163
    {
164
        return $this->getAttributeBunchProcessor()->persistAttributeOption($attributeOption);
165
    }
166
}
167