Passed
Push — master ( d7cab9...07cded )
by Aimeos
04:47
created

Base::getAttributeItem()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 16
rs 9.2222
cc 6
nc 8
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Item\Service;
12
13
14
/**
15
 * Order service item abstract class defining available types.
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
abstract class Base extends \Aimeos\MShop\Common\Item\Base implements Iface
21
{
22
	/**
23
	 * Delivery service.
24
	 */
25
	const TYPE_DELIVERY = 'delivery';
26
27
	/**
28
	 * Payment service.
29
	 */
30
	const TYPE_PAYMENT = 'payment';
31
32
33
	private ?array $attributesMap = null;
34
35
36
	/**
37
	 * Adds new and replaces existing attribute items for the service.
38
	 *
39
	 * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Service\Attribute\Iface[] $attributes List of order service attribute items
40
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
41
	 */
42
	public function addAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Service\Iface
43
	{
44
		map( $attributes )->implements( \Aimeos\MShop\Order\Item\Service\Attribute\Iface::class, true );
45
46
		foreach( $attributes as $attrItem ) {
47
			$this->setAttributeItem( $attrItem );
48
		}
49
50
		return $this;
51
	}
52
53
54
	/**
55
	 * Returns the value or list of values of the attribute item for the ordered service with the given code.
56
	 *
57
	 * @param string $code Code of the service attribute item
58
	 * @param array|string $type Type or list of types of the service attribute items
59
	 * @return array|string|null Value or list of values of the attribute item for the ordered service and the given code
60
	 */
61
	public function getAttribute( string $code, $type = '' )
62
	{
63
		$list = [];
64
		$map = $this->getAttributeMap();
65
66
		foreach( (array) $type as $key )
67
		{
68
			if( isset( $map[$key][$code] ) )
69
			{
70
				foreach( $map[$key][$code] as $item ) {
71
					$list[] = $item->getValue();
72
				}
73
			}
74
		}
75
76
		return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
77
	}
78
79
80
	/**
81
	 * Returns the attribute item or list of attribute items for the ordered service with the given code.
82
	 *
83
	 * @param string $code Code of the service attribute item
84
	 * @param array|string $type Type of the service attribute item
85
	 * @return \Aimeos\MShop\Order\Item\Service\Attribute\Iface|array|null
86
	 * 	Attribute item or list of items for the ordered service and the given code
87
	 */
88
	public function getAttributeItem( string $code, $type = '' )
89
	{
90
		$list = [];
91
		$map = $this->getAttributeMap();
92
93
		foreach( (array) $type as $key )
94
		{
95
			if( isset( $map[$key][$code] ) )
96
			{
97
				foreach( $map[$key][$code] as $item ) {
98
					$list[] = $item;
99
				}
100
			}
101
		}
102
103
		return count( $list ) > 1 ? $list : ( reset( $list ) ?: null );
104
	}
105
106
107
	/**
108
	 * Returns the list of attribute items for the ordered service.
109
	 *
110
	 * @param string|null $type Filters returned attributes by the given type or null for no filtering
111
	 * @return \Aimeos\Map List of attribute items implementing \Aimeos\MShop\Order\Item\Service\Attribute\Iface
112
	 */
113
	public function getAttributeItems( string $type = null ) : \Aimeos\Map
114
	{
115
		if( $type === null ) {
116
			return map( $this->get( '.attributes', [] ) );
117
		}
118
119
		$list = [];
120
121
		foreach( $this->get( '.attributes', [] ) as $attrItem )
122
		{
123
			if( $attrItem->getType() === $type ) {
124
				$list[] = $attrItem;
125
			}
126
		}
127
128
		return map( $list );
129
	}
130
131
132
	/**
133
	 * Adds or replaces the attribute item in the list of service attributes.
134
	 *
135
	 * @param \Aimeos\MShop\Order\Item\Service\Attribute\Iface $item Service attribute item
136
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
137
	 */
138
	public function setAttributeItem( \Aimeos\MShop\Order\Item\Service\Attribute\Iface $item ) : \Aimeos\MShop\Order\Item\Service\Iface
