Passed
Push — master ( fd781a...b1eaa4 )
by Aimeos
08:26
created

Standard::data()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 54
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 4
nop 3
dl 0
loc 54
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Checkout\Standard\Delivery;
13
14
15
// Strings for translation
16
sprintf( 'delivery' );
17
18
19
/**
20
 * Default implementation of checkout delivery HTML client.
21
 *
22
 * @package Client
23
 * @subpackage Html
24
 */
25
class Standard
26
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
27
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
28
{
29
	/** client/html/checkout/standard/delivery/subparts
30
	 * List of HTML sub-clients rendered within the checkout standard delivery section
31
	 *
32
	 * The output of the frontend is composed of the code generated by the HTML
33
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
34
	 * that are responsible for rendering certain sub-parts of the output. The
35
	 * sub-clients can contain HTML clients themselves and therefore a
36
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
37
	 * the output that is placed inside the container of its parent.
38
	 *
39
	 * At first, always the HTML code generated by the parent is printed, then
40
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
41
	 * determines the order of the output of these sub-clients inside the parent
42
	 * container. If the configured list of clients is
43
	 *
44
	 *  array( "subclient1", "subclient2" )
45
	 *
46
	 * you can easily change the order of the output by reordering the subparts:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
49
	 *
50
	 * You can also remove one or more parts if they shouldn't be rendered:
51
	 *
52
	 *  client/html/<clients>/subparts = array( "subclient1" )
53
	 *
54
	 * As the clients only generates structural HTML, the layout defined via CSS
55
	 * should support adding, removing or reordering content by a fluid like
56
	 * design.
57
	 *
58
	 * @param array List of sub-client names
59
	 * @since 2014.03
60
	 * @category Developer
61
	 */
62
	private $subPartPath = 'client/html/checkout/standard/delivery/subparts';
63
	private $subPartNames = [];
64
65
66
	/**
67
	 * Returns the HTML code for insertion into the body.
68
	 *
69
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
70
	 * @return string HTML code
71
	 */
72
	public function body( string $uid = '' ) : string
73
	{
74
		$view = $this->view();
75
		$step = $view->get( 'standardStepActive' );
76
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
77
78
		if( $step != 'delivery' && !( in_array( 'delivery', $onepage ) && in_array( $step, $onepage ) ) ) {
79
			return '';
80
		}
81
82
		$html = '';
83
		foreach( $this->getSubClients() as $subclient ) {
84
			$html .= $subclient->setView( $view )->body( $uid );
85
		}
86
		$view->deliveryBody = $html;
87
88
		/** client/html/checkout/standard/delivery/template-body
89
		 * Relative path to the HTML body template of the checkout standard delivery client.
90
		 *
91
		 * The template file contains the HTML code and processing instructions
92
		 * to generate the result shown in the body of the frontend. The
93
		 * configuration string is the path to the template file relative
94
		 * to the templates directory (usually in client/html/templates).
95
		 *
96
		 * You can overwrite the template file configuration in extensions and
97
		 * provide alternative templates. These alternative templates should be
98
		 * named like the default one but suffixed by
99
		 * an unique name. You may use the name of your project for this. If
100
		 * you've implemented an alternative client class as well, it
101
		 * should be suffixed by the name of the new class.
102
		 *
103
		 * @param string Relative path to the template creating code for the HTML page body
104
		 * @since 2014.03
105
		 * @category Developer
106
		 * @see client/html/checkout/standard/delivery/template-header
107
		 */
108
		$tplconf = 'client/html/checkout/standard/delivery/template-body';
109
		$default = 'checkout/standard/delivery-body';
110
111
		return $view->render( $view->config( $tplconf, $default ) );
112
	}
113
114
115
	/**
116
	 * Returns the sub-client given by its name.
117
	 *
118
	 * @param string $type Name of the client type
119
	 * @param string|null $name Name of the sub-client (Default if null)
120
	 * @return \Aimeos\Client\Html\Iface Sub-client object
121
	 */
122
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
123
	{
124
		/** client/html/checkout/standard/delivery/decorators/excludes
125
		 * Excludes decorators added by the "common" option from the checkout standard delivery html client
126
		 *
127
		 * Decorators extend the functionality of a class by adding new aspects
128
		 * (e.g. log what is currently done), executing the methods of the underlying
129
		 * class only in certain conditions (e.g. only for logged in users) or
130
		 * modify what is returned to the caller.
131
		 *
132
		 * This option allows you to remove a decorator added via
133
		 * "client/html/common/decorators/default" before they are wrapped
134
		 * around the html client.
135
		 *
136
		 *  client/html/checkout/standard/delivery/decorators/excludes = array( 'decorator1' )
137
		 *
138
		 * This would remove the decorator named "decorator1" from the list of
139
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
140
		 * "client/html/common/decorators/default" to the html client.
141
		 *
142
		 * @param array List of decorator names
143
		 * @since 2015.08
144
		 * @category Developer
145
		 * @see client/html/common/decorators/default
146
		 * @see client/html/checkout/standard/delivery/decorators/global
147
		 * @see client/html/checkout/standard/delivery/decorators/local
148
		 */
149
150
		/** client/html/checkout/standard/delivery/decorators/global
151
		 * Adds a list of globally available decorators only to the checkout standard delivery html client
152
		 *
153
		 * Decorators extend the functionality of a class by adding new aspects
154
		 * (e.g. log what is currently done), executing the methods of the underlying
155
		 * class only in certain conditions (e.g. only for logged in users) or
156
		 * modify what is returned to the caller.
157
		 *
158
		 * This option allows you to wrap global decorators
159
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
160
		 *
161
		 *  client/html/checkout/standard/delivery/decorators/global = array( 'decorator1' )
162
		 *
163
		 * This would add the decorator named "decorator1" defined by
164
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
165
		 *
166
		 * @param array List of decorator names
167
		 * @since 2015.08
168
		 * @category Developer
169
		 * @see client/html/common/decorators/default
170
		 * @see client/html/checkout/standard/delivery/decorators/excludes
171
		 * @see client/html/checkout/standard/delivery/decorators/local
172
		 */
173
174
		/** client/html/checkout/standard/delivery/decorators/local
175
		 * Adds a list of local decorators only to the checkout standard delivery html client
176
		 *
177
		 * Decorators extend the functionality of a class by adding new aspects
178
		 * (e.g. log what is currently done), executing the methods of the underlying
179
		 * class only in certain conditions (e.g. only for logged in users) or
180
		 * modify what is returned to the caller.
181
		 *
182
		 * This option allows you to wrap local decorators
183
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
184
		 *
185
		 *  client/html/checkout/standard/delivery/decorators/local = array( 'decorator2' )
186
		 *
187
		 * This would add the decorator named "decorator2" defined by
188
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
189
		 *
190
		 * @param array List of decorator names
191
		 * @since 2015.08
192
		 * @category Developer
193
		 * @see client/html/common/decorators/default
194
		 * @see client/html/checkout/standard/delivery/decorators/excludes
195
		 * @see client/html/checkout/standard/delivery/decorators/global
196
		 */
197
198
		return $this->createSubClient( 'checkout/standard/delivery/' . $type, $name );
199
	}
200
201
202
	/**
203
	 * Processes the input, e.g. store given values.
204
	 *
205
	 * A view must be available and this method doesn't generate any output
206
	 * besides setting view variables.
207
	 */
208
	public function init()
209
	{
210
		$view = $this->view();
211
		$context = $this->context();
212
213
		try
214
		{
215
			$basketCtrl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
216
			$servCtrl = \Aimeos\Controller\Frontend::create( $context, 'service' )->uses( ['media', 'price', 'text'] );
217
218
			// only start if there's something to do
219
			if( ( $serviceIds = $view->param( 'c_deliveryoption', null ) ) !== null )
220
			{
221
				$basketCtrl->deleteService( 'delivery' );
222
223
				foreach( (array) $serviceIds as $idx => $id )
224
				{
225
					try
226
					{
227
						$basketCtrl->addService( $servCtrl->get( $id ), $view->param( 'c_delivery/' . $id, [] ), $idx );
228
					}
229
					catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
230
					{
231
						$view->deliveryError = $e->getErrors();
232
						$view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $e->getErrors() );
233
234
						throw $e;
235
					}
236
				}
237
			}
238
239
240
			parent::init();
241
242
243
			if( !isset( $view->standardStepActive ) && !$this->call( 'isAvailable', $basketCtrl->get() ) ) {
244
				$view->standardStepActive = 'delivery';
245
			}
246
		}
247
		catch( \Exception $e )
248
		{
249
			$view->standardStepActive = 'delivery';
250
			throw $e;
251
		}
252
	}
