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

Standard::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
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 Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package Controller
8
 * @subpackage Frontend
9
 */
10
11
12
namespace Aimeos\Controller\Frontend\Service;
13
14
use \Psr\Http\Message\ServerRequestInterface;
15
use \Psr\Http\Message\ResponseInterface;
16
17
18
/**
19
 * Default implementation of the service frontend controller.
20
 *
21
 * @package Controller
22
 * @subpackage Frontend
23
 */
24
class Standard
25
	extends \Aimeos\Controller\Frontend\Base
26
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
27
{
28
	private $domains = [];
29
	private $filter;
30
	private $manager;
31
32
33
	/**
34
	 * Common initialization for controller classes
35
	 *
36
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
37
	 */
38
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
39
	{
40
		parent::__construct( $context );
41
42
		$this->manager = \Aimeos\MShop::create( $context, 'service' );
43
		$this->filter = $this->manager->filter( true );
44
45
		$this->addExpression( $this->filter->getConditions() );
46
		$this->addExpression( $this->filter->sort( '+', 'service.position' ) );
47
	}
48
49
50
	/**
51
	 * Adds generic condition for filtering services
52
	 *
53
	 * @param string $operator Comparison operator, e.g. "==", "!=", "<", "<=", ">=", ">", "=~", "~="
54
	 * @param string $key Search key defined by the service manager, e.g. "service.status"
55
	 * @param array|string $value Value or list of values to compare to
56
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
57
	 * @since 2019.04
58
	 */
59
	public function compare( string $operator, string $key, $value ) : Iface
60
	{
61
		$this->addExpression( $this->filter->compare( $operator, $key, $value ) );
62
		return $this;
63
	}
64
65
66
	/**
67
	 * Returns the service for the given code
68
	 *
69
	 * @param string $code Unique service code
70
	 * @return \Aimeos\MShop\Service\Item\Iface Service item including the referenced domains items
71
	 * @since 2019.04
72
	 */
73
	public function find( string $code ) : \Aimeos\MShop\Service\Item\Iface
74
	{
75
		return $this->manager->find( $code, $this->domains, null, null, true );
76
	}
77
78
79
	/**
80
	 * Creates a search function string for the given name and parameters
81
	 *
82
	 * @param string $name Name of the search function without parenthesis, e.g. "service:has"
83
	 * @param array $params List of parameters for the search function with numeric keys starting at 0
84
	 * @return string Search function string that can be used in compare()
85
	 */
86
	public function function( string $name, array $params ) : string
87
	{
88
		return $this->filter->make( $name, $params );
89
	}
90
91
92
	/**
93
	 * Returns the service for the given ID
94
	 *
95
	 * @param string $id Unique service ID
96
	 * @return \Aimeos\MShop\Service\Item\Iface Service item including the referenced domains items
97
	 * @since 2019.04
98
	 */
99
	public function get( string $id ) : \Aimeos\MShop\Service\Item\Iface
100
	{
101
		return $this->manager->get( $id, $this->domains, true );
102
	}
103
104
105
	/**
106
	 * Returns the service item for the given ID
107
	 *
108
	 * @param string $serviceId Unique service ID
109
	 * @return \Aimeos\MShop\Service\Provider\Iface Service provider object
110
	 */
111
	public function getProvider( string $serviceId ) : \Aimeos\MShop\Service\Provider\Iface
112
	{
113
		$item = $this->manager->get( $serviceId, $this->domains, true );
114
		return $this->manager->getProvider( $item, $item->getType() );
115
	}
116
117
118
	/**
119
	 * Returns the service providers of the given type
120
	 *
121
	 * @return \Aimeos\Map List of service IDs as keys and service provider objects as values
122
	 */
123
	public function getProviders() : \Aimeos\Map
124
	{
125
		$list = [];
126
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) )->order( 'service.position' );
127
128
		foreach( $this->manager->search( $this->filter, $this->domains ) as $id => $item ) {
129
			$list[$id] = $this->manager->getProvider( $item, $item->getType() );
130
		}
131
132
		return map( $list );
133
	}
134
135
136
	/**
137
	 * Parses the given array and adds the conditions to the list of conditions
138
	 *
139
	 * @param array $conditions List of conditions, e.g. ['&&' => [['>' => ['service.status' => 0]], ['==' => ['service.type' => 'default']]]]
140
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
141
	 * @since 2019.04
142
	 */
143
	public function parse( array $conditions ) : Iface
144
	{
145
		if( ( $cond = $this->filter->parse( $conditions ) ) !== null ) {
146
			$this->addExpression( $cond );
147
		}
148
149
		return $this;
150
	}
151
152
153
	/**
154
	 * Processes the payment service for the given order
155
	 *
156
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order which should be processed
157
	 * @param string $serviceId Unique service item ID
158
	 * @param array $urls Associative list of keys and the corresponding URLs
159
	 * 	(keys are payment.url-self, payment.url-success, payment.url-update)
160
	 * @param array $params Request parameters and order service attributes
161
	 * @return \Aimeos\MShop\Common\Helper\Form\Iface|null Form object with URL, parameters, etc.
162
	 * 	or null if no form data is required
163
	 */
164
	public function process( \Aimeos\MShop\Order\Item\Iface $orderItem,
165
		string $serviceId, array $urls, array $params ) : ?\Aimeos\MShop\Common\Helper\Form\Iface
