Passed
Push — master ( 9a0039...2f021f )
by Aimeos
07:58
created

Standard::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2021
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 $domains = [];
25
	private $filter;
26
	private $manager;
27
28
29
	/**
30
	 * Common initialization for controller classes
31
	 *
32
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
33
	 */
34
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
35
	{
36
		parent::__construct( $context );
37
38
		$this->manager = \Aimeos\MShop::create( $context, 'supplier' );
39
		$this->filter = $this->manager->filter( true );
40
		$this->addExpression( $this->filter->getConditions() );
41
	}
42
43
44
	/**
45
	 * Clones objects in controller and resets values
46
	 */
47
	public function __clone()
48
	{
49
		$this->filter = clone $this->filter;
50
	}
51
52
53
	/**
54
	 * Adds generic condition for filtering
55
	 *
56
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
57
	 * @param string $key Search key defined by the supplier manager, e.g. "supplier.status"
58
	 * @param array|string $value Value or list of values to compare to
59
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
60
	 * @since 2019.04
61
	 */
62
	public function compare( string $operator, string $key, $value ) : Iface
63
	{
64
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
65
		return $this;
66
	}
67
68
69
	/**
70
	 * Returns the supplier for the given supplier code
71
	 *
72
	 * @param string $code Unique supplier code
73
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
74
	 * @since 2019.04
75
	 */
76
	public function find( string $code ) : \Aimeos\MShop\Supplier\Item\Iface
77
	{
78
		return $this->manager->find( $code, $this->domains, null, null, true );
79
	}
80
81
82
	/**
83
	 * Creates a search function string for the given name and parameters
84
	 *
85
	 * @param string $name Name of the search function without parenthesis, e.g. "supplier:has"
86
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
87
	 * @return string Search function string that can be used in compare()
88
	 */
89
	public function function( string $name, array $params ) : string
90
	{
91
		return $this->filter->make( $name, $params );
92
	}
93
94
95
	/**
96
	 * Returns the supplier for the given supplier ID
97
	 *
98
	 * @param string $id Unique supplier ID
99
	 * @return \Aimeos\MShop\Supplier\Item\Iface Supplier item including the referenced domains items
100
	 * @since 2019.04
101
	 */
102
	public function get( string $id ) : \Aimeos\MShop\Supplier\Item\Iface
103
	{
104
		return $this->manager->get( $id, $this->domains, true );
105
	}
106
107
108
	/**
109
	 * Adds a filter to return only items containing a reference to the given ID
110
	 *
111
	 * @param string $domain Domain name of the referenced item, e.g. "product"
112
	 * @param string|null $type Type code of the reference, e.g. "default" or null for all types
113
	 * @param string|null $refId ID of the referenced item of the given domain or null for all references
114
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
115
	 * @since 2019.10
116
	 */
117
	public function has( string $domain, string $type = null, string $refId = null ) : Iface
118
	{
119
		$params = [$domain];
120
		!$type ?: $params[] = $type;
121
		!$refId ?: $params[] = $refId;
122
123
		$func = $this->filter->make( 'supplier:has', $params );
124
		$this->addExpression( $this->filter->compare( '!=', $func, null ) );
125
		return $this;
126
	}
127
128
129
	/**
130
	 * Parses the given array and adds the conditions to the list of conditions
131
	 *
132
	 * @param array $conditions List of conditions, e.g. ['=~' => ['supplier.label' => 'test']]
133
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
134
	 * @since 2019.04
135
	 */
136
	public function parse( array $conditions ) : Iface
137
	{
138
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
139
			$this->addExpression( $cond );
140
		}
141
142
		return $this;
143
	}
144
145
146
	/**
147
	 * Returns the suppliers filtered by the previously assigned conditions
148
	 *
149
	 * @param int &$total Parameter where the total number of found suppliers will be stored in
150
	 * @return \Aimeos\Map Ordered list of supplier items implementing \Aimeos\MShop\Supplier\Item\Iface
151
	 * @since 2019.04
152
	 */
153
	public function search( int &$total = null ) : \Aimeos\Map
154
	{
155
		$this->filter->setSortations( $this->getSortations() );
156
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
157
158
		return $this->manager->search( $this->filter, $this->domains, $total );
159
	}
160
161
162
	/**
163
	 * Sets the start value and the number of returned supplier items for slicing the list of found supplier items
164
	 *
165
	 * @param int $start Start value of the first supplier item in the list
166
	 * @param int $limit Number of returned supplier items
167
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
168
	 * @since 2019.04
169
	 */
170
	public function slice( int $start, int $limit ) : Iface
171
	{
172
		$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 );
173
		$this->filter->slice( $start, min( $limit, $maxsize ) );
174
		return $this;
175
	}
176
177
178
	/**
179
	 * Sets the sorting of the result list
180
	 *
181
	 * @param string|null $key Sorting key of the result list like "supplier.label", null for no sorting
182
	 * @return \Aimeos\Controller\Frontend\Supplier\Iface Supplier controller for fluent interface
183
	 * @since 2019.04
184
	 */
185
	public function sort( string $key = null ) : Iface
186
	{
187
		$list = $this->splitKeys( $key );
188
189
		foreach( $list as $sortkey )
190
		{
191
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
192
			$this->addExpression( $this->filter->sort( $direction, ltrim( $sortkey, '+-' ) ) );
193
		}
194
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
213
	/**
214
	 * Returns the manager used by the controller
215
	 *
216
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
217
	 */
218
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
219
	{
220
		return $this->manager;
221
	}
222
}
223