253
254
255
	/**
256
	 * Returns the list of sub-client names configured for the client.
257
	 *
258
	 * @return array List of HTML client names
259
	 */
260
	protected function getSubClientNames() : array
261
	{
262
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
263
	}
264
265
266
	/**
267
	 * Sets the necessary parameter values in the view.
268
	 *
269
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
270
	 * @param array &$tags Result array for the list of tags that are associated to the output
271
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
272
	 * @return \Aimeos\MW\View\Iface Modified view object
273
	 */
274
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
275
	{
276
		$context = $this->context();
277
		$domains = ['media', 'price', 'text'];
278
279
		/** client/html/checkout/standard/delivery/domains
280
		 * List of domain names whose items should be available in the checkout payment templates
281
		 *
282
		 * The templates rendering checkout delivery related data usually add the
283
		 * images, prices and texts associated to each item. If you want to display
284
		 * additional content like the attributes, you can configure your own list
285
		 * of domains (attribute, media, price, text, etc. are domains) whose items
286
		 * are fetched from the storage.
287
		 *
288
		 * @param array List of domain names
289
		 * @since 2019.04
290
		 * @category Developer
291
		 * @see client/html/checkout/standard/payment/domains
292
		 */
293
		$domains = $context->config()->get( 'client/html/checkout/standard/delivery/domains', $domains );
294
295
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
296
		$serviceCntl = \Aimeos\Controller\Frontend::create( $context, 'service' );
297
298
		$services = [];
299
		$basket = $basketCntl->get();
300
		$providers = $serviceCntl->uses( $domains )->type( 'delivery' )->getProviders();
301
		$orderServices = map( $basket->getService( 'delivery' ) )->col( null, 'order.base.service.serviceid' );
302
303
		foreach( $providers as $id => $provider )
304
		{
305
			if( $provider->isAvailable( $basket ) === true )
306
			{
307
				$attr = $provider->getConfigFE( $basket );
308
309
				if( $oservice = $orderServices->get( $id ) )
310
				{
311
					foreach( $attr as $key => $item )
312
					{
313
						$value = is_array( $item->getDefault() ) ? key( $item->getDefault() ) : $item->getDefault();
314
						$value = $oservice->getAttribute( $key, 'delivery' ) ?: $value;
315
						$item->value = $oservice->getAttribute( $key . '/hidden', 'delivery' ) ?: $value;
316
					}
317
				}
318
319
				$services[$id] = $provider->getServiceItem()->set( 'attributes', $attr )
320
					->set( 'price', $provider->calcPrice( $basket ) );
321
			}
322
		}
323
324
		$view->deliveryServices = $services;
325
		$view->deliveryOption = $view->param( 'c_deliveryoption', $orderServices->firstKey() ?: key( $services ) );
326
327
		return parent::data( $view, $tags, $expire );
328
	}
329
330
331
	/**
332
	 * Tests if an item is available and the step can be skipped
333
	 *
334
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
335
	 * @return bool TRUE if step can be skipped, FALSE if not
336
	 */
337
	protected function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : bool
338
	{
339
		return !empty( $basket->getService( 'delivery' ) );
340
	}
341
}
342