Passed
Push — master ( 26e563...322e4f )
by Aimeos
05:33
created

Standard::fromArray()   C

Complexity

Conditions 15
Paths 13

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 27
rs 5.9166
c 1
b 0
f 0
cc 15
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Item\Base\Service\Transaction;
12
13
14
/**
15
 * Default order item base service transaction.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
class Standard
21
	extends \Aimeos\MShop\Common\Item\Base
22
	implements \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface
23
{
24
	use \Aimeos\MShop\Common\Item\Config\Traits;
25
26
	private $price;
27
28
29
	/**
30
	 * Initializes the order item base service transaction item.
31
	 *
32
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price object
33
	 * @param array $values Associative array of key/value pairs
34
	 */
35
	public function __construct( \Aimeos\MShop\Price\Item\Iface $price, array $values = [] )
36
	{
37
		parent::__construct( 'order.base.service.transaction.', $values );
38
		$this->price = $price;
39
	}
40
41
42
	/**
43
	 * Returns the ID of the site the item is stored
44
	 *
45
	 * @return string Site ID (or null if not available)
46
	 */
47
	public function getSiteId() : string
48
	{
49
		return $this->get( 'order.base.service.transaction.siteid', '' );
50
	}
51
52
53
	/**
54
	 * Sets the site ID of the item.
55
	 *
56
	 * @param string $value Unique site ID of the item
57
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order base service transaction item for chaining method calls
58
	 */
59
	public function setSiteId( string $value ) : \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface
60
	{
61
		return $this->set( 'order.base.service.transaction.siteid', $value );
62
	}
63
64
65
	/**
66
	 * Returns the ID of the ordered service item as parent
67
	 *
68
	 * @return string|null ID of the ordered service item
69
	 */
70
	public function getParentId() : ?string
71
	{
72
		return $this->get( 'order.base.service.transaction.parentid' );
73
	}
74
75
76
	/**
77
	 * Sets the ID of the ordered service item as parent
78
	 *
79
	 * @param string|null $id ID of the ordered service item
80
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order base service transaction item for chaining method calls
81
	 */
82
	public function setParentId( ?string $id ) : \Aimeos\MShop\Common\Item\Iface
83
	{
84
		return $this->set( 'order.base.service.transaction.parentid', $id );
85
	}
86
87
88
	/**
89
	 * Returns the type of the service transaction item.
90
	 *
91
	 * @return string Type of the service transaction item
92
	 */
93
	public function getType() : string
94
	{
95
		return $this->get( 'order.base.service.transaction.type', '' );
96
	}
97
98
99
	/**
100
	 * Sets a new type for the service transaction item.
101
	 *
102
	 * @param string $type Type of the service transaction
103
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order base service transaction item for chaining method calls
104
	 */
105
	public function setType( string $type ) : \Aimeos\MShop\Common\Item\Iface
106
	{
107
		return $this->set( 'order.base.service.transaction.type', $this->checkCode( $type ) );
108
	}
109
110
111
	/**
112
	 * Returns the configuration values of the item
113
	 *
114
	 * @return array Configuration values
115
	 */
116
	public function getConfig() : array
117
	{
118
		return $this->get( 'order.base.service.transaction.config', [] );
119
	}
120
121
122
	/**
123
	 * Sets the configuration values of the item.
124
	 *
125
	 * @param array $config Configuration values
126
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order base service transaction item for chaining method calls
127
	 */
128
	public function setConfig( array $config ) : \Aimeos\MShop\Common\Item\Iface
129
	{
130
		return $this->set( 'order.base.service.transaction.config', $config );
131
	}
132
133
134
	/**
135
	 * Returns the price item for the transaction.
136
	 *
137
	 * @return \Aimeos\MShop\Price\Item\Iface Price item with price, costs and rebate
138
	 */
139
	public function getPrice() : \Aimeos\MShop\Price\Item\Iface
140
	{
141
		return $this->price;
142
	}
143
144
145
	/**
146
	 * Sets the price item for the transaction.
147
	 *
148
	 * @param \Aimeos\MShop\Price\Item\Iface $price Price item containing price and additional costs
149
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order base service transaction item for chaining method calls
150
	 */
