1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Observers\AttributeOptionValueObserver |
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
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
27
|
|
|
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Observer that create's the attribute option values found in the additional CSV file. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link https://github.com/techdivision/import-attribute |
36
|
|
|
* @link http://www.techdivision.com |
37
|
|
|
*/ |
38
|
|
|
class AttributeOptionValueObserver extends AbstractAttributeImportObserver |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The attribute processor instance. |
43
|
|
|
* |
44
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
45
|
|
|
*/ |
46
|
|
|
protected $attributeBunchProcessor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Initializes the observer with the passed subject instance. |
50
|
|
|
* |
51
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject instance |
52
|
|
|
* @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
SubjectInterface $subject, |
56
|
|
|
AttributeBunchProcessorInterface $attributeBunchProcessor |
57
|
|
|
) { |
58
|
|
|
|
59
|
|
|
// pass the subject through to the parend observer |
60
|
|
|
parent::__construct($subject); |
61
|
|
|
|
62
|
|
|
// initialize the attribute bunch processor |
63
|
|
|
$this->attributeBunchProcessor = $attributeBunchProcessor; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Process the observer's business logic. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
protected function process() |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
// prepare the store view code |
75
|
|
|
$this->prepareStoreViewCode(); |
76
|
|
|
|
77
|
|
|
// prepare and insert the attribute option value |
78
|
|
|
$this->persistAttributeOptionValue($this->initializeAttribute($this->prepareAttributes())); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
83
|
|
|
* |
84
|
|
|
* @return array The prepared attributes |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
protected function prepareAttributes() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
|
89
|
|
|
// load the option ID |
90
|
|
|
$optionId = $this->getLastOptionId(); |
91
|
|
|
|
92
|
|
|
// load the store ID |
93
|
|
|
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
|
|
|
|
94
|
|
|
|
95
|
|
|
// load the attribute option value |
96
|
|
|
$value = $this->getValue(ColumnKeys::VALUE); |
97
|
|
|
|
98
|
|
|
// return the prepared attribute option |
99
|
|
|
return $this->initializeEntity( |
100
|
|
|
array( |
101
|
|
|
MemberNames::OPTION_ID => $optionId, |
102
|
|
|
MemberNames::STORE_ID => $storeId, |
103
|
|
|
MemberNames::VALUE => $value |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Initialize the attribute with the passed attributes and returns an instance. |
110
|
|
|
* |
111
|
|
|
* @param array $attr The attribute attributes |
112
|
|
|
* |
113
|
|
|
* @return array The initialized attribute |
114
|
|
|
*/ |
115
|
|
|
protected function initializeAttribute(array $attr) |
116
|
|
|
{ |
117
|
|
|
return $attr; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Return's the attribute bunch processor instance. |
122
|
|
|
* |
123
|
|
|
* @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
124
|
|
|
*/ |
125
|
|
|
protected function getAttributeBunchProcessor() |
126
|
|
|
{ |
127
|
|
|
return $this->attributeBunchProcessor; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return's the ID of the option that has been created recently. |
132
|
|
|
* |
133
|
|
|
* @return integer The option ID |
134
|
|
|
*/ |
135
|
|
|
protected function getLastOptionId() |
136
|
|
|
{ |
137
|
|
|
return $this->getSubject()->getLastOptionId(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Persist the passed attribute option value. |
142
|
|
|
* |
143
|
|
|
* @param array $attributeOptionValue The attribute option value to persist |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
protected function persistAttributeOptionValue(array $attributeOptionValue) |
148
|
|
|
{ |
149
|
|
|
return $this->getAttributeBunchProcessor()->persistAttributeOptionValue($attributeOptionValue); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.