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