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

Standard::function()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
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-2018
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 $conditions = [];
29
	private $domains = [];
30
	private $filter;
31
	private $manager;
32
33
34
	/**
35
	 * Common initialization for controller classes
36
	 *
37
	 * @param \Aimeos\MShop\Context\Item\Iface $context Common MShop context object
38
	 */
39
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
40
	{
41
		parent::__construct( $context );
42
43
		$this->manager = \Aimeos\MShop::create( $context, 'service' );
44
		$this->filter = $this->manager->createSearch( true );
45
		$this->conditions[] = $this->filter->getConditions();
46
		$this->filter->setSortations( [$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->conditions[] = $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->findItem( $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
	 * @param string Search function string that can be used in compare()
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Frontend\Service\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...
85
	 */
86
	public function function( string $name, array $params ) : string
87
	{
88
		return $this->filter->createFunction( $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->getItem( $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->getItem( $serviceId, $this->domains, true );
114
		return $this->manager->getProvider( $item, $item->getType() );
1 ignored issue
show
Bug introduced by
The method getType() does not exist on Aimeos\MShop\Common\Item\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MAdmin\Log\Item\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MAdmin\Cache\Item\Iface or Aimeos\MShop\Common\Item\Status\Iface or Aimeos\MShop\Customer\Item\Group\Iface or Aimeos\MShop\Order\Item\Base\Coupon\Iface or Aimeos\MShop\Subscription\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MShop\Common\Item\ListRef\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Common\Item\PropertyRef\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Order\Item\Base\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Common\Item\Address\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Common\Item\AddressRef\Iface or Aimeos\MShop\Coupon\Item\Code\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Coupon\Item\Iface or Aimeos\MAdmin\Job\Item\Iface or Aimeos\MShop\Locale\Item\Iface or Aimeos\MShop\Common\Item\Type\Iface or Aimeos\MShop\Locale\Item\Language\Iface or Aimeos\MShop\Common\Item\Tree\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Locale\Item\Currency\Iface or Aimeos\MShop\Locale\Item\Site\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Catalog\Item\Iface or Aimeos\MShop\Supplier\Item\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Order\Item\Base\Base or Aimeos\MShop\Order\Item\Base\Standard or Aimeos\MShop\Customer\Item\Address\Iface or Aimeos\MShop\Supplier\Item\Address\Iface or Aimeos\MShop\Customer\Item\Iface or Aimeos\MShop\Supplier\Item\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
		return $this->manager->getProvider( $item, $item->/** @scrutinizer ignore-call */ getType() );
Loading history...
115
	}
116
117
118
	/**
119
	 * Returns the service providers of the given type
120
	 *
121
	 * @return \Aimeos\MShop\Service\Provider\Iface[] List of service IDs as keys and service provider objects as values
122
	 */
123
	public function getProviders()
124
	{
125
		$list = [];
126
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
127
128
		foreach( $this->manager->searchItems( $this->filter, $this->domains ) as $id => $item ) {
129
			$list[$id] = $this->manager->getProvider( $item, $item->getType() );
130
		}
131
132
		return $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->toConditions( $conditions ) ) !== null ) {
146
			$this->conditions[] = $cond;
147
		}
148
149
		return $this;
150
	}
151
152
153
	/**
154
	 * Processes the service for the given order, e.g. payment and delivery services
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 <type>.url-self, <type>.url-success, <type>.url-update where type can be "delivery" or "payment")
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->getItem( $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\MShop\Service\Item\Iface[] Ordered list of service items
181
	 * @since 2019.04
182
	 */
183
	public function search( int &$total = null )
184
	{
185
		$this->filter->setConditions( $this->filter->combine( '&&', $this->conditions ) );
186
		return $this->manager->searchItems( $this->filter, $this->domains, $total );
187
	}
188
189
190
	/**
191
	 * Sets the start value and the number of returned services for slicing the list of found services
192
	 *
193
	 * @param int $start Start value of the first attribute in the list
194
	 * @param int $limit Number of returned services
195
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
196
	 * @since 2019.04
197
	 */
198
	public function slice( int $start, int $limit ) : Iface
199
	{
200
		$this->filter->setSlice( $start, $limit );
201
		return $this;
202
	}
203
204
205
	/**
206
	 * Sets the sorting of the result list
207
	 *
208
	 * @param string|null $key Sorting of the result list like "position", null for no sorting
209
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
210
	 * @since 2019.04
211
	 */
212
	public function sort( string $key = null ) : Iface
213
	{
214
		$sort = [];
215
		$list = ( $key ? explode( ',', $key ) : [] );
216
217
		foreach( $list as $sortkey )
218
		{
219
			$direction = ( $sortkey[0] === '-' ? '-' : '+' );
220
			$sortkey = ltrim( $sortkey, '+-' );
221
222
			switch( $sortkey )
223
			{
224
				case 'type':
225
					$sort[] = $this->filter->sort( $direction, 'service.type' );
226
					break;
227
228
				default:
229
					$sort[] = $this->filter->sort( $direction, $sortkey );
230
			}
231
		}
232
233
		$this->filter->setSortations( $sort );
234
		return $this;
235
	}
236
237
238
	/**
239
	 * Adds attribute types for filtering
240
	 *
241
	 * @param array|string $code Service type or list of types
242
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
243
	 * @since 2019.04
244
	 */
245
	public function type( $code ) : Iface
246
	{
247
		if( $code ) {
248
			$this->conditions[] = $this->filter->compare( '==', 'service.type', $code );
249
		}
250
251
		return $this;
252
	}
253
254
255
	/**
256
	 * Updates the order status sent by payment gateway notifications
257
	 *
258
	 * @param ServerRequestInterface $request Request object
259
	 * @param ResponseInterface $response Response object that will contain HTTP status and response body
260
	 * @param string $code Unique code of the service used for the current order
261
	 * @return \Psr\Http\Message\ResponseInterface Response object
262
	 */
263
	public function updatePush( ServerRequestInterface $request, ResponseInterface $response,
264
		string $code ) : \Psr\Http\Message\ResponseInterface
265
	{
266
		$item = $this->manager->findItem( $code );
267
		$provider = $this->manager->getProvider( $item, $item->getType() );
268
269
		return $provider->updatePush( $request, $response );
270
	}
271
272
273
	/**
274
	 * Updates the payment or delivery status for the given request
275
	 *
276
	 * @param ServerRequestInterface $request Request object with parameters and request body
277
	 * @param string $code Unique code of the service used for the current order
278
	 * @param string $orderid ID of the order whose payment status should be updated
279
	 * @return \Aimeos\MShop\Order\Item\Iface $orderItem Order item that has been updated
280
	 */
281
	public function updateSync( ServerRequestInterface $request,
282
		string $code, string $orderid ) : \Aimeos\MShop\Order\Item\Iface
283
	{
284
		$orderItem = \Aimeos\MShop::create( $this->getContext(), 'order' )->getItem( $orderid );
285
		$serviceItem = $this->manager->findItem( $code );
286
287
		$provider = $this->manager->getProvider( $serviceItem, $serviceItem->getType() );
288
289
290
		if( ( $orderItem = $provider->updateSync( $request, $orderItem ) ) !== null )
291
		{
292
			if( $orderItem->getPaymentStatus() === \Aimeos\MShop\Order\Item\Base::PAY_UNFINISHED
293
				&& $provider->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY )
294
			) {
295
				$provider->query( $orderItem );
296
			}
297
		}
298
299
		return $orderItem;
300
	}
301
302
303
	/**
304
	 * Sets the referenced domains that will be fetched too when retrieving items
305
	 *
306
	 * @param array $domains Domain names of the referenced items that should be fetched too
307
	 * @return \Aimeos\Controller\Frontend\Service\Iface Service controller for fluent interface
308
	 * @since 2019.04
309
	 */
310
	public function uses( array $domains ) : Iface
311
	{
312
		$this->domains = $domains;
313
		return $this;
314
	}
315
}
316