Passed
Push — master ( beeae9...b3aaac )
by Aimeos
04:26
created

Base::checkConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package MShop
8
 * @subpackage Service
9
 */
10
11
12
namespace Aimeos\MShop\Service\Provider;
13
14
15
/**
16
 * Abstract class for all service provider implementations with some default methods.
17
 *
18
 * @package MShop
19
 * @subpackage Service
20
 */
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 )
39
	{
40
		$this->context = $context;
41
		$this->serviceItem = $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,
234
		\Aimeos\MShop\Order\Item\Iface $order ) : \Aimeos\MShop\Order\Item\Iface
235
	{
236
		return $order;
237
	}
238
239
240
	/**
241
	 * Creates service attributes from the passed data
242
	 *
243
	 * @param array $map Attribute key/value pairs entered by the customer during the checkout process
244
	 * @param string $type Type of the configuration values (delivery or payment)
245
	 * @return array List of \Aimeos\MShop\Order\Item\Base\Service\Attribute\Iface objects
246
	 */
247
	protected function attributes( array $map, string $type ) : array
248
	{
249
		$list = [];
250
		$manager = \Aimeos\MShop::create( $this->context, 'order/base/service' );
251
252
		foreach( $map as $key => $value ) {
253
			$list[] = $manager->createAttributeItem()->setType( $type )->setCode( $key )->setValue( $value );
254
		}
255
256
		return $list;
257
	}
258
259
260
	/**
261
	 * Checks required fields and the types of the given data map
262
	 *
263
	 * @param array $criteria Multi-dimensional associative list of criteria configuration
264
	 * @param array $map Values to check agains the criteria
265
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
266
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
267
	 */
268
	protected function checkConfig( array $criteria, array $map ) : array
269
	{
270
		$helper = new \Aimeos\MShop\Common\Helper\Config\Standard( $this->getConfigItems( $criteria ) );
271
		return $helper->check( $map );
272
	}
273
274
275
	/**
276
	 * Returns the context item.
277
	 *
278
	 * @return \Aimeos\MShop\ContextIface Context item
279
	 */
280
	protected function context() : \Aimeos\MShop\ContextIface
281
	{
282
		return $this->context;
283
	}
284
285
286
	/**
287
	 * Returns the service related data from the customer account if available
288
	 *
289
	 * @param string $customerId Unique customer ID the service token belongs to
290
	 * @param string $key Key of the value that should be returned
291
	 * @return array|string|null Service data or null if none is available
292
	 */
293
	protected function data( string $customerId, string $key )
294
	{
295
		if( $customerId )
296
		{
297
			$item = \Aimeos\MShop::create( $this->context, 'customer' )->get( $customerId, ['service'] );
298
299
			if( $listItem = $item->getListItem( 'service', 'default', $this->getServiceItem()->getId() ) ) {
300
				return $listItem->getConfigValue( $key );
301
			}
302
		}
303
304
		return null;
305
	}
306
307
308
	/**
309
	 * Returns the calculated amount of the price item
310
	 *
311
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price item
312
	 * @param bool $costs Include costs per item
313
	 * @param bool $tax Include tax
314
	 * @param int $precision Number for decimal digits
315
	 * @return string Formatted money amount
316
	 */
317
	protected function getAmount( \Aimeos\MShop\Price\Item\Iface $price, bool $costs = true, bool $tax = true,
318
		int $precision = null ) : string
319
	{
320
		$amount = $price->getValue();
321
322
		if( $costs === true ) {
323
			$amount += $price->getCosts();
324
		}
325
326
		if( $tax === true && $price->getTaxFlag() === false )
327
		{
328
			$tmp = clone $price;
329
330
			if( $costs === false )
331
			{
332
				$tmp->clear();
333
				$tmp->setValue( $price->getValue() );
334
				$tmp->setTaxRate( $price->getTaxRate() );
335
				$tmp->setQuantity( $price->getQuantity() );
336
			}
337
338
			$amount += $tmp->getTaxValue();
339
		}
340
341
		return number_format( $amount, $precision !== null ? $precision : $price->getPrecision(), '.', '' );
342
	}
343
344
345
	/**
346
	 * Returns the order service matching the given code from the basket
347
	 *
348
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
349
	 * @param string $type Service type constant from \Aimeos\MShop\Order\Item\Service\Base
350
	 * @param string $code Code of the service item that should be returned
351
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Iface Order service item
352
	 * @throws \Aimeos\MShop\Order\Exception If no service for the given type and code is found
353
	 */
354
	protected function getBasketService( \Aimeos\MShop\Order\Item\Base\Iface $basket, string $type,
355
		string $code ) : \Aimeos\MShop\Order\Item\Base\Service\Iface
356
	{
357
		$msg = $this->context->translate( 'mshop', 'Service not available' );
358
359
		return map( $basket->getService( $type ) )->find( function( $service ) use ( $code ) {
360
				return $service->getCode() === $code;
361
		}, new \Aimeos\MShop\Service\Exception( $msg ) );
362
	}
363
364
365
	/**
366
	 * Returns the criteria attribute items for the backend configuration
367
	 *
368
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of criteria attribute items
369
	 */
370
	protected function getConfigItems( array $configList ) : array
