Passed
Push — master ( 0cda2e...f3fa1e )
by Aimeos
17:16 queued 07:34
created

Base::object()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021
6
 * @package MShop
7
 * @subpackage Rule
8
 */
9
10
11
namespace Aimeos\MShop\Rule\Provider;
12
13
14
/**
15
 * Abstract class for rule provider and decorator implementations
16
 *
17
 * @package MShop
18
 * @subpackage Rule
19
 */
20
abstract class Base
21
	implements \Aimeos\MW\Macro\Iface
22
{
23
	use \Aimeos\MW\Macro\Traits;
24
25
	private $item;
26
	private $context;
27
	private $object;
28
	private $beConfig = [
29
		'last-rule' => [
30
			'code' => 'last-rule',
31
			'internalcode' => 'last-rule',
32
			'label' => 'Don\'t execute subsequent rules',
33
			'type' => 'boolean',
34
			'internaltype' => 'boolean',
35
			'default' => 0,
36
			'required' => false,
37
		],
38
	];
39
40
41
	/**
42
	 * Initializes the rule instance.
43
	 *
44
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
45
	 * @param \Aimeos\MShop\Rule\Item\Iface $item Rule item object
46
	 */
47
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Rule\Item\Iface $item )
48
	{
49
		$this->item = $item;
50
		$this->context = $context;
51
	}
52
53
54
	/**
55
	 * Checks the backend configuration attributes for validity.
56
	 *
57
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
58
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
59
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
60
	 */
61
	public function checkConfigBE( array $attributes ) : array
62
	{
63
		return $this->checkConfig( $this->beConfig, $attributes );
64
	}
65
66
67
	/**
68
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
69
	 * rules for the value of each field in the administration interface.
70
	 *
71
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
72
	 */
73
	public function getConfigBE() : array
74
	{
75
		return $this->getConfigItems( $this->beConfig );
76
	}
77
78
79
	/**
80
	 * Injects the outer object into the decorator stack
81
	 *
82
	 * @param \Aimeos\MShop\Rule\Provider\Iface $object First object of the decorator stack
83
	 * @return \Aimeos\MShop\Rule\Provider\Iface Rule object for chaining method calls
84
	 */
85
	public function setObject( \Aimeos\MShop\Rule\Provider\Iface $object ) : \Aimeos\MShop\Rule\Provider\Iface
86
	{
87
		$this->object = $object;
88
		return $this;
89
	}
90
91
92
	/**
93
	 * Checks required fields and the types of the given data map
94
	 *
95
	 * @param array $criteria Multi-dimensional associative list of criteria configuration
96
	 * @param array $map Values to check agains the criteria
97
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
98
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
99
	 */
100
	protected function checkConfig( array $criteria, array $map ) : array
101
	{
102
		$helper = new \Aimeos\MShop\Common\Helper\Config\Standard( $this->getConfigItems( $criteria ) );
103
		return $helper->check( $map );
104
	}
105
106
107
	/**
108
	 * Returns the criteria attribute items for the backend configuration
109
	 *
110
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of criteria attribute items
111
	 */
112
	protected function getConfigItems( array $configList ) : array
113
	{
114
		$list = [];
115
116
		foreach( $configList as $key => $config ) {
117
			$list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $config );
118
		}
119
120
		return $list;
121
	}
122
123
124
	/**
125
	 * Returns the first object of the decorator stack
126
	 *
127
	 * @return \Aimeos\MShop\Rule\Provider\Iface First object of the decorator stack
128
	 */
129
	protected function object() : \Aimeos\MShop\Rule\Provider\Iface
130
	{
131
		if( $this->object !== null ) {
132
			return $this->object;
133
		}
134
135
		return $this;
136
	}
137
138
139
	/**
140
	 * Returns the rule item the provider is configured with.
141
	 *
142
	 * @return \Aimeos\MShop\Rule\Item\Iface Rule item object
143
	 */
144
	protected function getItemBase() : \Aimeos\MShop\Rule\Item\Iface
145
	{
146
		return $this->item;
147
	}
148
149
150
	/**
151
	 * Returns the configuration value from the service item specified by its key.
152
	 *
153
	 * @param string $key Configuration key
154
	 * @param mixed $default Default value if configuration key isn't available
155
	 * @return mixed Value from service item configuration
156
	 */
157
	protected function getConfigValue( string $key, $default = null )
158
	{
159
		$config = $this->item->getConfig();
160
161
		if( isset( $config[$key] ) ) {
162
			return $config[$key];
163
		}
164
165
		return $default;
166
	}
167
168
169
	/**
170
	 * Returns the context object.
171
	 *
172
	 * @return \Aimeos\MShop\Context\Item\Iface Context item object
173
	 */
174
	protected function getContext() : \Aimeos\MShop\Context\Item\Iface
175
	{
176
		return $this->context;
177
	}
178
179
180
	/**
181
	 * Tests if this rule should be the last one that is applied
182
	 *
183
	 * @return bool True, if rule should be the last one, false to continue with further rules
184
	 */
185
	protected function isLast() : bool
186
	{
187
		return (bool) $this->getConfigValue( 'last-rule', false );
188
	}
189
}
190