Total Complexity | 46 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | abstract class Base extends \Aimeos\MShop\Common\Item\Base implements Iface |
||
21 | { |
||
22 | /** |
||
23 | * Delivery service. |
||
24 | */ |
||
25 | const TYPE_DELIVERY = 'delivery'; |
||
26 | |||
27 | /** |
||
28 | * Payment service. |
||
29 | */ |
||
30 | const TYPE_PAYMENT = 'payment'; |
||
31 | |||
32 | |||
33 | private $attributes; |
||
34 | private $attributesMap; |
||
35 | private $transactions; |
||
36 | private $price; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Initializes the order base service item |
||
41 | * |
||
42 | * @param \Aimeos\MShop\Price\Item\Iface $price |
||
43 | * @param array $values Values to be set on initialisation |
||
44 | * @param \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface[] $attributes List of order service attribute items |
||
45 | * @param \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface[] $attributes List of order service transaction items |
||
46 | */ |
||
47 | public function __construct( \Aimeos\MShop\Price\Item\Iface $price, array $values = [], |
||
48 | array $attributes = [], array $transactions = [] ) |
||
49 | { |
||
50 | parent::__construct( 'order.base.service.', $values ); |
||
51 | |||
52 | map( $attributes )->implements( \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface::class, true ); |
||
53 | |||
54 | $this->transactions = $transactions; |
||
55 | $this->attributes = $attributes; |
||
56 | $this->price = $price; |
||
57 | } |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Clones internal objects of the order base product item. |
||
62 | */ |
||
63 | public function __clone() |
||
74 | } |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Adds new and replaces existing attribute items for the service. |
||
79 | * |
||
80 | * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface[] $attributes List of order service attribute items |
||
81 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
82 | */ |
||
83 | public function addAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
84 | { |
||
85 | ( $attributes = map( $attributes ) )->implements( \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface::class, true ); |
||
86 | |||
87 | foreach( $attributes as $attrItem ) { |
||
88 | $this->setAttributeItem( $attrItem ); |
||
89 | } |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Returns the item type |
||
97 | * |
||
98 | * @return string Item type, subtypes are separated by slashes |
||
99 | */ |
||
100 | public function getResourceType() : string |
||
101 | { |
||
102 | return 'order/base/service'; |
||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Returns the value or list of values of the attribute item for the ordered service with the given code. |
||
108 | * |
||
109 | * @param string $code Code of the service attribute item |
||
110 | * @param array|string $type Type or list of types of the service attribute items |
||
111 | * @return array|string|null Value or list of values of the attribute item for the ordered service and the given code |
||
112 | */ |
||
113 | public function getAttribute( string $code, $type = '' ) |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Returns the attribute item or list of attribute items for the ordered service with the given code. |
||
134 | * |
||
135 | * @param string $code Code of the service attribute item |
||
136 | * @param array|string $type Type of the service attribute item |
||
137 | * @return \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface|array|null |
||
138 | * Attribute item or list of items for the ordered service and the given code |
||
139 | */ |
||
140 | public function getAttributeItem( string $code, $type = '' ) |
||
141 | { |
||
142 | $list = []; |
||
143 | $map = $this->getAttributeMap(); |
||
144 | |||
145 | foreach( (array) $type as $key ) |
||
146 | { |
||
147 | if( isset( $map[$key][$code] ) ) |
||
148 | { |
||
149 | foreach( $map[$key][$code] as $item ) { |
||
150 | $list[] = $item; |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return count( $list ) > 1 ? $list : ( reset( $list ) ?: null ); |
||
156 | } |
||
157 | |||
158 | |||
159 | /** |
||
160 | * Adds or replaces the attribute item in the list of service attributes. |
||
161 | * |
||
162 | * @param \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface $item Service attribute item |
||
163 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
164 | */ |
||
165 | public function setAttributeItem( \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface $item ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
166 | { |
||
167 | $this->getAttributeMap(); |
||
168 | |||
169 | $type = $item->getType(); |
||
170 | $code = $item->getCode(); |
||
171 | $attrId = $item->getAttributeId(); |
||
172 | |||
173 | if( !isset( $this->attributesMap[$type][$code][$attrId] ) ) |
||
174 | { |
||
175 | $this->attributesMap[$type][$code][$attrId] = $item; |
||
176 | $this->attributes[] = $item; |
||
177 | } |
||
178 | |||
179 | $this->attributesMap[$type][$code][$attrId]->setValue( $item->getValue() ); |
||
180 | $this->setModified(); |
||
181 | |||
182 | return $this; |
||
183 | } |
||
184 | |||
185 | |||
186 | /** |
||
187 | * Returns the list of attribute items for the service. |
||
188 | * |
||
189 | * @param string|null $type Filters returned attributes by the given type or null for no filtering |
||
190 | * @return \Aimeos\Map List of attribute items implementing \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface |
||
191 | */ |
||
192 | public function getAttributeItems( string $type = null ) : \Aimeos\Map |
||
208 | } |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Sets the new list of attribute items for the service. |
||
213 | * |
||
214 | * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface[] $attributes List of order service attribute items |
||
215 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
216 | */ |
||
217 | public function setAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
218 | { |
||
219 | ( $attributes = map( $attributes ) )->implements( \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface::class, true ); |
||
220 | |||
221 | $this->attributes = $attributes->all(); |
||
222 | $this->attributesMap = null; |
||
223 | $this->setModified(); |
||
224 | |||
225 | return $this; |
||
226 | } |
||
227 | |||
228 | |||
229 | /** |
||
230 | * Returns the price item for the product. |
||
231 | * |
||
232 | * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate |
||
233 | */ |
||
234 | public function getPrice() : \Aimeos\MShop\Price\Item\Iface |
||
235 | { |
||
236 | return $this->price; |
||
237 | } |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Sets the price item for the product. |
||
242 | * |
||
243 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item containing price and additional costs |
||
244 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base product item for chaining method calls |
||
245 | */ |
||
246 | public function setPrice( \Aimeos\MShop\Price\Item\Iface $price ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
255 | } |
||
256 | |||
257 | |||
258 | /** |
||
259 | * Adds a new transaction to the service. |
||
260 | * |
||
261 | * @param \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface $item Transaction item |
||
262 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
263 | */ |
||
264 | public function addTransaction( \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface $item ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
265 | { |
||
266 | $this->transactions[] = $item; |
||
267 | $this->setModified(); |
||
268 | |||
269 | return $this; |
||
270 | } |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Returns the list of transactions items for the service. |
||
275 | * |
||
276 | * @param string|null $type Filters returned transactions by the given type or null for no filtering |
||
277 | * @return \Aimeos\Map List of transaction items implementing \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface |
||
278 | */ |
||
279 | public function getTransactions( string $type = null ) : \Aimeos\Map |
||
280 | { |
||
281 | return map( $this->transactions ); |
||
282 | } |
||
283 | |||
284 | |||
285 | /** |
||
286 | * Sets the new list of transactions items for the service. |
||
287 | * |
||
288 | * @param iterable $list List of order service transaction items |
||
289 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order base service item for chaining method calls |
||
290 | */ |
||
291 | public function setTransactions( iterable $list ) : \Aimeos\MShop\Order\Item\Base\Service\Iface |
||
297 | } |
||
298 | |||
299 | |||
300 | /* |
||
301 | * Sets the item values from the given array and removes that entries from the list |
||
302 | * |
||
303 | * @param array &$list Associative list of item keys and their values |
||
304 | * @param bool True to set private properties too, false for public only |
||
305 | * @return \Aimeos\MShop\Order\Item\Base\Product\Iface Order service item for chaining method calls |
||
306 | */ |
||
307 | public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface |
||
308 | { |
||
309 | $item = parent::fromArray( $list, $private ); |
||
310 | $price = $item->getPrice(); |
||
311 | |||
312 | foreach( $list as $key => $value ) |
||
313 | { |
||
314 | switch( $key ) |
||
315 | { |
||
316 | case 'order.base.service.price': $price = $price->setValue( $value ); break; |
||
317 | case 'order.base.service.costs': $price = $price->setCosts( $value ); break; |
||
318 | case 'order.base.service.rebate': $price = $price->setRebate( $value ); break; |
||
319 | case 'order.base.service.taxrate': $price = $price->setTaxRate( $value ); break; |
||
320 | case 'order.base.service.taxrates': $price = $price->setTaxRates( (array) $value ); break; |
||
321 | default: continue 2; |
||
322 | } |
||
323 | |||
324 | unset( $list[$key] ); |
||
325 | } |
||
326 | |||
327 | return $item->setPrice( $price ); |
||
328 | } |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Returns the item values as associative list. |
||
333 | * |
||
334 | * @param bool True to return private properties, false for public only |
||
335 | * @return array Associative list of item properties and their values |
||
336 | */ |
||
337 | public function toArray( bool $private = false ) : array |
||
348 | } |
||
349 | |||
350 | |||
351 | /** |
||
352 | * Checks if the given address type is valid |
||
353 | * |
||
354 | * @param string $value Address type defined in \Aimeos\MShop\Order\Item\Base\Address\Base |
||
355 | * @throws \Aimeos\MShop\Order\Exception If type is invalid |
||
356 | */ |
||
357 | protected function checkType( string $value ) |
||
366 | } |
||
367 | } |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Returns the attribute map for the service. |
||
372 | * |
||
373 | * @return array Associative list of type and code as key and an \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface as value |
||
374 | */ |
||
375 | protected function getAttributeMap() : array |
||
389 |