139
	{
140
		$this->getAttributeMap();
141
142
		$type = $item->getType();
143
		$code = $item->getCode();
144
		$attrId = $item->getAttributeId();
145
146
		if( !isset( $this->attributesMap[$type][$code][$attrId] ) )
147
		{
148
			$this->set( '.attributes', map( $this->get( '.attributes', [] ) )->push( $item ) );
149
			$this->attributesMap[$type][$code][$attrId] = $item;
150
		}
151
152
		$this->attributesMap[$type][$code][$attrId]->setValue( $item->getValue() );
153
		$this->setModified();
154
155
		return $this;
156
	}
157
158
159
	/**
160
	 * Sets the new list of attribute items for the service.
161
	 *
162
	 * @param \Aimeos\Map|\Aimeos\MShop\Order\Item\Service\Attribute\Iface[] $attributes List of order service attribute items
163
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
164
	 */
165
	public function setAttributeItems( iterable $attributes ) : \Aimeos\MShop\Order\Item\Service\Iface
166
	{
167
		( $attributes = map( $attributes ) )->implements( \Aimeos\MShop\Order\Item\Service\Attribute\Iface::class, true );
168
169
		$this->set( '.attributes', $attributes );
170
		$this->attributesMap = null;
171
172
		return $this;
173
	}
174
175
176
	/**
177
	 * Adds a new transaction to the service.
178
	 *
179
	 * @param \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item Transaction item
180
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
181
	 */
182
	public function addTransaction( \Aimeos\MShop\Order\Item\Service\Transaction\Iface $item ) : \Aimeos\MShop\Order\Item\Service\Iface
183
	{
184
		return $this->set( '.transactions', map( $this->get( '.transactions', [] ) )->push( $item ) );
185
186
		$this->transactions[] = $item;
0 ignored issues
show
Unused Code introduced by
$this->transactions[] = $item is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
187
		$this->setModified();
188
189
		return $this;
190
	}
191
192
193
	/**
194
	 * Returns the list of transactions items for the service.
195
	 *
196
	 * @param string|null $type Filters returned transactions by the given type or null for no filtering
197
	 * @return \Aimeos\Map List of transaction items implementing \Aimeos\MShop\Order\Item\Service\Attribute\Iface
198
	 */
199
	public function getTransactions( string $type = null ) : \Aimeos\Map
200
	{
201
		return map( $this->get( '.transactions', [] ) );
202
	}
203
204
205
	/**
206
	 * Sets the new list of transactions items for the service.
207
	 *
208
	 * @param iterable $list List of order service transaction items
209
	 * @return \Aimeos\MShop\Order\Item\Service\Iface Order base service item for chaining method calls
210
	 */
211
	public function setTransactions( iterable $list ) : \Aimeos\MShop\Order\Item\Service\Iface
212
	{
213
		return $this->set( '.transactions', $list );
214
	}
215
216
217
	/**
218
	 * Checks if the given flag constant is valid.
219
	 *
220
	 * @param int $value Flag constant value
221
	 */
222
	protected function checkFlags( int $value )
223
	{
224
		if( $value < \Aimeos\MShop\Order\Item\Service\Base::FLAG_NONE ||
0 ignored issues
show
Bug introduced by
The constant Aimeos\MShop\Order\Item\Service\Base::FLAG_NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
225
			$value > \Aimeos\MShop\Order\Item\Service\Base::FLAG_IMMUTABLE
0 ignored issues
show
Bug introduced by
The constant Aimeos\MShop\Order\Item\...ce\Base::FLAG_IMMUTABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
226
		) {
227
			throw new \Aimeos\MShop\Order\Exception( sprintf( 'Flags "%1$s" not within allowed range', $value ) );
228
		}
229
230
		return $value;
231
	}
232
233
234
	/**
235
	 * Returns the attribute map for the ordered services.
236
	 *
237
	 * @return array Associative list of type and code as key and an \Aimeos\MShop\Order\Item\Service\Attribute\Iface as value
238
	 */
239
	protected function getAttributeMap() : array
240
	{
241
		if( !isset( $this->attributesMap ) )
242
		{
243
			$this->attributesMap = [];
244
245
			foreach( $this->get( '.attributes', [] ) as $item ) {
246
				$this->attributesMap[$item->getType()][$item->getCode()][$item->getAttributeId()] = $item;
247
			}
248
		}
249
250
		return $this->attributesMap;
251
	}
252
}
253