Completed
Push — master ( 94eb6c...a336e2 )
by Aimeos
02:13
created

Standard::uses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018
6
 * @package Controller
7
 * @subpackage Frontend
8
 */
9
10
11
namespace Aimeos\Controller\Frontend\Supplier;
12
13
14
/**
15
 * Default implementation of the supplier frontend controller
16
 *
17
 * @package Controller
18
 * @subpackage Frontend
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Frontend\Base
22
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
23
{
24
	private $conditions = [];
25
	private $domains = [];
26
	private $filter;
27
	private $manager;
28
29
30
	/**
31
	 * Common initialization for controller classes
32
	 *
33
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
34
	 */
35
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
36
	{
37
		parent::__construct( $context );
38
39
		$this->manager = \Aimeos\MShop::create( $context, 'supplier' );
40
		$this->filter = $this->manager->createSearch( true );
41
		$this->conditions[] = $this->filter->getConditions();
42
	}
43
44
45
	/**
46
	 * Clones objects in controller and resets values
47
	 */
48
	public function __clone()
49
	{
50
		$this->filter = clone $this->filter;
51
	}
52
53
54
	/**
55
	 * Adds generic condition for filtering
56
	 *
57
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
58
	 * @param string $key Search key defined by the supplier manager, e.g. "supplier.status"
59
	 * @param array|string $value Value or list of values to compare to
60
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
61
	 * @since 2019.04
62
	 */
63
	public function compare( string $operator, string $key, $value ) : Iface
64
	{
65
		$this->conditions[] = $this->filter->compare( $operator, $key, $value );
66
		return $this;
67
	}
68
69
70
	/**
71
	 * Returns the supplier for the given supplier code
72
	 *
73
	 * @param string $code Unique supplier code
74
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
75
	 * @since 2019.04
76
	 */
77
	public function find( string $code ) : \Aimeos\MShop\Supplier\Item\Iface
78
	{
79
		return $this->manager->findItem( $code, $this->domains, null, null, true );
80
	}
81
82
83
	/**
84
	 * Creates a search function string for the given name and parameters
85
	 *
86
	 * @param string $name Name of the search function without parenthesis, e.g. "supplier:has"
87
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
88
	 * @param string Search function string that can be used in compare()
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Frontend\Supplier\Search was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
	 */
90
	public function function( string $name, array $params ) : string
91
	{
92
		return $this->filter->createFunction( $name, $params );
93
	}
94
95
96
	/**
97
	 * Returns the supplier for the given supplier ID
98
	 *
99
	 * @param string $id Unique supplier ID
100
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
101
	 * @since 2019.04
102
	 */
103
	public function get( string $id ) : \Aimeos\MShop\Supplier\Item\Iface
104
	{
105
		return $this->manager->getItem( $id, $this->domains, true );
106
	}
107
108
109
	/**
110
	 * Adds a filter to return only items containing a reference to the given ID
111
	 *
112
	 * @param string $domain Domain name of the referenced item, e.g. "product"
113
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
114
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
115
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
116
	 * @since 2019.10
117
	 */
118
	public function has( string $domain, string $type = null, string $refId = null ) : Iface
119
	{
120
		$params = [$domain];
121
		!$type ?: $params[] = $type;
122
		!$refId ?: $params[] = $refId;
123
124
		$func = $this->filter->createFunction( 'supplier:has', $params );
125
		$this->conditions[] = $this->filter->compare( '!=', $func, null );
126
		return $this;
127
	}
128
129
130
	/**
131
	 * Parses the given array and adds the conditions to the list of conditions
132
	 *
133
	 * @param array $conditions List of conditions, e.g. ['=~' => ['supplier.label' => 'test']]
134
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
135
	 * @since 2019.04
136
	 */
137
	public function parse( array $conditions ) : Iface
138
	{
139
		if( ( $cond = $this->filter->toConditions( $conditions ) ) !== null ) {
140
			$this->conditions[] = $cond;
141
		}
142
143
		return $this;
144
	}
145
146
147
	/**
148
	 * Returns the suppliers filtered by the previously assigned conditions
149
	 *
150
	 * @param int &$total Parameter where the total number of found suppliers will be stored in
151
	 * @return \Aimeos\MShop\Supplier\Item\Iface[] Ordered list of supplier items
152
	 * @since 2019.04
153
	 */
154
	public function search( int &$total = null )
155
	{
156
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
157
		return $this->manager->searchItems( $this->filter, $this->domains, $total );
158
	}
159
160
161
	/**
162
	 * Sets the start value and the number of returned supplier items for slicing the list of found supplier items
163
	 *
164
	 * @param int $start Start value of the first supplier item in the list
165
	 * @param int $limit Number of returned supplier items
166
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
167
	 * @since 2019.04
168
	 */
169
	public function slice( int $start, int $limit ) : Iface
170
	{
171
		$this->filter->setSlice( $start, $limit );
172
		return $this;
173
	}
174
175
176
	/**
177
	 * Sets the sorting of the result list
178
	 *
179
	 * @param string|null $key Sorting key of the result list like "supplier.label", null for no sorting
180
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
181
	 * @since 2019.04
182
	 */
183
	public function sort( string $key = null ) : Iface
184
	{
185
		$sort = [];
186
		$list = ( $key ? explode( ',', $key ) : [] );
187
188
		foreach( $list as $sortkey )
189
		{
190
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
191
			$sort[] = $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) );
192
		}
193
194
		$this->filter->setSortations( $sort );
195
		return $this;
196
	}
197
198
199
	/**
200
	 * Sets the referenced domains that will be fetched too when retrieving items
201
	 *
202
	 * @param array $domains Domain names of the referenced items that should be fetched too
203
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
204
	 * @since 2019.04
205
	 */
206
	public function uses( array $domains ) : Iface
207
	{
208
		$this->domains = $domains;
209
		return $this;
210
	}
211
}
212