Completed
Push — master ( 87706b...b447e5 )
by Aimeos
02:08
created

Standard::updateSync()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 12
nc 3
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-2017
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 $providers = [];
0 ignored issues
show
Unused Code introduced by
The property $providers is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
29
30
31
	/**
32
	 * Returns a list of attributes that are invalid
33
	 *
34
	 * @param string $serviceId Unique service ID
35
	 * @param string[] $attributes List of attribute codes as keys and strings entered by the customer as value
36
	 * @return string[] List of attributes codes as keys and error messages as values for invalid or missing values
37
	 */
38
	public function checkAttributes( $serviceId, array $attributes )
39
	{
40
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
41
42
		$item = $manager->getItem( $serviceId, [], true );
43
		$provider = $manager->getProvider( $item, $item->getType() );
44
45
		return array_filter( $provider->checkConfigFE( $attributes ) );
46
	}
47
48
49
	/**
50
	 * Returns the service item for the given ID
51
	 *
52
	 * @param string $serviceId Unique service ID
53
	 * @param string[] $ref List of domain names whose items should be fetched too
54
	 * @return \Aimeos\MShop\Service\Provider\Iface Service provider object
55
	 */
56
	public function getProvider( $serviceId, $ref = ['media', 'price', 'text'] )
57
	{
58
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
59
		$item = $manager->getItem( $serviceId, $ref, true );
60
61
		return $manager->getProvider( $item, $item->getType() );
62
	}
63
64
65
	/**
66
	 * Returns the service providers of the given type
67
	 *
68
	 * @param string|null $type Service type, e.g. "delivery" (shipping related), "payment" (payment related) or null for all
69
	 * @param string[] $ref List of domain names whose items should be fetched too
70
	 * @return \Aimeos\MShop\Service\Provider\Iface[] List of service IDs as keys and service provider objects as values
71
	 */
72
	public function getProviders( $type = null, $ref = ['media', 'price', 'text'] )
73
	{
74
		$list = [];
75
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
76
77
		$search = $manager->createSearch( true );
78
		$search->setSortations( array( $search->sort( '+', 'service.position' ) ) );
79
80
		if( $type != null )
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $type of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
81
		{
82
			$expr = array(
83
				$search->getConditions(),
84
				$search->compare( '==', 'service.type.code', $type ),
85
				$search->compare( '==', 'service.type.domain', 'service' ),
86
			);
87
			$search->setConditions( $search->combine( '&&', $expr ) );
88
		}
89
90
		foreach( $manager->searchItems( $search, $ref ) as $id => $item ) {
91
			$list[$id] = $manager->getProvider( $item, $item->getType() );
92
		}
93
94
		return $list;
95
	}
96
97
98
	/**
99
	 * Processes the service for the given order, e.g. payment and delivery services
100
	 *
101
	 * @param \Aimeos\MShop\Order\Item\Iface $orderItem Order which should be processed
102
	 * @param string $serviceId Unique service item ID
103
	 * @param array $urls Associative list of keys and the corresponding URLs
104
	 * 	(keys are <type>.url-self, <type>.url-success, <type>.url-update where type can be "delivery" or "payment")
105
	 * @param array $params Request parameters and order service attributes
106
	 * @return \Aimeos\MShop\Common\Item\Helper\Form\Iface|null Form object with URL, parameters, etc.
107
	 * 	or null if no form data is required
108
	 */
109
	public function process( \Aimeos\MShop\Order\Item\Iface $orderItem, $serviceId, array $urls, array $params )
110
	{
111
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
112
		$item = $manager->getItem( $serviceId, [], true );
113
114
		$provider = $manager->getProvider( $item, $item->getType() );
115
		$provider->injectGlobalConfigBE( $urls );
116
117
		return $provider->process( $orderItem, $params );
118
	}
119
120
121
	/**
122
	 * Updates the order status sent by payment gateway notifications
123
	 *
124
	 * @param ServerRequestInterface $request Request object
125
	 * @param ResponseInterface $response Response object that will contain HTTP status and response body
126
	 * @param string $code Unique code of the service used for the current order
127
	 * @return \Psr\Http\Message\ResponseInterface Response object
128
	 */
129
	public function updatePush( ServerRequestInterface $request, ResponseInterface $response, $code )
130
	{
131
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
132
		$item = $manager->findItem( $code );
133
134
		$provider = $manager->getProvider( $item, $item->getType() );
135
136
		return $provider->updatePush( $request, $response );
137
	}
138
139
140
	/**
141
	 * Updates the payment or delivery status for the given request
142
	 *
143
	 * @param ServerRequestInterface $request Request object with parameters and request body
144
	 * @param string $code Unique code of the service used for the current order
145
	 * @param string $orderid ID of the order whose payment status should be updated
146
	 * @return \Aimeos\MShop\Order\Item\Iface $orderItem Order item that has been updated
147
	 */
148
	public function updateSync( ServerRequestInterface $request, $code, $orderid )
149
	{
150
		$context = $this->getContext();
151
		$orderManager = \Aimeos\MShop\Factory::createManager( $context, 'order' );
152
		$serviceManager = \Aimeos\MShop\Factory::createManager( $context, 'service' );
153
154
		$orderItem = $orderManager->getItem( $orderid );
155
		$serviceItem = $serviceManager->findItem( $code );
156
157
		$provider = $serviceManager->getProvider( $serviceItem, $serviceItem->getType() );
158
159
160
		if( ( $orderItem = $provider->updateSync( $request, $orderItem ) ) !== null )
161
		{
162
			if( $orderItem->getPaymentStatus() === \Aimeos\MShop\Order\Item\Base::PAY_UNFINISHED
163
				&& $provider->isImplemented( \Aimeos\MShop\Service\Provider\Payment\Base::FEAT_QUERY )
164
			) {
165
				$provider->query( $orderItem );
166
			}
167
		}
168
169
		return $orderItem;
170
	}
171
}
172