Completed
Push — master ( 8112ea...73e535 )
by Aimeos
13:12
created

Base   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 217
rs 8.3673
c 0
b 0
f 0
wmc 45
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
C checkConfig() 0 77 33
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-2017
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 'number':
119
						if( is_numeric( $attributes[$key] ) === false ) {
120
							$errors[$key] = sprintf( 'Not a number' ); continue 2;
121
						}
122
						break;
123
					case 'date':
124
						$pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$/';
125
						if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) {
126
							$errors[$key] = sprintf( 'Not a date' ); continue 2;
127
						}
128
						break;
129
					case 'datetime':
130
						$pattern = '/^[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9](:[0-5][0-9])?$/';
131
						if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) {
132
							$errors[$key] = sprintf( 'Not a date and time' ); continue 2;
133
						}
134
						break;
135
					case 'time':
136
						$pattern = '/^([0-2])?[0-9]:[0-5][0-9](:[0-5][0-9])?$/';
137
						if( !is_string( $attributes[$key] ) || preg_match( $pattern, $attributes[$key] ) !== 1 ) {
138
							$errors[$key] = sprintf( 'Not a time' ); continue 2;
139
						}
140
						break;
141
					case 'list':
142
					case 'select':
143
						if( !is_array( $def['default'] ) || !isset( $def['default'][$attributes[$key]] )
144
							&& !in_array( $attributes[$key], $def['default'] )
145
						) {
146
							$errors[$key] = sprintf( 'Not a listed value' ); continue 2;
147
						}
148
						break;
149
					case 'map':
150
						if( !is_array( $attributes[$key] ) ) {
151
							$errors[$key] = sprintf( 'Not a key/value map' ); continue 2;
152
						}
153
						break;
154
					default:
155
						throw new \Aimeos\MShop\Service\Exception( sprintf( 'Invalid type "%1$s"', $def['type'] ) );
156
				}
157
			}
158
159
			$errors[$key] = null;
160
		}
161
162
		return $errors;
163
	}
164
165
166
	/**
167
	 * Returns the criteria attribute items for the backend configuration
168
	 *
169
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of criteria attribute items
170
	 */
171
	protected function getConfigItems( array $configList )
172
	{
173
		$list = [];
174
175
		foreach( $configList as $key => $config ) {
176
			$list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $config );
177
		}
178
179
		return $list;
180
	}
181
182
183
	/**
184
	 * Returns the first object of the decorator stack
185
	 *
186
	 * @return \Aimeos\MShop\Plugin\Provider\Iface First object of the decorator stack
187
	 */
188
	protected function getObject()
189
	{
190
		if( $this->object !== null ) {
191
			return $this->object;
192
		}
193
194
		return $this;
195
	}
196
197
198
	/**
199
	 * Returns the plugin item the provider is configured with.
200
	 *
201
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item object
202
	 */
203
	protected function getItemBase()
204
	{
205
		return $this->item;
206
	}
207
208
209
	/**
210
	 * Returns the configuration value from the service item specified by its key.
211
	 *
212
	 * @param string $key Configuration key
213
	 * @param mixed $default Default value if configuration key isn't available
214
	 * @return string|null Value from service item configuration
215
	 */
216
	protected function getConfigValue( $key, $default = null )
217
	{
218
		$config = $this->item->getConfig();
219
220
		if( isset( $config[$key] ) ) {
221
			return $config[$key];
222
		}
223
224
		return $default;
225
	}
226
227
228
	/**
229
	 * Returns the context object.
230
	 *
231
	 * @return \Aimeos\MShop\Context\Item\Iface Context item object
232
	 */
233
	protected function getContext()
234
	{
235
		return $this->context;
236
	}
237
}