Completed
Push — master ( 452e16...087a76 )
by Aimeos
02:19
created

Standard::getServiceItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Standard::getServicePrice() 0 11 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 $id Unique service ID
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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 $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
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, $ref = ['media', 'price', 'text'] )
66
	{
67
		$list = [];
68
		$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
69
70
		$search = $manager->createSearch( true );
71
		$expr = array(
72
			$search->getConditions(),
73
			$search->compare( '==', 'service.type.code', $type ),
74
			$search->compare( '==', 'service.type.domain', 'service' ),
75
		);
76
		$search->setConditions( $search->combine( '&&', $expr ) );
77
		$search->setSortations( array( $search->sort( '+', 'service.position' ) ) );
78
79
		foreach( $manager->searchItems( $search, $ref ) as $id => $item ) {
80
			$list[$id] = $manager->getProvider( $item );
81
		}
82
83
		return $list;
84
	}
85
86
87
	/**
88
	 * Returns the service items that are available for the service type and the content of the basket.
89
	 *
90
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
91
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket of the user
92
	 * @param array $ref List of domains for which the items referenced by the services should be fetched too
93
	 * @return array List of service items implementing \Aimeos\MShop\Service\Item\Iface with referenced items
94
	 * @throws \Exception If an error occurs
95
	 * @deprecated Use getProviders() instead
96
	 */
97
	public function getServices( $type, \Aimeos\MShop\Order\Item\Base\Iface $basket, $ref = ['media', 'price', 'text'] )
98
	{
99
		$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
100
101
		$search = $serviceManager->createSearch( true );
102
		$expr = array(
103
			$search->getConditions(),
104
			$search->compare( '==', 'service.type.domain', 'service' ),
105
			$search->compare( '==', 'service.type.code', $type ),
106
		);
107
		$search->setConditions( $search->combine( '&&', $expr ) );
108
		$search->setSortations( array( $search->sort( '+', 'service.position' ) ) );
109
110
		$items = $serviceManager->searchItems( $search, $ref );
111
112
113
		foreach( $items as $id => $service )
114
		{
115
			try
116
			{
117
				$provider = $serviceManager->getProvider( $service );
118
119
				if( $provider->isAvailable( $basket ) ) {
120
					$this->providers[$type][$id] = $provider;
121
				} else {
122
					unset( $items[$id] );
123
				}
124
			}
125
			catch( \Aimeos\MShop\Service\Exception $e )
126
			{
127
				$msg = sprintf( 'Unable to create provider "%1$s" for service with ID "%2$s"', $service->getCode(), $id );
128
				$this->getContext()->getLogger()->log( $msg, \Aimeos\MW\Logger\Base::WARN );
129
			}
130
		}
131
132
		return $items;
133
	}
134
135
136
	/**
137
	 * Returns the list of attribute definitions which must be used to render the input form where the customer can
138
	 * enter or chose the required data necessary by the service provider.
139
	 *
140
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
141
	 * @param string $serviceId Identifier of one of the service option returned by getService()
142
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
143
	 * @return array List of attribute definitions implementing \Aimeos\MW\Criteria\Attribute\Iface
144
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
145
	 * @throws \Aimeos\MShop\Exception If service provider isn't available
146
	 * @throws \Exception If an error occurs
147
	 * @deprecated Use getProvider() instead
148
	 */
149
	public function getServiceAttributes( $type, $serviceId, \Aimeos\MShop\Order\Item\Base\Iface $basket )
150
	{
151
		if( isset( $this->providers[$type][$serviceId] ) ) {
152
			return $this->providers[$type][$serviceId]->getConfigFE( $basket );
153
		}
154
155
		$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
156
		$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...
157
158
		return $serviceManager->getProvider( $item )->getConfigFE( $basket );
159
	}
160
161
162
	/**
163
	 * Returns the price of the service.
164
	 *
165
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
166
	 * @param string $serviceId Identifier of one of the service option returned by getService()
167
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket with products
168
	 * @return \Aimeos\MShop\Price\Item\Iface Price item
169
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
170
	 * @throws \Aimeos\MShop\Exception If service provider isn't available
171
	 * @throws \Exception If an error occurs
172
	 * @deprecated Use getProvider() instead
173
	 */
174
	public function getServicePrice( $type, $serviceId, \Aimeos\MShop\Order\Item\Base\Iface $basket )
175
	{
176
		if( isset( $this->providers[$type][$serviceId] ) ) {
177
			return $this->providers[$type][$serviceId]->calcPrice( $basket );
178
		}
179
180
		$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
181
		$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...
182
183
		return $serviceManager->getProvider( $item )->calcPrice( $basket );
184
	}
185
186
187
	/**
188
	 * Returns a list of attributes that are invalid.
189
	 *
190
	 * @param string $type Service type, e.g. "delivery" (shipping related) or "payment" (payment related)
191
	 * @param string $serviceId Identifier of the service option chosen by the customer
192
	 * @param array $attributes List of key/value pairs with name of the attribute from attribute definition object as
193
	 * 	key and the string entered by the customer as value
194
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
195
	 * 	known by the provider but aren't valid resp. null for attributes whose values are OK
196
	 * @throws \Aimeos\Controller\Frontend\Service\Exception If no active service provider for this ID is available
197
	 * @deprecated Use checkAttributes() instead
198
	 */
199
	public function checkServiceAttributes( $type, $serviceId, array $attributes )
200
	{
201
		if( !isset( $this->providers[$type][$serviceId] ) )
202
		{
203
			$serviceManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'service' );
204
			$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...
205
206
			$this->providers[$type][$serviceId] = $serviceManager->getProvider( $item );
207
		}
208
209
		$errors = $this->providers[$type][$serviceId]->checkConfigFE( $attributes );
210
211
		foreach( $errors as $key => $msg )
212
		{
213
			if( $msg === null ) {
214
				unset( $errors[$key] );
215
			}
216
		}
217
218
		return $errors;
219
	}
220
}
221