Passed
Push — master ( c9dfc7...a4ce90 )
by Aimeos
01:38
created

Standard::updatePush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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