Completed
Push — master ( fbc717...7baaae )
by Aimeos
09:41
created

Base   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 219
rs 8.439
c 0
b 0
f 0
wmc 47
lcom 2
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkConfigBE() 0 4 1
A getConfigBE() 0 4 1
A setObject() 0 5 1
D checkConfig() 0 79 35
A getConfigItems() 0 10 2
A getObject() 0 8 2
A getItemBase() 0 4 1
A getConfigValue() 0 10 2
A getContext() 0 4 1

How to fix   Complexity   

Complex Class

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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2016
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
{
23
	private $item;
24
	private $context;
25
	private $object;
26
27
28
	/**
29
	 * Initializes the plugin instance.
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object with required objects
32
	 * @param \Aimeos\MShop\Plugin\Item\Iface $item Plugin item object
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Plugin\Item\Iface $item )
35
	{
36
		$this->item = $item;
37
		$this->context = $context;
38
	}
39
40
41
	/**
42
	 * Checks the backend configuration attributes for validity.
43
	 *
44
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
45
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
46
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
47
	 */
48
	public function checkConfigBE( array $attributes )
49
	{
50
		return [];
51
	}
52
53
54
	/**
55
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
56
	 * rules for the value of each field in the administration interface.
57
	 *
58
	 * @return array List of attribute definitions implementing \Aimeos\MW\Common\Critera\Attribute\Iface
59
	 */
60
	public function getConfigBE()
61
	{
62
		return [];
63
	}
64
65
66
	/**
67
	 * Injects the outer object into the decorator stack
68
	 *
69
	 * @param \Aimeos\MShop\Plugin\Provider\Iface $object First object of the decorator stack
70
	 * @return \Aimeos\MShop\Plugin\Provider\Iface Plugin object for chaining method calls
71
	 */
72
	public function setObject( \Aimeos\MShop\Plugin\Provider\Iface $object )
73
	{
74
		$this->object = $object;
75
		return $this;
76
	}
77
78
79
	/**
80
	 * Checks required fields and the types of the config array.
81
	 *
82
	 * @param array $config Config parameters
83
	 * @param array $attributes Attributes for the config array
84
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
85
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
86
	 */
87
	protected function checkConfig( array $config, array $attributes )
88
	{
89
		$errors = [];
90
91
		foreach( $config as $key => $def )
92
		{
93
			if( $def['required'] === true && ( !isset( $attributes[$key] ) || $attributes[$key] === '' ) )
94
			{
95
				$errors[$key] = sprintf( 'Configuration for "%1$s" is missing', $key );
96
				continue;
97
			}
98
99
			if( isset( $attributes[$key] ) && $attributes[$key] != '' )
100
			{
101
				switch( $def['type'] )
102
				{
103
					case 'boolean':
104
						if( !is_string( $attributes[$key] ) || $attributes[$key] != '0' && $attributes[$key] != '1' ) {
105
							$errors[$key] = sprintf( 'Not a true/false value' ); continue 2;
106
						}
107
						break;
108
					case 'string':
109
						if( is_string( $attributes[$key] ) === false ) {
110
							$errors[$key] = sprintf( 'Not a string' ); continue 2;
111
						}
112
						break;
113
					case 'integer':
114
						if( ctype_digit( $attributes[$key] ) === false ) {
115
							$errors[$key] = sprintf( 'Not an integer number' ); continue 2;
116
						}
117
						break;
118
					case 'decimal': // deprecated
119
					case 'float': // deprecated
120
					case 'number':
121
						if( is_numeric( $attributes[$key] ) === false ) {
122
							$errors[$key] = sprintf( 'Not a number' ); continue 2;
123
						}
124
						break;
125
					case 'date':
126
						$pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/';
127
						if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) {
128
							$errors[$key] = sprintf( 'Not a date' ); continue 2;
129
						}
130
						break;
131
					case 'datetime':
132
						$pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/';
133
						if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) {
134
							$errors[$key] = sprintf( 'Not a date and time' ); continue 2;
135
						}
136
						break;
137
					case 'time':
138
						$pattern = '/^([0-2])?[0-9]:[0-5][0-9](:[0-5][0-9])?$/';
139
						if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) {
140
							$errors[$key] = sprintf( 'Not a time' ); continue 2;
141
						}
142
						break;
143
					case 'list':
144
					case 'select':
145
						if( !is_array( $def['default'] ) || !isset( $def['default'][$attributes[$key]] )
146
							&& !in_array( $attributes[$key], $def['default'] )
147
						) {
148
							$errors[$key] = sprintf( 'Not a listed value' ); continue 2;
149
						}
150
						break;
151
					case 'map':
152
						if( !is_array( $attributes[$key] ) ) {
153
							$errors[$key] = sprintf( 'Not a key/value map' ); continue 2;
154
						}
155
						break;
156
					default:
157
						throw new \Aimeos\MShop\Service\Exception( sprintf( 'Invalid type "%1$s"', $def['type'] ) );
158
				}
159
			}
160
161
			$errors[$key] = null;
162
		}
163
164
		return $errors;
165
	}
166
167
168
	/**
169
	 * Returns the criteria attribute items for the backend configuration
170
	 *
171
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of criteria attribute items
172
	 */
173
	protected function getConfigItems( array $configList )
174
	{
175
		$list = [];
176
177
		foreach( $configList as $key => $config ) {
178
			$list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $config );
179
		}
180
181
		return $list;
182
	}
183
184
185
	/**
186
	 * Returns the first object of the decorator stack
187
	 *
188
	 * @return \Aimeos\MShop\Plugin\Provider\Iface First object of the decorator stack
189
	 */
190
	protected function getObject()
191
	{
192
		if( $this->object !== null ) {
193
			return $this->object;
194
		}
195
196
		return $this;
197
	}
198
199
200
	/**
201
	 * Returns the plugin item the provider is configured with.
202
	 *
203
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item object
204
	 */
205
	protected function getItemBase()
206
	{
207
		return $this->item;
208
	}
209
210
211
	/**
212
	 * Returns the configuration value from the service item specified by its key.
213
	 *
214
	 * @param string $key Configuration key
215
	 * @param mixed $default Default value if configuration key isn't available
216
	 * @return string|null Value from service item configuration
217
	 */
218
	protected function getConfigValue( $key, $default = null )
219
	{
220
		$config = $this->item->getConfig();
221
222
		if( isset( $config[$key] ) ) {
223
			return $config[$key];
224
		}
225
226
		return $default;
227
	}
228
229
230
	/**
231
	 * Returns the context object.
232
	 *
233
	 * @return \Aimeos\MShop\Context\Item\Iface Context item object
234
	 */
235
	protected function getContext()
236
	{
237
		return $this->context;
238
	}
239
}