Completed
Push — master ( 24a86a...771b97 )
by Aimeos
10:31
created

Standard   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 317
rs 9.1199
c 0
b 0
f 0
wmc 41
lcom 1
cbo 1

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getType() 0 6 2
A getTypeName() 0 6 2
A getTypeId() 0 6 2
A setTypeId() 0 10 2
A getProvider() 0 8 2
A setProvider() 0 10 2
A getLabel() 0 8 2
A setLabel() 0 10 2
A getConfig() 0 8 2
A setConfig() 0 7 1
A getPosition() 0 8 2
A setPosition() 0 10 2
A getStatus() 0 8 2
A setStatus() 0 10 2
A getResourceType() 0 4 1
A isAvailable() 0 4 2
B fromArray() 0 22 8
A toArray() 0 18 2

How to fix   Complexity   

Complex Class

Complex classes like Standard 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 Standard, 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, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 * @package MShop
8
 * @subpackage Plugin
9
 */
10
11
12
namespace Aimeos\MShop\Plugin\Item;
13
14
15
/**
16
 * Default implementation of plugin items.
17
 *
18
 * @package MShop
19
 * @subpackage Plugin
20
 */
21
class Standard
22
	extends \Aimeos\MShop\Common\Item\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MShop\Plugin\Item\Iface
