Passed
Push — master ( c858ef...566d79 )
by Aimeos
05:46
created

Required::calcPrice()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 18
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2020
7
 * @package MShop
8
 * @subpackage Coupon
9
 */
10
11
12
namespace Aimeos\MShop\Coupon\Provider\Decorator;
13
14
15
/**
16
 * Required decorator for coupon provider.
17
 *
18
 * @package MShop
19
 * @subpackage Coupon
20
 */
21
class Required
22
	extends \Aimeos\MShop\Coupon\Provider\Decorator\Base
23
	implements \Aimeos\MShop\Coupon\Provider\Decorator\Iface
24
{
25
	private $beConfig = array(
26
		'required.productcode' => array(
27
			'code' => 'required.productcode',
28
			'internalcode' => 'required.productcode',
29
			'label' => 'Code of the product that must be in the basket',
30
			'type' => 'string',
31
			'internaltype' => 'string',
32
			'default' => '',
33
			'required' => true,
34
		),
35
		'required.only' => array(
36
			'code' => 'required.only',
37
			'internalcode' => 'required.only',
38
			'label' => 'Rebate is applied only to products',
39
			'type' => 'boolean',
40
			'internaltype' => 'boolean',
41
			'default' => false,
42
			'required' => false,
43
		),
44
	);
45
46
47
	/**
48
	 * Returns the price the discount should be applied to
49
	 *
50
	 * The result depends on the configured restrictions and it must be less or
51
	 * equal to the passed price.
52
	 *
53
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $base Basic order of the customer
54
	 * @return \Aimeos\MShop\Price\Item\Iface New price that should be used
55
	 */
56
	public function calcPrice( \Aimeos\MShop\Order\Item\Base\Iface $base ) : \Aimeos\MShop\Price\Item\Iface
57
	{
58
		if( $this->getConfigValue( 'required.only' ) == true )
59
		{
60
			$price = \Aimeos\MShop::create( $this->getContext(), 'price' )->createItem();
61
			$codes = explode( ',', $this->getConfigValue( 'required.productcode', '' ) );
62
63
			foreach( $base->getProducts() as $product )
64
			{
65
				if( in_array( $product->getProductCode(), $codes ) ) {
66
					$price = $price->addItem( $product->getPrice(), $product->getQuantity() );
67
				}
68
			}
69
70
			return $price;
71
		}
72
73
		return $this->getProvider()->calcPrice( $base );
74
	}
75
76
77
	/**
78
	 * Checks the backend configuration attributes for validity.
79
	 *
80
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
81
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
82
	 * 	known by the provider but aren't valid
83
	 */
84
	public function checkConfigBE( array $attributes ) : array
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
85
	{
86
		return $this->checkConfig( $this->beConfig, $attributes );
87
	}
88
89
90
	/**
91
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
92
	 * rules for the value of each field in the administration interface.
93
	 *
94
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
95
	 */
96
	public function getConfigBE() : array
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
97
	{
98
		return $this->getConfigItems( $this->beConfig );
99
	}
100
101
	/**
102
	 * Checks for requirements.
103
	 *
104
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $base Basic order of the customer
105
	 * @return bool True if the requirements are met, false if not
106
	 */
107
	public function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $base ) : bool
108
	{
109
		if( ( $prodcode = $this->getConfigValue( 'required.productcode' ) ) !== null )
110
		{
111
			foreach( $base->getProducts() as $product )
112
			{
113
				if( $product->getProductCode() == $prodcode ) {
114
					return parent::isAvailable( $base );
115
				}
116
			}
117
118
			return false;
119
		}
120
121
		return true;
122
	}
123
}
124