Completed
Push — master ( 10ff4e...12c65b )
by Aimeos
02:25
created

Standard::getProviders()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
nc 4
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-2017
7
 * @package Controller
8
 * @subpackage Frontend
9
 */
10
11
12
namespace Aimeos\Controller\Frontend\Service;
13
14
15
/**
16
 * Default implementation of the service frontend controller.
17
 *
18
 * @package Controller
19
 * @subpackage Frontend
20
 */
21
class Standard
22
	extends \Aimeos\Controller\Frontend\Base
23
	implements Iface, \Aimeos\Controller\Frontend\Common\Iface
24
{
25
	private $providers = array();
26
27
28
	/**
29
	 * Returns a list of attributes that are invalid
30
	 *
31
	 * @param string $serviceId Unique service ID
32
	 * @param string[] $attributes List of attribute codes as keys and strings entered by the customer as value
33
	 * @return string[] List of attributes codes as keys and error messages as values for invalid or missing values
34
	 */
35
	public function checkAttributes( $serviceId, array $attributes )
36
	{
37
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
38
		$provider = $manager->getProvider( $manager->getItem( $serviceId, [], true ) );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
40
		return array_filter( $provider->checkConfigFE( $attributes ) );
41
	}
42
43
44
	/**
45
	 * Returns the service item for the given ID
46
	 *
47
	 * @param string $serviceId Unique service ID
48
	 * @param string[] $ref List of domain names whose items should be fetched too
49
	 * @return \Aimeos\MShop\Service\Provider\Iface Service provider object
50
	 */
51
	public function getProvider( $serviceId, $ref = ['media', 'price', 'text'] )
52
	{
53
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
54
		return $manager->getProvider( $manager->getItem( $serviceId, $ref, true ) );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
55
	}
56
57
58
	/**
59
	 * Returns the service providers of the given type
60
	 *
61
	 * @param string|null $type Service type, e.g. "delivery" (shipping related), "payment" (payment related) or null for all
62
	 * @param string[] $ref List of domain names whose items should be fetched too
63
	 * @return \Aimeos\MShop\Service\Provider\Iface[] List of service IDs as keys and service provider objects as values
64
	 */
65
	public function getProviders( $type = null, $ref = ['media', 'price', 'text'] )
66
	{
67
		$list = [];
68
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
69
70
		$search = $manager->createSearch( true );
71
		$search->setSortations( array( $search->sort( '+', 'service.position' ) ) );
72
73
		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...
74
		{
75
			$expr = array(
76
				$search->getConditions(),
77
				$search->compare( '==', 'service.type.code', $type ),
78
				$search->compare( '==', 'service.type.domain', 'service' ),
79
			);
80
			$search->setConditions( $search->combine( '&&', $expr ) );
81
		}
82
83
		foreach( $manager->searchItems( $search, $ref ) as $id => $item ) {
84
			$list[$id] = $manager->getProvider( $item );
85
		}
86
87
		return $list;
88
	}
89
90
91
	/**
92
	 * Returns the service items that are available for the service type and the content of the basket.
93
	 *
94
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
95
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket of the user
96
	 * @param array $ref List of domains for which the items referenced by the services should be fetched too
97
	 * @return array List of service items implementing \Aimeos\MShop\Service\Item\Iface with referenced items
98
	 * @throws \Exception If an error occurs
99
	 * @deprecated Use getProviders() instead
100
	 */
101
	public function getServices( $type, \Aimeos\MShop\Order\Item\Base\Iface $basket, $ref = ['media', 'price', 'text'] )
102
	{
103
		$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
104
105
		$search = $serviceManager->createSearch( true );
106
		$expr = array(
107
			$search->getConditions(),
108
			$search->compare( '==', 'service.type.domain', 'service' ),
109
			$search->compare( '==', 'service.type.code', $type ),
110
		);
111
		$search->setConditions( $search->combine( '&&', $expr ) );
112
		$search->setSortations( array( $search->sort( '+', 'service.position' ) ) );
113
114
		$items = $serviceManager->searchItems( $search, $ref );
115
116
117
		foreach( $items as $id => $service )
118
		{
119
			try
120
			{
121
				$provider = $serviceManager->getProvider( $service );
122
123
				if( $provider->isAvailable( $basket ) ) {
124
					$this->providers[$type][$id] = $provider;
125
				} else {
126
					unset( $items[$id] );
127
				}
128
			}
129
			catch( \Aimeos\MShop\Service\Exception $e )
130
			{
131
				$msg = sprintf( 'Unable to create provider "%1$s" for service with ID "%2$s"', $service->getCode(), $id );
132
				$this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::WARN );
133
			}
134
		}
135
136
		return $items;
137
	}