151
	public function setPrice( \Aimeos\MShop\Price\Item\Iface $price ) : \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface
152
	{
153
		if( $price !== $this->price )
154
		{
155
			$this->price = $price;
156
			$this->setModified();
157
		}
158
159
		return $this;
160
	}
161
162
163
	/**
164
	 * Returns the status of the transaction
165
	 *
166
	 * @return int Status of the transaction
167
	 */
168
	public function getStatus() : int
169
	{
170
		return $this->get( 'order.base.service.transaction.status', 1 );
171
	}
172
173
174
	/**
175
	 * Sets the new status of the transaction
176
	 *
177
	 * @param int $status New status of the transaction
178
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order base service transaction item for chaining method calls
179
	 */
180
	public function setStatus( int $status ) : \Aimeos\MShop\Common\Item\Iface
181
	{
182
		return $this->set( 'order.base.service.transaction.status', $status );
183
	}
184
185
186
	/**
187
	 * Returns the item type
188
	 *
189
	 * @return string Item type, subtypes are separated by slashes
190
	 */
191
	public function getResourceType() : string
192
	{
193
		return 'order/base/service/transaction';
194
	}
195
196
197
	/*
198
	 * Sets the item values from the given array and removes that entries from the list
199
	 *
200
	 * @param array &$list Associative list of item keys and their values
201
	 * @param bool True to set private properties too, false for public only
202
	 * @return \Aimeos\MShop\Order\Item\Base\Service\Transaction\Iface Order service transaction item for chaining method calls
203
	 */
204
	public function fromArray( array &$list, bool $private = false ) : \Aimeos\MShop\Common\Item\Iface
205
	{
206
		$item = parent::fromArray( $list, $private );
207
		$price = $item->getPrice();
208
209
		foreach( $list as $key => $value )
210
		{
211
			switch( $key )
212
			{
213
				case 'order.base.service.transaction.parentid': !$private ?: $item = $item->setParentId( $value ); break;
214
				case 'order.base.service.transaction.siteid': !$private ?: $item = $item->setSiteId( $value ); break;
215
				case 'order.base.service.transaction.config': $item = $item->setConfig( (array) $value ); break;
216
				case 'order.base.service.transaction.status': $item = $item->setStatus( (int) $value ); break;
217
				case 'order.base.service.transaction.currencyid': $price->setCurrencyId( $value ); break;
218
				case 'order.base.service.transaction.type': $item = $item->setType( $value ); break;
219
				case 'order.base.service.transaction.price': $price->setValue( $value ); break;
220
				case 'order.base.service.transaction.costs': $price->setCosts( $value ); break;
221
				case 'order.base.service.transaction.rebate': $price->setRebate( $value ); break;
222
				case 'order.base.service.transaction.taxvalue': $price->setTaxvalue( $value ); break;
223
				case 'order.base.service.transaction.taxflag': $price->setTaxflag( (bool) $value ); break;
224
				default: continue 2;
225
			}
226
227
			unset( $list[$key] );
228
		}
229
230
		return $item;
231
	}
232
233
234
	/**
235
	 * Returns the item values as array.
236
	 *
237
	 * @param bool True to return private properties, false for public only
238
	 * @return array Associative list of item properties and their values
239
	 */
240
	public function toArray( bool $private = false ) : array
241
	{
242
		$list = parent::toArray( $private );
243
		$price = $this->getPrice();
244
245
		$list['order.base.service.transaction.type'] = $this->getType();
246
		$list['order.base.service.transaction.config'] = $this->getConfig();
247
		$list['order.base.service.transaction.status'] = $this->getStatus();
248
		$list['order.base.service.transaction.currencyid'] = $price->getCurrencyId();
249
		$list['order.base.service.transaction.price'] = $price->getValue();
250
		$list['order.base.service.transaction.costs'] = $price->getCosts();
251
		$list['order.base.service.transaction.rebate'] = $price->getRebate();
252
		$list['order.base.service.transaction.taxvalue'] = $price->getTaxvalue();
253
		$list['order.base.service.transaction.taxflag'] = $price->getTaxflag();
254
255
		if( $private === true ) {
256
			$list['order.base.service.transaction.parentid'] = $this->getParentId();
257
		}
258
259
		return $list;
260
	}
261
}
262