Passed
Push — master ( 56acc7...6ec9fa )
by Aimeos
02:41
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 74
rs 10
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;
13
14
15
/**
16
 * Default implementation of standard checkout HTML client.
17
 *
18
 * @package Client
19
 * @subpackage Html
20
 */
21
class Standard
22
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
23
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
24
{
25
	/** client/html/checkout/standard/subparts
26
	 * List of HTML sub-clients rendered within the checkout standard section
27
	 *
28
	 * The output of the frontend is composed of the code generated by the HTML
29
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
30
	 * that are responsible for rendering certain sub-parts of the output. The
31
	 * sub-clients can contain HTML clients themselves and therefore a
32
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
33
	 * the output that is placed inside the container of its parent.
34
	 *
35
	 * At first, always the HTML code generated by the parent is printed, then
36
	 * the HTML code of its sub-clients. The order of the HTML sub-clients
37
	 * determines the order of the output of these sub-clients inside the parent
38
	 * container. If the configured list of clients is
39
	 *
40
	 *  array( "subclient1", "subclient2" )
41
	 *
42
	 * you can easily change the order of the output by reordering the subparts:
43
	 *
44
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
45
	 *
46
	 * You can also remove one or more parts if they shouldn't be rendered:
47
	 *
48
	 *  client/html/<clients>/subparts = array( "subclient1" )
49
	 *
50
	 * As the clients only generates structural HTML, the layout defined via CSS
51
	 * should support adding, removing or reordering content by a fluid like
52
	 * design.
53
	 *
54
	 * @param array List of sub-client names
55
	 * @since 2014.03
56
	 */
57
	private $subPartPath = 'client/html/checkout/standard/subparts';
58
59
	/** client/html/checkout/standard/address/name
60
	 * Name of the address part used by the checkout standard client implementation
61
	 *
62
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Checkout\Standard\Address\Myname".
63
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
64
	 *
65
	 * @param string Last part of the client class name
66
	 * @since 2014.03
67
	 */
68
69
	/** client/html/checkout/standard/delivery/name
70
	 * Name of the delivery part used by the checkout standard client implementation
71
	 *
72
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Checkout\Standard\Delivery\Myname".
73
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
74
	 *
75
	 * @param string Last part of the client class name
76
	 * @since 2014.03
77
	 */
78
79
	/** client/html/checkout/standard/payment/name
80
	 * Name of the payment part used by the checkout standard client implementation
81
	 *
82
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Checkout\Standard\Payment\Myname".
83
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
84
	 *
85
	 * @param string Last part of the client class name
86
	 * @since 2014.03
87
	 */
88
89
	/** client/html/checkout/standard/summary/name
90
	 * Name of the summary part used by the checkout standard client implementation
91
	 *
92
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Checkout\Standard\Summary\Myname".
93
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
94
	 *
95
	 * @param string Last part of the client class name
96
	 * @since 2014.03
97
	 */
98
99
	/** client/html/checkout/standard/process/name
100
	 * Name of the process part used by the checkout standard client implementation
101
	 *
102
	 * Use "Myname" if your class is named "\Aimeos\Client\Html\Checkout\Standard\Process\Myname".
103
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
104
	 *
105
	 * @param string Last part of the client class name
106
	 * @since 2015.07
107
	 */
108
	private $subPartNames = ['address', 'delivery', 'payment', 'summary', 'process'];
109
110
111
	/**
112
	 * Returns the sub-client given by its name.
113
	 *
114
	 * @param string $type Name of the client type
115
	 * @param string|null $name Name of the sub-client (Default if null)
116
	 * @return \Aimeos\Client\Html\Iface Sub-client object
117
	 */
118
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
119
	{
120
		/** client/html/checkout/standard/decorators/excludes
121
		 * Excludes decorators added by the "common" option from the checkout standard html client
122
		 *
123
		 * Decorators extend the functionality of a class by adding new aspects
124
		 * (e.g. log what is currently done), executing the methods of the underlying
125
		 * class only in certain conditions (e.g. only for logged in users) or
126
		 * modify what is returned to the caller.
127
		 *
128
		 * This option allows you to remove a decorator added via
129
		 * "client/html/common/decorators/default" before they are wrapped
130
		 * around the html client.
131
		 *
132
		 *  client/html/checkout/standard/decorators/excludes = array( 'decorator1' )
133
		 *
134
		 * This would remove the decorator named "decorator1" from the list of
135
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
136
		 * "client/html/common/decorators/default" to the html client.
137
		 *
138
		 * @param array List of decorator names
139
		 * @since 2014.05
140
		 * @see client/html/common/decorators/default
141
		 * @see client/html/checkout/standard/decorators/global
142
		 * @see client/html/checkout/standard/decorators/local
143
		 */
144
145
		/** client/html/checkout/standard/decorators/global
146
		 * Adds a list of globally available decorators only to the checkout standard html client
147
		 *
148
		 * Decorators extend the functionality of a class by adding new aspects
149
		 * (e.g. log what is currently done), executing the methods of the underlying
150
		 * class only in certain conditions (e.g. only for logged in users) or
151
		 * modify what is returned to the caller.
152
		 *
153
		 * This option allows you to wrap global decorators
154
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
155
		 *
156
		 *  client/html/checkout/standard/decorators/global = array( 'decorator1' )
157
		 *
158
		 * This would add the decorator named "decorator1" defined by
159
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
160
		 *
161
		 * @param array List of decorator names
162
		 * @since 2014.05
163
		 * @see client/html/common/decorators/default
164
		 * @see client/html/checkout/standard/decorators/excludes
165
		 * @see client/html/checkout/standard/decorators/local
166
		 */
167
168
		/** client/html/checkout/standard/decorators/local
169
		 * Adds a list of local decorators only to the checkout standard html client
170
		 *
171
		 * Decorators extend the functionality of a class by adding new aspects
172
		 * (e.g. log what is currently done), executing the methods of the underlying
173
		 * class only in certain conditions (e.g. only for logged in users) or
174
		 * modify what is returned to the caller.
175
		 *
176
		 * This option allows you to wrap local decorators
177
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
178
		 *
179
		 *  client/html/checkout/standard/decorators/local = array( 'decorator2' )
180
		 *
181
		 * This would add the decorator named "decorator2" defined by
182
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
183
		 *
184
		 * @param array List of decorator names
185
		 * @since 2014.05
186
		 * @see client/html/common/decorators/default
187
		 * @see client/html/checkout/standard/decorators/excludes
188
		 * @see client/html/checkout/standard/decorators/global
189
		 */
190
191
		return $this->createSubClient( 'checkout/standard/' . $type, $name );
192
	}
193
194
195
	/**
196
	 * Returns the list of sub-client names configured for the client.
197
	 *
198
	 * @return array List of HTML client names
199
	 */
200
	protected function getSubClientNames() : array
201
	{
202
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
203
	}
204
205
206
	/**
207
	 * Sets the necessary parameter values in the view.
208
	 *
209
	 * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output
210
	 * @param array &$tags Result array for the list of tags that are associated to the output
211
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
212
	 * @return \Aimeos\Base\View\Iface Modified view object
213
	 */
214
	public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\Base\View\Iface
215
	{
216
		$context = $this->context();
217
218
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
219
		$view->standardBasket = $basketCntl->get();
220
221
222
		/** client/html/checkout/standard/url/step-active
223
		 * Name of the checkout process step to jump to if no previous step requires attention
224
		 *
225
		 * The checkout process consists of several steps which are usually
226
		 * displayed one by another to the customer. If the data of a step
227
		 * is already available, then that step is skipped. The active step
228
		 * is the one that is displayed if all other steps are skipped.
229
		 *
230
		 * If one of the previous steps misses some data the customer has
231
		 * to enter, then this step is displayed first. After providing
232
		 * the missing data, the whole series of steps are tested again
233
		 * and if no other step requests attention, the configured active
234
		 * step will be displayed.
235
		 *
236
		 * The order of the steps is determined by the order of sub-parts
237
		 * that are configured for the checkout client.
238
		 *
239
		 * @param string Name of the confirm standard HTML client
240
		 * @since 2014.07
241
		 * @see client/html/checkout/subparts
242
		 */
243
		$default = $view->config( 'client/html/checkout/standard/url/step-active', 'summary' );
244
245
		/** client/html/checkout/standard/onepage
246
		 * Shows all named checkout subparts at once for a one page checkout
247
		 *
248
		 * Normally, the checkout process is divided into several steps for entering
249
		 * addresses, select delivery and payment options as well as showing the
250
		 * summary page. This enables dependencies between two steps like showing
251
		 * delivery options based on the address entered by the customer. Furthermore,
252
		 * this is good way to limit the amount of information displayed which is
253
		 * preferred by mobile users.
254
		 *
255
		 * Contrary to that, a one page checkout displays all information on only
256
		 * one page and customers get an immediate overview of which information
257
		 * they have to enter and what options they can select from. This is an
258
		 * advantage if only a very limited amount of information must be entered
259
		 * or if there are almost no options to choose from and no dependencies
260
		 * between exist.
261
		 *
262
		 * Using this config options, shop developers are able to define which
263
		 * checkout subparts are combined to a one page view. Simply add the names
264
		 * of all checkout subparts to the list. Available checkout subparts for
265
		 * a one page checkout are:
266
		 *
267
		 * * address
268
		 * * delivery
269
		 * * payment
270
		 * * summary
271
		 *
272
		 * @param array List of checkout subparts name
273
		 * @since 2015.05
274
		 */
275
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
276
		$onestep = ( !empty( $onepage ) ? array_shift( $onepage ) : $default ); // keep the first one page step
277
278
		$steps = (array) $context->config()->get( $this->subPartPath, $this->subPartNames );
279
		$steps = array_values( array_diff( $steps, $onepage ) ); // remove all remaining steps in $onepage and reindex
280
281
		// use first step if default step isn't available
282
		$default = ( !in_array( $default, $steps ) ? reset( $steps ) : $default );
283
		$current = $view->param( 'c_step', $default );
284
285
		// use $onestep if the current step isn't available due to one page layout
286
		if( !in_array( $current, $steps ) ) {
287
			$current = $onestep;
288
		}
289
290
		// use $onestep if the active step isn't available due to one page layout
291
		if( isset( $view->standardStepActive ) && in_array( $view->standardStepActive, $onepage ) ) {
292
			$view->standardStepActive = $onestep;
293
		}
294
295
		$cpos = array_search( $current, $steps );
296
297
		if( !isset( $view->standardStepActive )
298
			|| ( ( $apos = array_search( $view->standardStepActive, $steps ) ) !== false
299
			&& $cpos !== false && $cpos < $apos )
300
		) {
301
			$view->standardStepActive = $current;
302
		}
303
304
		$cpos = (int) array_search( $view->standardStepActive, array_values( $steps ) );
305
306
		$view->standardStepsBefore = array_slice( $steps, 0, $cpos );
307
		$view->standardStepsAfter = array_slice( $steps, $cpos + 1 );
308
309
		$view = $this->navigation( $view, $steps, $view->standardStepActive );
310
		$view->standardSteps = $steps;
311
312
		return parent::data( $view, $tags, $expire );
313
	}
314
315
316
	/**
317
	 * Adds the "back" and "next" URLs to the view
318
	 *
319
	 * @param \Aimeos\Base\View\Iface $view View object
320
	 * @param array $steps List of checkout step names
321
	 * @param string $activeStep Name of the active step
322
	 * @return \Aimeos\Base\View\Iface Enhanced view object
323
	 * @since 2016.05
324
	 */
325
	protected function navigation( \Aimeos\Base\View\Iface $view, array $steps, string $activeStep ) : \Aimeos\Base\View\Iface
326
	{
327
		/** client/html/checkout/standard/url/target
328
		 * Destination of the URL where the controller specified in the URL is known
329
		 *
330
		 * The destination can be a page ID like in a content management system or the
331
		 * module of a software development framework. This "target" must contain or know
332
		 * the controller that should be called by the generated URL.
333
		 *
334
		 * @param string Destination of the URL
335
		 * @since 2014.03
336
		 * @see client/html/checkout/standard/url/controller
337
		 * @see client/html/checkout/standard/url/action
338
		 * @see client/html/checkout/standard/url/config
339
		 */
340
341
		/** client/html/checkout/standard/url/controller
342
		 * Name of the controller whose action should be called
343
		 *
344
		 * In Model-View-Controller (MVC) applications, the controller contains the methods
345
		 * that create parts of the output displayed in the generated HTML page. Controller
346
		 * names are usually alpha-numeric.
347
		 *
348
		 * @param string Name of the controller
349
		 * @since 2014.03
350
		 * @see client/html/checkout/standard/url/target
351
		 * @see client/html/checkout/standard/url/action
352
		 * @see client/html/checkout/standard/url/config
353
		 */
354
355
		/** client/html/checkout/standard/url/action
356
		 * Name of the action that should create the output
357
		 *
358
		 * In Model-View-Controller (MVC) applications, actions are the methods of a
359
		 * controller that create parts of the output displayed in the generated HTML page.
360
		 * Action names are usually alpha-numeric.
361
		 *
362
		 * @param string Name of the action
363
		 * @since 2014.03
364
		 * @see client/html/checkout/standard/url/target
365
		 * @see client/html/checkout/standard/url/controller
366
		 * @see client/html/checkout/standard/url/config
367
		 */
368
369
		/** client/html/checkout/standard/url/config
370
		 * Associative list of configuration options used for generating the URL
371
		 *
372
		 * You can specify additional options as key/value pairs used when generating
373
		 * the URLs, like
374
		 *
375
		 *  client/html/<clientname>/url/config = array( 'absoluteUri' => true )
376
		 *
377
		 * The available key/value pairs depend on the application that embeds the e-commerce
378
		 * framework. This is because the infrastructure of the application is used for
379
		 * generating the URLs. The full list of available config options is referenced
380
		 * in the "see also" section of this page.
381
		 *
382
		 * @param string Associative list of configuration options
383
		 * @since 2014.03
384
		 * @see client/html/checkout/standard/url/target
385
		 * @see client/html/checkout/standard/url/controller
386
		 * @see client/html/checkout/standard/url/action
387
		 * @see client/html/url/config
388
		 */
389
390
		$step = null;
391
		do {
392
			$lastStep = $step;
393
		}
394
		while( ( $step = array_shift( $steps ) ) !== null && $step !== $activeStep );
395
396
397
		if( $lastStep !== null ) {
0 ignored issues
show
introduced by
The condition $lastStep !== null is always false.
Loading history...
398
			$param = array( 'c_step' => $lastStep );
399
			$view->standardUrlBack = $view->link( 'client/html/checkout/standard/url', $param );
400
		} else {
401
			$view->standardUrlBack = $view->link( 'client/html/basket/standard/url' );
402
		}
403
404
		if( !isset( $view->standardUrlNext ) && ( $nextStep = array_shift( $steps ) ) !== null ) {
405
			$param = array( 'c_step' => $nextStep );
406
			$view->standardUrlNext = $view->link( 'client/html/checkout/standard/url', $param );
407
		}
408
		// don't overwrite $view->standardUrlNext so process step URL is used
409
410
		return $view;
411
	}
412
413
414
	/** client/html/checkout/standard/template-body
415
	 * Relative path to the HTML body template of the checkout standard client.
416
	 *
417
	 * The template file contains the HTML code and processing instructions
418
	 * to generate the result shown in the body of the frontend. The
419
	 * configuration string is the path to the template file relative
420
	 * to the templates directory (usually in client/html/templates).
421
	 *
422
	 * You can overwrite the template file configuration in extensions and
423
	 * provide alternative templates. These alternative templates should be
424
	 * named like the default one but suffixed by
425
	 * an unique name. You may use the name of your project for this. If
426
	 * you've implemented an alternative client class as well, it
427
	 * should be suffixed by the name of the new class.
428
	 *
429
	 * @param string Relative path to the template creating code for the HTML page body
430
	 * @since 2014.03
431
	 * @see client/html/checkout/template-header
432
	 */
433
434
	/** client/html/checkout/standard/template-header
435
	 * Relative path to the HTML header template of the checkout standard client.
436
	 *
437
	 * The template file contains the HTML code and processing instructions
438
	 * to generate the HTML code that is inserted into the HTML page header
439
	 * of the rendered page in the frontend. The configuration string is the
440
	 * path to the template file relative to the templates directory (usually
441
	 * in client/html/templates).
442
	 *
443
	 * You can overwrite the template file configuration in extensions and
444
	 * provide alternative templates. These alternative templates should be
445
	 * named like the default one but suffixed by
446
	 * an unique name. You may use the name of your project for this. If
447
	 * you've implemented an alternative client class as well, it
448
	 * should be suffixed by the name of the new class.
449
	 *
450
	 * @param string Relative path to the template creating code for the HTML page head
451
	 * @since 2014.03
452
	 * @see client/html/checkout/template-body
453
	 */
454
}
455