Total Complexity | 48 |
Total Lines | 507 |
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 |
||
21 | abstract class Base |
||
22 | implements Iface, \Aimeos\Macro\Iface |
||
23 | { |
||
24 | use \Aimeos\Macro\Macroable; |
||
25 | |||
26 | private $object; |
||
27 | private $context; |
||
28 | private $serviceItem; |
||
29 | private $beGlobalConfig; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Initializes the service provider object. |
||
34 | * |
||
35 | * @param \Aimeos\MShop\ContextIface $context Context object with required objects |
||
36 | * @param \Aimeos\MShop\Service\Item\Iface $serviceItem Service item with configuration for the provider |
||
37 | */ |
||
38 | public function __construct( \Aimeos\MShop\ContextIface $context, \Aimeos\MShop\Service\Item\Iface $serviceItem ) |
||
42 | } |
||
43 | |||
44 | |||
45 | /** |
||
46 | * Returns the price when using the provider. |
||
47 | * Usually, this is the lowest price that is available in the service item but can also be a calculated based on |
||
48 | * the basket content, e.g. 2% of the value as transaction cost. |
||
49 | * |
||
50 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
51 | * @param array $options Selected options by customer from frontend |
||
52 | * @return \Aimeos\MShop\Price\Item\Iface Price item containing the price, shipping, rebate |
||
53 | */ |
||
54 | public function calcPrice( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $options = [] ) : \Aimeos\MShop\Price\Item\Iface |
||
55 | { |
||
56 | $manager = \Aimeos\MShop::create( $this->context, 'price' ); |
||
57 | $prices = $this->serviceItem->getRefItems( 'price', 'default', 'default' ); |
||
58 | |||
59 | return $prices->isEmpty() ? $manager->create() : $manager->getLowestPrice( $prices, 1 ); |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Checks the backend configuration attributes for validity. |
||
65 | * |
||
66 | * @param array $attributes Attributes added by the shop owner in the administraton interface |
||
67 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
68 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
69 | */ |
||
70 | public function checkConfigBE( array $attributes ) : array |
||
71 | { |
||
72 | return []; |
||
73 | } |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Checks the frontend configuration attributes for validity. |
||
78 | * |
||
79 | * @param array $attributes Attributes entered by the customer during the checkout process |
||
80 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
81 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
82 | */ |
||
83 | public function checkConfigFE( array $attributes ) : array |
||
84 | { |
||
85 | return []; |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
91 | * rules for the value of each field in the administration interface. |
||
92 | * |
||
93 | * @return array List of attribute definitions implementing \Aimeos\Base\Critera\Attribute\Iface |
||
94 | */ |
||
95 | public function getConfigBE() : array |
||
96 | { |
||
97 | return []; |
||
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Returns the configuration attribute definitions of the provider to generate a list of available fields and |
||
103 | * rules for the value of each field in the frontend. |
||
104 | * |
||
105 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
106 | * @return array List of attribute definitions implementing \Aimeos\Base\Critera\Attribute\Iface |
||
107 | */ |
||
108 | public function getConfigFE( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : array |
||
109 | { |
||
110 | return []; |
||
111 | } |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Returns the service item which also includes the configuration for the service provider. |
||
116 | * |
||
117 | * @return \Aimeos\MShop\Service\Item\Iface Service item |
||
118 | */ |
||
119 | public function getServiceItem() : \Aimeos\MShop\Service\Item\Iface |
||
120 | { |
||
121 | return $this->serviceItem; |
||
122 | } |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Injects additional global configuration for the backend. |
||
127 | * |
||
128 | * It's used for adding additional backend configuration from the application |
||
129 | * like the URLs to redirect to. |
||
130 | * |
||
131 | * Supported redirect URLs are: |
||
132 | * - payment.url-success |
||
133 | * - payment.url-failure |
||
134 | * - payment.url-cancel |
||
135 | * - payment.url-update |
||
136 | * |
||
137 | * @param array $config Associative list of config keys and their value |
||
138 | * @return \Aimeos\MShop\Service\Provider\Iface Provider object for chaining method calls |
||
139 | */ |
||
140 | public function injectGlobalConfigBE( array $config ) : \Aimeos\MShop\Service\Provider\Iface |
||
141 | { |
||
142 | $this->beGlobalConfig = $config; |
||
143 | return $this; |
||
144 | } |
||
145 | |||
146 | |||
147 | /** |
||
148 | * Checks if payment provider can be used based on the basket content. |
||
149 | * Checks for country, currency, address, RMS, etc. -> in separate decorators |
||
150 | * |
||
151 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
152 | * @return bool True if payment provider can be used, false if not |
||
153 | */ |
||
154 | public function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : bool |
||
155 | { |
||
156 | return true; |
||
157 | } |
||
158 | |||
159 | |||
160 | /** |
||
161 | * Checks what features the payment provider implements. |
||
162 | * |
||
163 | * @param int $what Constant from abstract class |
||
164 | * @return bool True if feature is available in the payment provider, false if not |
||
165 | */ |
||
166 | public function isImplemented( int $what ) : bool |
||
167 | { |
||
168 | return false; |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Queries for status updates for the given order if supported. |
||
174 | * |
||
175 | * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice object |
||
176 | * @return \Aimeos\MShop\Order\Item\Iface Updated order item object |
||
177 | */ |
||
178 | public function query( \Aimeos\MShop\Order\Item\Iface $order ) : \Aimeos\MShop\Order\Item\Iface |
||
179 | { |
||
180 | $msg = $this->context->translate( 'mshop', 'Method "%1$s" for provider not available' ); |
||
181 | throw new \Aimeos\MShop\Service\Exception( sprintf( $msg, 'query' ) ); |
||
182 | } |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Injects the outer object into the decorator stack |
||
187 | * |
||
188 | * @param \Aimeos\MShop\Service\Provider\Iface $object First object of the decorator stack |
||
189 | * @return \Aimeos\MShop\Service\Provider\Iface Service object for chaining method calls |
||
190 | */ |
||
191 | public function setObject( \Aimeos\MShop\Service\Provider\Iface $object ) : \Aimeos\MShop\Service\Provider\Iface |
||
192 | { |
||
193 | $this->object = $object; |
||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Looks for new update files and updates the orders for which status updates were received. |
||
200 | * If batch processing of files isn't supported, this method can be empty. |
||
201 | * |
||
202 | * @return bool True if the update was successful, false if async updates are not supported |
||
203 | * @throws \Aimeos\MShop\Service\Exception If updating one of the orders failed |
||
204 | */ |
||
205 | public function updateAsync() : bool |
||
206 | { |
||
207 | return false; |
||
208 | } |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Updates the order status sent by payment gateway notifications |
||
213 | * |
||
214 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
215 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
216 | * @return \Psr\Http\Message\ResponseInterface Response object |
||
217 | */ |
||
218 | public function updatePush( \Psr\Http\Message\ServerRequestInterface $request, |
||
219 | \Psr\Http\Message\ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
220 | { |
||
221 | return $response->withStatus( 501, 'Not implemented' ); |
||
222 | } |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Updates the orders for whose status updates have been received by the confirmation page |
||
227 | * |
||
228 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object with parameters and request body |
||
229 | * @param \Aimeos\MShop\Order\Item\Iface $order Order item that should be updated |
||
230 | * @return \Aimeos\MShop\Order\Item\Iface Updated order item |
||
231 | * @throws \Aimeos\MShop\Service\Exception If updating the orders failed |
||
232 | */ |
||
233 | public function updateSync( \Psr\Http\Message\ServerRequestInterface $request, |
||
237 | } |
||
238 | |||
239 | |||
240 | /** |
||
241 | * Checks required fields and the types of the given data map |
||
242 | * |
||
243 | * @param array $criteria Multi-dimensional associative list of criteria configuration |
||
244 | * @param array $map Values to check agains the criteria |
||
245 | * @return array An array with the attribute keys as key and an error message as values for all attributes that are |
||
246 | * known by the provider but aren't valid resp. null for attributes whose values are OK |
||
247 | */ |
||
248 | protected function checkConfig( array $criteria, array $map ) : array |
||
249 | { |
||
250 | $helper = new \Aimeos\MShop\Common\Helper\Config\Standard( $this->getConfigItems( $criteria ) ); |
||
251 | return $helper->check( $map ); |
||
252 | } |
||
253 | |||
254 | |||
255 | /** |
||
256 | * Returns the criteria attribute items for the backend configuration |
||
257 | * |
||
258 | * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of criteria attribute items |
||
259 | */ |
||
260 | protected function getConfigItems( array $configList ) : array |
||
269 | } |
||
270 | |||
271 | |||
272 | /** |
||
273 | * Returns the configuration value that matches one of the given keys. |
||
274 | * |
||
275 | * The config of the service item and (optionally) the global config |
||
276 | * is tested in the order of the keys. The first one that matches will |
||
277 | * be returned. |
||
278 | * |
||
279 | * @param array|string $keys Key name or list of key names that should be tested for in the order to test |
||
280 | * @param mixed $default Returned value if the key wasn't was found |
||
281 | * @return mixed Value of the first key that matches or null if none was found |
||
282 | */ |
||
283 | protected function getConfigValue( $keys, $default = null ) |
||
284 | { |
||
285 | foreach( (array) $keys as $key ) |
||
286 | { |
||
287 | if( ( $value = $this->getServiceItem()->getConfigValue( $key ) ) !== null ) { |
||
288 | return $value; |
||
289 | } |
||
290 | |||
291 | if( isset( $this->beGlobalConfig[$key] ) ) { |
||
292 | return $this->beGlobalConfig[$key]; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | return $default; |
||
297 | } |
||
298 | |||
299 | |||
300 | /** |
||
301 | * Returns the context item. |
||
302 | * |
||
303 | * @return \Aimeos\MShop\ContextIface Context item |
||
304 | */ |
||
305 | protected function context() : \Aimeos\MShop\ContextIface |
||
306 | { |
||
307 | return $this->context; |
||
308 | } |
||
309 | |||
310 | |||
311 | /** |
||
312 | * Returns the calculated amount of the price item |
||
313 | * |
||
314 | * @param \Aimeos\MShop\Price\Item\Iface $price Price item |
||
315 | * @param bool $costs Include costs per item |
||
316 | * @param bool $tax Include tax |
||
317 | * @param int $precision Number for decimal digits |
||
318 | * @return string Formatted money amount |
||
319 | */ |
||
320 | protected function getAmount( \Aimeos\MShop\Price\Item\Iface $price, bool $costs = true, bool $tax = true, |
||
345 | } |
||
346 | |||
347 | |||
348 | /** |
||
349 | * Returns the order service matching the given code from the basket |
||
350 | * |
||
351 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
352 | * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base |
||
353 | * @param string $code Code of the service item that should be returned |
||
354 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order service item |
||
355 | * @throws \Aimeos\MShop\Order\Exception If no service for the given type and code is found |
||
356 | */ |
||
357 | protected function getBasketService( \Aimeos\MShop\Order\Item\Base\Iface $basket, string $type, |
||
365 | } |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Returns the first object of the decorator stack |
||
370 | * |
||
371 | * @return \Aimeos\MShop\Service\Provider\Iface First object of the decorator stack |
||
372 | */ |
||
373 | protected function object() : \Aimeos\MShop\Service\Provider\Iface |
||
376 | } |
||
377 | |||
378 | |||
379 | /** |
||
380 | * Logs the given message with the passed log level |
||
381 | * |
||
382 | * @param mixed $msg Message or object |
||
383 | * @param int $level Log level (default: ERR) |
||
384 | * @return self Same object for fluid method calls |
||
385 | */ |
||
386 | protected function log( $msg, int $level = \Aimeos\Base\Logger\Iface::ERR ) : self |
||
387 | { |
||
388 | $facility = basename( str_replace( '\\', '/', get_class( $this ) ) ); |
||
389 | $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 ); |
||
390 | $trace = array_pop( $trace ) ?: []; |
||
391 | $name = ( $trace['class'] ?? '' ) . '::' . ( $trace['function'] ?? '' ); |
||
392 | |||
393 | if( !is_scalar( $msg ) ) { |
||
394 | $msg = print_r( $msg, true ); |
||
395 | } |
||
396 | |||
397 | $this->context->logger()->log( $name . ': ' . $msg, $level, 'core/service/' . $facility ); |
||
398 | return $this; |
||
399 | } |
||
400 | |||
401 | |||
402 | /** |
||
403 | * Returns the configuration value that matches given key |
||
404 | * |
||
405 | * @param string $key Key name |
||
406 | * @return mixed Value for the key |
||
407 | * @throws \Aimeos\MShop\Service\Exception If configuration key isn't found |
||
408 | */ |
||
409 | protected function require( string $key ) |
||
410 | { |
||
411 | if( ( $value = $this->getConfigValue( $key ) ) === null ) |
||
412 | { |
||
413 | $msg = $this->context()->translate( 'mshop', 'Missing configuration "%1$s"' ); |
||
414 | throw new \Aimeos\MShop\Service\Exception( sprintf( $msg, $key ) ); |
||
415 | } |
||
416 | |||
417 | return $value; |
||
418 | } |
||
419 | |||
420 | |||
421 | /** |
||
422 | * Returns the service related data from the customer account if available |
||
423 | * |
||
424 | * @param string $customerId Unique customer ID the service token belongs to |
||
425 | * @param string $type Type of the value that should be returned |
||
426 | * @return array|string|null Service data or null if none is available |
||
427 | */ |
||
428 | protected function getCustomerData( string $customerId, string $type ) |
||
429 | { |
||
430 | if( $customerId != null ) |
||
431 | { |
||
432 | $manager = \Aimeos\MShop::create( $this->context, 'customer' ); |
||
433 | $item = $manager->get( $customerId, ['service'] ); |
||
434 | $serviceId = $this->getServiceItem()->getId(); |
||
435 | |||
436 | if( ( $listItem = $item->getListItem( 'service', 'default', $serviceId ) ) !== null ) { |
||
437 | return $listItem->getConfigValue( $type ); |
||
438 | } |
||
439 | } |
||
440 | |||
441 | return null; |
||
442 | } |
||
443 | |||
444 | |||
445 | /** |
||
446 | * Saves the order item. |
||
447 | * |
||
448 | * @param \Aimeos\MShop\Order\Item\Iface $item Order object |
||
449 | * @return \Aimeos\MShop\Order\Item\Iface Order object including the generated ID |
||
450 | */ |
||
451 | protected function save( \Aimeos\MShop\Order\Item\Iface $item ) : \Aimeos\MShop\Order\Item\Iface |
||
452 | { |
||
453 | return \Aimeos\MShop::create( $this->context, 'order' )->save( $item ); |
||
454 | } |
||
455 | |||
456 | |||
457 | /** |
||
458 | * Sets the attributes in the given service item. |
||
459 | * |
||
460 | * @param \Aimeos\MShop\Order\Item\Base\Service\Iface $orderServiceItem Order service item that will be added to the basket |
||
461 | * @param array $attributes Attribute key/value pairs entered by the customer during the checkout process |
||
462 | * @param string $type Type of the configuration values (delivery or payment) |
||
463 | * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Modified order service item |
||
464 | */ |
||
465 | protected function setAttributes( \Aimeos\MShop\Order\Item\Base\Service\Iface $orderServiceItem, array $attributes, |
||
481 | } |
||
482 | |||
483 | |||
484 | /** |
||
485 | * Adds the service data to the customer account if available |
||
486 | * |
||
487 | * @param string $customerId Unique customer ID the service token belongs to |
||
488 | * @param string $type Type of the value that should be added |
||
489 | * @param string|array $data Service data to store |
||
490 | * @param \Aimeos\MShop\Service\Provider\Iface Provider object for chaining method calls |
||
|
|||
491 | */ |
||
492 | protected function setCustomerData( string $customerId, string $type, $data ) : \Aimeos\MShop\Service\Provider\Iface |
||
511 | } |
||
512 | |||
513 | |||
514 | /** |
||
515 | * Throws an exception with the given message |
||
516 | * |
||
517 | * @param string $msg Message |
||
518 | * @param string|null $domain Translation domain |
||
519 | * @param int $code Custom error code |
||
520 | */ |
||
521 | protected function throw( string $msg, string $domain = null, int $code = 0 ) |
||
528 | } |
||
529 | } |
||
530 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths