1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\TierPrice\Observers\TierPriceUpdateObserver |
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 Klaas-Tido Rühl <[email protected]> |
15
|
|
|
* @author Tim Wagner <[email protected]> |
16
|
|
|
* @copyright 2019 REFUSiON GmbH <[email protected]> |
17
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
18
|
|
|
* @link https://github.com/techdivision/import-product-tier-price |
19
|
|
|
* @link https://www.techdivision.com |
20
|
|
|
* @link https://www.refusion.com |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace TechDivision\Import\Product\TierPrice\Observers; |
24
|
|
|
|
25
|
|
|
use TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface; |
26
|
|
|
use TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Observer for creating/updating/deleting tier prices from the database. |
30
|
|
|
* |
31
|
|
|
* @author Klaas-Tido Rühl <[email protected]> |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2019 REFUSiON 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-product-tier-price |
36
|
|
|
* @link https://www.techdivision.com |
37
|
|
|
* @link https://www.refusion.com |
38
|
|
|
*/ |
39
|
|
|
class TierPriceObserver extends AbstractProductTierPriceObserver |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The trait that prepares the tier price data. |
44
|
|
|
* |
45
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Observers\PrepareTierPriceTrait |
46
|
|
|
*/ |
47
|
|
|
use PrepareTierPriceTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The available tier price value types. |
51
|
|
|
* |
52
|
|
|
* @var \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface |
53
|
|
|
*/ |
54
|
|
|
protected $valueTypes; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize the observer with the passed product tier price processor instance. |
58
|
|
|
* |
59
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Services\TierPriceProcessorInterface $tierPriceProcessor The processor instance |
60
|
|
|
* @param \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface $valueTypes The tier price value types |
61
|
|
|
*/ |
62
|
|
|
public function __construct(TierPriceProcessorInterface $tierPriceProcessor, ValueTypesInterface $valueTypes) |
63
|
|
|
{ |
64
|
|
|
|
65
|
|
|
// pass the tier price processor through to the parent instance |
66
|
|
|
parent::__construct($tierPriceProcessor); |
67
|
|
|
|
68
|
|
|
// set the value types |
69
|
|
|
$this->valueTypes = $valueTypes; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the tier price value types. |
74
|
|
|
* |
75
|
|
|
* @return \TechDivision\Import\Product\TierPrice\Utils\ValueTypesInterface The tier price value types |
76
|
|
|
*/ |
77
|
|
|
protected function getValueTypes() |
78
|
|
|
{ |
79
|
|
|
return $this->valueTypes; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Process the observer's business logic. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
protected function process() |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
// intialize the tier price data |
92
|
|
|
$tierPrice = $this->initializeTierPrice($this->prepareAttributes()); |
93
|
|
|
|
94
|
|
|
// persist the tier price and mark it as processed |
95
|
|
|
$this->addProcessedTierPrice( |
96
|
|
|
$this->persistTierPrice($tierPrice), |
|
|
|
|
97
|
|
|
$tierPrice[$this->getPrimaryKeyMemberName()] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/**s |
102
|
|
|
* Initialize the product website with the passed attributes and returns an instance. |
103
|
|
|
* |
104
|
|
|
* @param array $attr The product website attributes |
105
|
|
|
* |
106
|
|
|
* @return array The initialized product website |
107
|
|
|
* @throws \RuntimeException Is thrown, if the attributes can not be initialized |
108
|
|
|
*/ |
109
|
|
|
protected function initializeTierPrice(array $attr) |
110
|
|
|
{ |
111
|
|
|
return $attr; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Loads and returns the product with the passed SKU. |
116
|
|
|
* |
117
|
|
|
* @param string $sku The SKU of the product to load |
118
|
|
|
* |
119
|
|
|
* @return array The product |
120
|
|
|
*/ |
121
|
|
|
protected function loadProduct($sku) |
122
|
|
|
{ |
123
|
|
|
return $this->getTierPriceProcessor()->loadProduct($sku); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Persists the tier price with the passed data. |
128
|
|
|
* |
129
|
|
|
* @param array $row The tier price to persist |
130
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
131
|
|
|
* |
132
|
|
|
* @return string The ID of the persisted entity |
133
|
|
|
*/ |
134
|
|
|
protected function persistTierPrice(array $row, $name = null) |
135
|
|
|
{ |
136
|
|
|
return $this->getTierPriceProcessor()->persistTierPrice($row, $name); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add the ID of the processed tier price. |
141
|
|
|
* |
142
|
|
|
* @param integer $valueId The ID of the processed tier price |
143
|
|
|
* @param integer $entityId The entity ID of the related product |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
protected function addProcessedTierPrice($valueId, $entityId) |
148
|
|
|
{ |
149
|
|
|
$this->getSubject()->addProcessedTierPrice($valueId, $entityId); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the customer group ID for the given code, if it exists. |
154
|
|
|
* |
155
|
|
|
* @param string $code The code of the requested customer group |
156
|
|
|
* |
157
|
|
|
* @return integer|null The ID of the customer group |
158
|
|
|
*/ |
159
|
|
|
protected function getCustomerGroupIdByCode($code) |
160
|
|
|
{ |
161
|
|
|
return $this->getSubject()->getCustomerGroupIdByCode($code); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return's the store website for the passed code. |
166
|
|
|
* |
167
|
|
|
* @param string $code The code of the store website to return the ID for |
168
|
|
|
* |
169
|
|
|
* @return integer The store website ID |
170
|
|
|
* @throws \Exception Is thrown, if the store website with the requested code is not available |
171
|
|
|
*/ |
172
|
|
|
protected function getStoreWebsiteIdByCode($code) |
173
|
|
|
{ |
174
|
|
|
return $this->getSubject()->getStoreWebsiteIdByCode($code); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Query whether or not the passed value type is valid. |
179
|
|
|
* |
180
|
|
|
* @param string $valueType The value type to query for |
181
|
|
|
* |
182
|
|
|
* @return boolean TRUE if the value type is valid, else FALSE |
183
|
|
|
*/ |
184
|
|
|
protected function isValueType($valueType) |
185
|
|
|
{ |
186
|
|
|
return $this->getValueTypes()->isValueType($valueType); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Queries whether or not the passed customer group code matches all groups or not. |
191
|
|
|
* |
192
|
|
|
* @param string $code The customer group code to query for |
193
|
|
|
* |
194
|
|
|
* @return boolean TRUE if the customer group code matches, else FALSE |
195
|
|
|
*/ |
196
|
|
|
protected function isAllGroups($code) |
197
|
|
|
{ |
198
|
|
|
return $this->getSubject()->isAllGroups($code); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|