138
139
140
	/**
141
	 * Returns the list of attribute definitions which must be used to render the input form where the customer can
142
	 * enter or chose the required data necessary by the service provider.
143
	 *
144
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
145
	 * @param string $serviceId Identifier of one of the service option returned by getService()
146
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
147
	 * @return array List of attribute definitions implementing \Aimeos\MW\Criteria\Attribute\Iface
148
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
149
	 * @throws \Aimeos\MShop\Exception If service provider isn't available
150
	 * @throws \Exception If an error occurs
151
	 * @deprecated Use getProvider() instead
152
	 */
153
	public function getServiceAttributes( $type, $serviceId, \Aimeos\MShop\Order\Item\Base\Iface $basket )
154
	{
155
		if( isset( $this->providers[$type][$serviceId] ) ) {
156
			return $this->providers[$type][$serviceId]->getConfigFE( $basket );
157
		}
158
159
		$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
160
		$item = $serviceManager->getItem( $serviceId, ['price'], true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
161
162
		return $serviceManager->getProvider( $item )->getConfigFE( $basket );
163
	}
164
165
166
	/**
167
	 * Returns the price of the service.
168
	 *
169
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
170
	 * @param string $serviceId Identifier of one of the service option returned by getService()
171
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket with products
172
	 * @return \Aimeos\MShop\Price\Item\Iface Price item
173
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
174
	 * @throws \Aimeos\MShop\Exception If service provider isn't available
175
	 * @throws \Exception If an error occurs
176
	 * @deprecated Use getProvider() instead
177
	 */
178
	public function getServicePrice( $type, $serviceId, \Aimeos\MShop\Order\Item\Base\Iface $basket )
179
	{
180
		if( isset( $this->providers[$type][$serviceId] ) ) {
181
			return $this->providers[$type][$serviceId]->calcPrice( $basket );
182
		}
183
184
		$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
185
		$item = $serviceManager->getItem( $serviceId, ['price'], true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
186
187
		return $serviceManager->getProvider( $item )->calcPrice( $basket );
188
	}
189
190
191
	/**
192
	 * Returns a list of attributes that are invalid.
193
	 *
194
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
195
	 * @param string $serviceId Identifier of the service option chosen by the customer
196
	 * @param array $attributes List of key/value pairs with name of the attribute from attribute definition object as
197
	 * 	key and the string entered by the customer as value
198
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
199
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
200
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
201
	 * @deprecated Use checkAttributes() instead
202
	 */
203
	public function checkServiceAttributes( $type, $serviceId, array $attributes )
204
	{
205
		if( !isset( $this->providers[$type][$serviceId] ) )
206
		{
207
			$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
208
			$item = $serviceManager->getItem( $serviceId, ['price'], true );
0 ignored issues
show
Unused Code introduced by
The call to Iface::getItem() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
209
210
			$this->providers[$type][$serviceId] = $serviceManager->getProvider( $item );
211
		}
212
213
		$errors = $this->providers[$type][$serviceId]->checkConfigFE( $attributes );
214
215
		foreach( $errors as $key => $msg )
216
		{
217
			if( $msg === null ) {
218
				unset( $errors[$key] );
219
			}
220
		}
221
222
		return $errors;
223
	}
224
}
225