24
{
25
	private $values;
26
27
	/**
28
	 * Initializes the plugin object
29
	 *
30
	 * @param array $values Associative array of id, typeid, name, config and status
31
	 */
32
	public function __construct( array $values = [] )
33
	{
34
		parent::__construct( 'plugin.', $values );
35
36
		$this->values = $values;
37
	}
38
39
40
	/**
41
	 * Returns the type of the plugin.
42
	 *
43
	 * @return string|null Plugin type
44
	 */
45
	public function getType()
46
	{
47
		if( isset( $this->values['plugin.type'] ) ) {
48
			return (string) $this->values['plugin.type'];
49
		}
50
	}
51
52
53
	/**
54
	 * Returns the localized name of the type
55
	 *
56
	 * @return string|null Localized name of the type
57
	 */
58
	public function getTypeName()
59
	{
60
		if( isset( $this->values['plugin.typename'] ) ) {
61
			return (string) $this->values['plugin.typename'];
62
		}
63
	}
64
65
66
	/**
67
	 * Returns the type ID of the plugin.
68
	 *
69
	 * @return string|null Plugin type ID
70
	 */
71
	public function getTypeId()
72
	{
73
		if( isset( $this->values['plugin.typeid'] ) ) {
74
			return (string) $this->values['plugin.typeid'];
75
		}
76
	}
77
78
79
	/**
80
	 * Sets the new type ID of the plugin item.
81
	 *
82
	 * @param string $typeid New plugin type ID
83
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item for chaining method calls
84
	 */
85
	public function setTypeId( $typeid )
86
	{
87
		if( (string) $typeid !== $this->getTypeId() )
88
		{
89
			$this->values['plugin.typeid'] = (string) $typeid;
90
			$this->setModified();
91
		}
92
93
		return $this;
94
	}
95
96
97
	/**
98
	 * Returns the provider of the plugin.
99
	 *
100
	 * @return string Plugin provider which is the short plugin class name
101
	 */
102
	public function getProvider()
103
	{
104
		if( isset( $this->values['plugin.provider'] ) ) {
105
			return (string) $this->values['plugin.provider'];
106
		}
107
108
		return '';
109
	}
110
111
112
	/**
113
	 * Sets the new provider of the plugin item which is the short
114
	 * name of the plugin class name.
115
	 *
116
	 * @param string $provider Plugin provider, esp. short plugin class name
117
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item for chaining method calls
118
	 */
119
	public function setProvider( $provider )
120
	{
121
		if( (string) $provider !== $this->getProvider() )
122
		{
123
			$this->values['plugin.provider'] = (string) $provider;
124
			$this->setModified();
125
		}
126
127
		return $this;
128
	}
129
130
131
	/**
132
	 * Returns the name of the plugin item.
133
	 *
134
	 * @return string Label of the plugin item
135
	 */
136
	public function getLabel()
137
	{
138
		if( isset( $this->values['plugin.label'] ) ) {
139
			return (string) $this->values['plugin.label'];
140
		}
141
142
		return '';
143
	}
144
145
146
	/**
147
	 * Sets the new label of the plugin item.
148
	 *
149
	 * @param string $label New label of the plugin item
150
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item for chaining method calls
151
	 */
152
	public function setLabel( $label )
153
	{
154
		if( (string) $label !== $this->getLabel() )
155
		{
156
			$this->values['plugin.label'] = (string) $label;
157
			$this->setModified();
158
		}
159
160
		return $this;
161
	}
162
163
164
	/**
165
	 * Returns the configuration of the plugin item.
166
	 *
167
	 * @return array Custom configuration values
168
	 */
169
	public function getConfig()
170
	{
171
		if( isset( $this->values['plugin.config'] ) ) {
172
			return (array) $this->values['plugin.config'];
173
		}
174
175
		return [];
176
	}
177
178
179
	/**
180
	 * Sets the new configuration for the plugin item.
181
	 *
182
	 * @param array $config Custom configuration values
183
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item for chaining method calls
184
	 */
185
	public function setConfig( array $config )
186
	{
187
		$this->values['plugin.config'] = $config;
188
		$this->setModified();
189
190
		return $this;
191
	}
192
193
194
	/**
195
	 * Returns the position of the plugin item.
196
	 *
197
	 * @return integer Position of the item
198
	 */
199
	public function getPosition()
200
	{
201
		if( isset( $this->values['plugin.position'] ) ) {
202
			return (int) $this->values['plugin.position'];
203
		}
204
205
		return 0;
206
	}
207
208
209
	/**
210
	 * Sets the new position of the plugin item.
211
	 *
212
	 * @param integer $position Position of the item
213
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item for chaining method calls
214
	 */
215
	public function setPosition( $position )
216
	{
217
		if( (int) $position !== $this->getPosition() )
218
		{
219
			$this->values['plugin.position'] = (int) $position;
220
			$this->setModified();
221
		}
222
223
		return $this;
224
	}
225
226
227
	/**
228
	 * Returns the status of the plugin item.
229
	 *
230
	 * @return integer Status of the item
231
	 */
232
	public function getStatus()
233
	{
234
		if( isset( $this->values['plugin.status'] ) ) {
235
			return (int) $this->values['plugin.status'];
236
		}
237
238
		return 0;
239
	}
240
241
242
	/**
243
	 * Sets the new status of the plugin item.
244
	 *
245
	 * @param integer $status Status of the item
246
	 * @return \Aimeos\MShop\Plugin\Item\Iface Plugin item for chaining method calls
247
	 */
248
	public function setStatus( $status )
249
	{
250
		if( (int) $status !== $this->getStatus() )
251
		{
252
			$this->values['plugin.status'] = (int) $status;
253
			$this->setModified();
254
		}
255
256
		return $this;
257
	}
258
259
260
	/**
261
	 * Returns the item type
262
	 *
263
	 * @return string Item type, subtypes are separated by slashes
264
	 */
265
	public function getResourceType()
266
	{
267
		return 'plugin';
268
	}
269
270
271
	/**
272
	 * Tests if the item is available based on status, time, language and currency
273
	 *
274
	 * @return boolean True if available, false if not
275
	 */
276
	public function isAvailable()
277
	{
278
		return parent::isAvailable() && (bool) $this->getStatus();
279
	}
280
281
282
	/**
283
	 * Sets the item values from the given array.
284
	 *
285
	 * @param array $list Associative list of item keys and their values
286
	 * @return array Associative list of keys and their values that are unknown
287
	 */
288
	public function fromArray( array $list )
289
	{
290
		$unknown = [];
291
		$list = parent::fromArray( $list );
292
		unset( $list['plugin.type'], $list['plugin.typename'] );
293
294
		foreach( $list as $key => $value )
295
		{
296
			switch( $key )
297
			{
298
				case 'plugin.typeid': $this->setTypeId( $value ); break;
299
				case 'plugin.label': $this->setLabel( $value ); break;
300
				case 'plugin.provider': $this->setProvider( $value ); break;
301
				case 'plugin.config': $this->setConfig( $value ); break;
302
				case 'plugin.status': $this->setStatus( $value ); break;
303
				case 'plugin.position': $this->setPosition( $value ); break;
304
				default: $unknown[$key] = $value;
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
305
			}
306
		}
307
308
		return $unknown;
309
	}
310
311
312
	/**
313
	 * Returns the item values as array.
314
	 *
315
	 * @param boolean True to return private properties, false for public only
316
	 * @return array Associative list of item properties and their values
317
	 */
318
	public function toArray( $private = false )
319
	{
320
		$list = parent::toArray( $private );
321
322
		$list['plugin.type'] = $this->getType();
323
		$list['plugin.typename'] = $this->getTypeName();
324
		$list['plugin.label'] = $this->getLabel();
325
		$list['plugin.provider'] = $this->getProvider();
326
		$list['plugin.config'] = $this->getConfig();
327
		$list['plugin.status'] = $this->getStatus();
328
		$list['plugin.position'] = $this->getPosition();
329
330
		if( $private === true ) {
331
			$list['plugin.typeid'] = $this->getTypeId();
332
		}
333
334
		return $list;
335
	}
336
337
}
338