371
	{
372
		$list = [];
373
374
		foreach( $configList as $key => $config ) {
375
			$list[$key] = new \Aimeos\Base\Criteria\Attribute\Standard( $config );
376
		}
377
378
		return $list;
379
	}
380
381
382
	/**
383
	 * Returns the configuration value that matches one of the given keys.
384
	 *
385
	 * The config of the service item and (optionally) the global config
386
	 * is tested in the order of the keys. The first one that matches will
387
	 * be returned.
388
	 *
389
	 * @param array|string $keys Key name or list of key names that should be tested for in the order to test
390
	 * @param mixed $default Returned value if the key wasn't was found
391
	 * @return mixed Value of the first key that matches or null if none was found
392
	 */
393
	protected function getConfigValue( $keys, $default = null )
394
	{
395
		foreach( (array) $keys as $key )
396
		{
397
			if( ( $value = $this->getServiceItem()->getConfigValue( $key ) ) !== null ) {
398
				return $value;
399
			}
400
401
			if( isset( $this->beGlobalConfig[$key] ) ) {
402
				return $this->beGlobalConfig[$key];
403
			}
404
		}
405
406
		return $default;
407
	}
408
409
410
	/**
411
	 * Logs the given message with the passed log level
412
	 *
413
	 * @param mixed $msg Message or object
414
	 * @param int $level Log level (default: ERR)
415
	 * @return self Same object for fluid method calls
416
	 */
417
	protected function log( $msg, int $level = \Aimeos\Base\Logger\Iface::ERR ) : self
418
	{
419
		$facility = basename( str_replace( '\\', '/', get_class( $this ) ) );
420
		$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 );
421
		$trace = array_pop( $trace ) ?: [];
422
		$name = ( $trace['class'] ?? '' ) . '::' . ( $trace['function'] ?? '' );
423
424
		if( !is_scalar( $msg ) ) {
425
			$msg = print_r( $msg, true );
426
		}
427
428
		$this->context->logger()->log( $name . ': ' . $msg, $level, 'core/service/' . $facility );
429
		return $this;
430
	}
431
432
433
	/**
434
	 * Returns the first object of the decorator stack
435
	 *
436
	 * @return \Aimeos\MShop\Service\Provider\Iface First object of the decorator stack
437
	 */
438
	protected function object() : \Aimeos\MShop\Service\Provider\Iface
439
	{
440
		return $this->object ?? $this;
441
	}
442
443
444
	/**
445
	 * Returns the configuration value that matches given key
446
	 *
447
	 * @param string $key Key name
448
	 * @return mixed Value for the key
449
	 * @throws \Aimeos\MShop\Service\Exception If configuration key isn't found
450
	 */
451
	protected function require( string $key )
452
	{
453
		if( ( $value = $this->getConfigValue( $key ) ) === null )
454
		{
455
			$msg = $this->context()->translate( 'mshop', 'Missing configuration "%1$s"' );
456
			throw new \Aimeos\MShop\Service\Exception( sprintf( $msg, $key ) );
457
		}
458
459
		return $value;
460
	}
461
462
463
	/**
464
	 * Saves the order item.
465
	 *
466
	 * @param \Aimeos\MShop\Order\Item\Iface $item Order object
467
	 * @return \Aimeos\MShop\Order\Item\Iface Order object including the generated ID
468
	 */
469
	protected function save( \Aimeos\MShop\Order\Item\Iface $item ) : \Aimeos\MShop\Order\Item\Iface
470
	{
471
		return \Aimeos\MShop::create( $this->context, 'order' )->save( $item );
472
	}
473
474
475
	/**
476
	 * Adds the service data to the customer account if available
477
	 *
478
	 * @param string $customerId Unique customer ID the service token belongs to
479
	 * @param string $key Key of the value that should be added
480
	 * @param string|array $data Service data to store
481
	 * @param \Aimeos\MShop\Service\Provider\Iface Provider object for chaining method calls
0 ignored issues
show
Bug introduced by
The type Aimeos\MShop\Service\Provider\Provider was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
482
	 */
483
	protected function setData( string $customerId, string $key, $data ) : \Aimeos\MShop\Service\Provider\Iface
484
	{
485
		if( $customerId )
486
		{
487
			$manager = \Aimeos\MShop::create( $this->context, 'customer' );
488
			$item = $manager->get( $customerId, ['service'] );
489
			$serviceId = $this->getServiceItem()->getId();
490
491
			if( ( $listItem = $item->getListItem( 'service', 'default', $serviceId, false ) ) === null )
492
			{
493
				$listManager = \Aimeos\MShop::create( $this->context, 'customer/lists' );
494
				$listItem = $listManager->create()->setRefId( $serviceId );
495
			}
496
497
			$manager->save( $item->addListItem( 'service', $listItem->setConfigValue( $key, $data ) ) );
498
		}
499
500
		return $this;
501
	}
502
503
504
	/**
505
	 * Throws an exception with the given message
506
	 *
507
	 * @param string $msg Message
508
	 * @param string|null $domain Translation domain
509
	 * @param int $code Custom error code
510
	 */
511
	protected function throw( string $msg, string $domain = null, int $code = 0 )
512
	{
513
		if( $domain ) {
514
			$msg = $this->context->translate( $domain, $msg );
515
		}
516
517
		throw new \Aimeos\MShop\Service\Exception( $msg, $code );
518
	}
519
}
520