166
	{
167
		$item = $this->manager->get( $serviceId, [], true );
168
169
		$provider = $this->manager->getProvider( $item, $item->getType() );
170
		$provider->injectGlobalConfigBE( $urls );
171
172
		return $provider->process( $orderItem, $params );
173
	}
174
175
176
	/**
177
	 * Returns the services filtered by the previously assigned conditions
178
	 *
179
	 * @param int &$total Parameter where the total number of found services will be stored in
180
	 * @return \Aimeos\Map Ordered list of service items implementing \Aimeos\MShop\Service\Item\Iface
181
	 * @since 2019.04
182
	 */
183
	public function search( int &$total = null ) : \Aimeos\Map
184
	{
185
		$this->filter->setSortations( $this->getSortations() );
186
		$this->filter->setConditions( $this->filter->and( $this->getConditions() ) );
187
188
		return $this->manager->search( $this->filter, $this->domains, $total );
189
	}
190
191
192
	/**
193
	 * Sets the start value and the number of returned services for slicing the list of found services
194
	 *
195
	 * @param int $start Start value of the first attribute in the list
196
	 * @param int $limit Number of returned services
197
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
198
	 * @since 2019.04
199
	 */
200
	public function slice( int $start, int $limit ) : Iface
201
	{
202
		$maxsize = $this->getContext()->config()->get( 'controller/frontend/common/max-size', 250 );
203
		$this->filter->slice( $start, min( $limit, $maxsize ) );
204
		return $this;
205
	}
206
207
208
	/**
209
	 * Sets the sorting of the result list
210
	 *
211
	 * @param string|null $key Sorting of the result list like "position", null for no sorting
212
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
213
	 * @since 2019.04
214
	 */
215
	public function sort( string $key = null ) : Iface
216
	{
217
		$list = $this->splitKeys( $key );
218
219
		foreach( $list as $sortkey )
220
		{
221
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
222
			$sortkey = ltrim( $sortkey, '+-' );
223
224
			switch( $sortkey )
225
			{
226
				case 'type':
227
					$this->addExpression( $this->filter->sort( $direction, 'service.type' ) ); break;
228
				default:
229
					$this->addExpression( $this->filter->sort( $direction, $sortkey ) );
230
			}
231
		}
232
233
		return $this;
234
	}
235
236
237
	/**
238
	 * Adds attribute types for filtering
239
	 *
240
	 * @param array|string $code Service type or list of types
241
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
242
	 * @since 2019.04
243
	 */
244
	public function type( $code ) : Iface
245
	{
246
		if( $code ) {
247
			$this->addExpression( $this->filter->compare( '==', 'service.type', $code ) );
248
		}
249
250
		return $this;
251
	}
252
253
254
	/**
255
	 * Updates the order status sent by payment gateway notifications
256
	 *
257
	 * @param ServerRequestInterface $request Request object
258
	 * @param ResponseInterface $response Response object that will contain HTTP status and response body
259
	 * @param string $code Unique code of the service used for the current order
260
	 * @return \Psr\Http\Message\ResponseInterface Response object
261
	 */
262
	public function updatePush( ServerRequestInterface $request, ResponseInterface $response,
263
		string $code ) : \Psr\Http\Message\ResponseInterface
264
	{
265
		$item = $this->manager->find( $code );
266
		$provider = $this->manager->getProvider( $item, $item->getType() );
267
268
		return $provider->updatePush( $request, $response );
269
	}
270
271
272
	/**
273
	 * Updates the payment or delivery status for the given request
274
	 *
275
	 * @param ServerRequestInterface $request Request object with parameters and request body
276
	 * @param string $code Unique code of the service used for the current order
277
	 * @param string $orderid ID of the order whose payment status should be updated
278
	 * @return \Aimeos\MShop\Order\Item\Iface $orderItem Order item that has been updated
279
	 */
280
	public function updateSync( ServerRequestInterface $request,
281
		string $code, string $orderid ) : \Aimeos\MShop\Order\Item\Iface
282
	{
283
		$orderItem = \Aimeos\MShop::create( $this->getContext(), 'order' )->get( $orderid );
284
		$serviceItem = $this->manager->find( $code );
285
286
		$provider = $this->manager->getProvider( $serviceItem, $serviceItem->getType() );
287
288
289
		if( ( $orderItem = $provider->updateSync( $request, $orderItem ) ) !== null )
290
		{
291
			if( empty( $orderItem->getStatusPayment() )
292
				&& $provider->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY )
293
			) {
294
				$provider->query( $orderItem );
295
			}
296
		}
297
298
		return $orderItem;
299
	}
300
301
302
	/**
303
	 * Sets the referenced domains that will be fetched too when retrieving items
304
	 *
305
	 * @param array $domains Domain names of the referenced items that should be fetched too
306
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
307
	 * @since 2019.04
308
	 */
309
	public function uses( array $domains ) : Iface
310
	{
311
		$this->domains = $domains;
312
		return $this;
313
	}
314
315
316
	/**
317
	 * Returns the manager used by the controller
318
	 *
319
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object
320
	 */
321
	protected function getManager() : \Aimeos\MShop\Common\Manager\Iface
322
	{
323
		return $this->manager;
324
	}
325
}
326