Passed
Push — master ( 51d5ad...84139c )
by Aimeos
04:55
created

Nolimit::search()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 3
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2024
6
 * @package MShop
7
 * @subpackage Stock
8
 */
9
10
11
namespace Aimeos\MShop\Stock\Manager;
12
13
14
/**
15
 * Stock manager implementation for unlimited stocks
16
 *
17
 * @package MShop
18
 * @subpackage Stock
19
 */
20
class Nolimit
21
	extends \Aimeos\MShop\Stock\Manager\Standard
22
	implements \Aimeos\MShop\Stock\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface
23
{
24
	/**
25
	 * Removes old entries from the storage.
26
	 *
27
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
28
	 * @return \Aimeos\MShop\Stock\Manager\Iface Manager object for chaining method calls
29
	 */
30
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
31
	{
32
		return $this;
33
	}
34
35
36
	/**
37
	 * Decreases the stock level for the given product ID/quantity pairs and type
38
	 *
39
	 * @param array $pairs Associative list of product IDs as keys and quantities as values
40
	 * @param string $type Unique code of the stock type
41
	 * @return \Aimeos\MShop\Stock\Manager\Iface Manager object for chaining method calls
42
	 */
43
	public function decrease( iterable $pairs, string $type = 'default' ) : \Aimeos\MShop\Stock\Manager\Iface
44
	{
45
		return $this;
46
	}
47
48
49
	/**
50
	 * Removes multiple items.
51
	 *
52
	 * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $itemIds List of item objects or IDs of the items
53
	 * @return \Aimeos\MShop\Stock\Manager\Iface Manager object for chaining method calls
54
	 */
55
	public function delete( $itemIds ) : \Aimeos\MShop\Common\Manager\Iface
56
	{
57
		return $this;
58
	}
59
60
61
	/**
62
	 * Creates a stock item object for the given item id.
63
	 *
64
	 * @param string $id Id of the stock item
65
	 * @param string[] $ref List of domains to fetch list items and referenced items for
66
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
67
	 * @return \Aimeos\MShop\Stock\Item\Iface Returns the product stock item of the given id
68
	 * @throws \Aimeos\MShop\Exception If item couldn't be found
69
	 */
70
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
71
	{
72
		$values = ['stock.id' => $id, 'stock.type' => 'default'];
73
		return $this->object()->create( $values );
74
	}
75
76
77
	/**
78
	 * Increases the stock level for the given product ID/quantity pairs and type
79
	 *
80
	 * @param array $pairs Associative list of product IDs as keys and quantities as values
81
	 * @param string $type Unique code of the type
82
	 * @return \Aimeos\MShop\Stock\Manager\Iface Manager object for chaining method calls
83
	 */
84
	public function increase( iterable $pairs, string $type = 'default' ) : \Aimeos\MShop\Stock\Manager\Iface
85
	{
86
		return $this;
87
	}
88
89
90
	/**
91
	 * Search for stock items based on the given critera.
92
	 *
93
	 * @param \Aimeos\Base\Criteria\Iface $search Search criteria object
94
	 * @param string[] $ref List of domains to fetch list items and referenced items for
95
	 * @param int|null &$total Number of items that are available in total
96
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Stock\Item\Iface with ids as keys
97
	 */
98
	public function search( \Aimeos\Base\Criteria\Iface $search, array $ref = [], int &$total = null ) : \Aimeos\Map
99
	{
100
		$items = [];
101
		$item = $this->object()->create( ['stock.type' => 'default'] );
102
103
		foreach( $this->getProductIds( $search->getConditions() ) as $idx => $prodid ) {
104
			$items[$idx] = ( clone $item )->setProductId( $prodid )->setId( $idx );
105
		}
106
107
		if( $total !== null ) {
108
			$total = count( $items );
109
		}
110
111
		return map( array_splice( $items, 0, $search->getLimit() ) );
112
	}
113
114
115
	/**
116
	 * Returns the product IDs from the conditions
117
	 *
118
	 * @param \Aimeos\Base\Criteria\Expression\Iface|null $cond Criteria object
119
	 * @return string[] List of product IDs
120
	 */
121
	protected function getProductIds( \Aimeos\Base\Criteria\Expression\Iface $cond = null ) : array
122
	{
123
		$list = [];
124
125
		if( $cond instanceof \Aimeos\Base\Criteria\Expression\Combine\Iface )
126
		{
127
			foreach( $cond->getExpressions() as $expr ) {
128
				$list = array_merge( $list, $this->getProductIds( $expr ) );
129
			}
130
		}
131
		elseif( $cond instanceof \Aimeos\Base\Criteria\Expression\Compare\Iface )
132
		{
133
			if( $cond->getName() === 'stock.productid' && $cond->getOperator() === '==' ) {
134
				$list = array_merge( $list, (array) $cond->getValue() );
135
			}
136
		}
137
138
		return $list;
139
	}
140
141
142
	/**
143
	 * Inserts the new stock item
144
	 *
145
	 * @param \Aimeos\MShop\Common\Item\Iface $item Stock item which should be saved
146
	 * @param bool $fetch True if the new ID should be returned in the item
147
	 * @return \Aimeos\MShop\Stock\Item\Iface Updated item including the generated ID
148
	 */
149
	protected function saveBase( \Aimeos\MShop\Common\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Stock\Item\Iface
150
	{
151
		return $item;
152
	}
153
}
154