Passed
Push — master ( b9e760...0cd60c )
by Aimeos
03:30
created

Standard   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 68
dl 0
loc 335
rs 10
c 2
b 0
f 0
wmc 28

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubClient() 0 77 1
A header() 0 11 4
A body() 0 40 5
B init() 0 43 7
A getSubClientNames() 0 3 1
A isAvailable() 0 3 1
B data() 0 54 9
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-2021
7
 * @package Client
8
 * @subpackage Html
9
 */
10
11
12
namespace Aimeos\Client\Html\Checkout\Standard\Payment;
13
14
15
// Strings for translation
16
sprintf( 'payment' );
17
18
19
/**
20
 * Default implementation of checkout payment 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/payment/subparts
30
	 * List of HTML sub-clients rendered within the checkout standard payment 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/payment/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 != 'payment' && !( in_array( 'payment', $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->paymentBody = $html;
87
88
		/** client/html/checkout/standard/payment/template-body
89
		 * Relative path to the HTML body template of the checkout standard payment 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 with the string "standard" replaced 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, "standard"
101
		 * should be replaced 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/payment/template-header
107
		 */
108
		$tplconf = 'client/html/checkout/standard/payment/template-body';
109
		$default = 'checkout/standard/payment-body-standard';
110
111
		return $view->render( $view->config( $tplconf, $default ) );
112
	}
113
114
115
	/**
116
	 * Returns the HTML string for insertion into the header.
117
	 *
118
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
119
	 * @return string|null String including HTML tags for the header on error
120
	 */
121
	public function header( string $uid = '' ) : ?string
122
	{
123
		$view = $this->view();
124
		$step = $view->get( 'standardStepActive' );
125
		$onepage = $view->config( 'client/html/checkout/standard/onepage', [] );
126
127
		if( $step != 'payment' && !( in_array( 'payment', $onepage ) && in_array( $step, $onepage ) ) ) {
128
			return '';
129
		}
130
131
		return parent::header( $uid );
132
	}
133
134
135
	/**
136
	 * Returns the sub-client given by its name.
137
	 *
138
	 * @param string $type Name of the client type
139
	 * @param string|null $name Name of the sub-client (Default if null)
140
	 * @return \Aimeos\Client\Html\Iface Sub-client object
141
	 */
142
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
143
	{
144
		/** client/html/checkout/standard/payment/decorators/excludes
145
		 * Excludes decorators added by the "common" option from the checkout standard payment html client
146
		 *
147
		 * Decorators extend the functionality of a class by adding new aspects
148
		 * (e.g. log what is currently done), executing the methods of the underlying
149
		 * class only in certain conditions (e.g. only for logged in users) or
150
		 * modify what is returned to the caller.
151
		 *
152
		 * This option allows you to remove a decorator added via
153
		 * "client/html/common/decorators/default" before they are wrapped
154
		 * around the html client.
155
		 *
156
		 *  client/html/checkout/standard/payment/decorators/excludes = array( 'decorator1' )
157
		 *
158
		 * This would remove the decorator named "decorator1" from the list of
159
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
160
		 * "client/html/common/decorators/default" to the html client.
161
		 *
162
		 * @param array List of decorator names
163
		 * @since 2015.08
164
		 * @category Developer
165
		 * @see client/html/common/decorators/default
166
		 * @see client/html/checkout/standard/payment/decorators/global
167
		 * @see client/html/checkout/standard/payment/decorators/local
168
		 */
169
170
		/** client/html/checkout/standard/payment/decorators/global
171
		 * Adds a list of globally available decorators only to the checkout standard payment html client
172
		 *
173
		 * Decorators extend the functionality of a class by adding new aspects
174
		 * (e.g. log what is currently done), executing the methods of the underlying
175
		 * class only in certain conditions (e.g. only for logged in users) or
176
		 * modify what is returned to the caller.
177
		 *
178
		 * This option allows you to wrap global decorators
179
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
180
		 *
181
		 *  client/html/checkout/standard/payment/decorators/global = array( 'decorator1' )
182
		 *
183
		 * This would add the decorator named "decorator1" defined by
184
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
185
		 *
186
		 * @param array List of decorator names
187
		 * @since 2015.08
188
		 * @category Developer
189
		 * @see client/html/common/decorators/default
190
		 * @see client/html/checkout/standard/payment/decorators/excludes
191
		 * @see client/html/checkout/standard/payment/decorators/local
192
		 */
193
194
		/** client/html/checkout/standard/payment/decorators/local
195
		 * Adds a list of local decorators only to the checkout standard payment html client
196
		 *
197
		 * Decorators extend the functionality of a class by adding new aspects
198
		 * (e.g. log what is currently done), executing the methods of the underlying
199
		 * class only in certain conditions (e.g. only for logged in users) or
200
		 * modify what is returned to the caller.
201
		 *
202
		 * This option allows you to wrap local decorators
203
		 * ("\Aimeos\Client\Html\Checkout\Decorator\*") around the html client.
204
		 *
205
		 *  client/html/checkout/standard/payment/decorators/local = array( 'decorator2' )
206
		 *
207
		 * This would add the decorator named "decorator2" defined by
208
		 * "\Aimeos\Client\Html\Checkout\Decorator\Decorator2" only to the html client.
209
		 *
210
		 * @param array List of decorator names
211
		 * @since 2015.08
212
		 * @category Developer
213
		 * @see client/html/common/decorators/default
214
		 * @see client/html/checkout/standard/payment/decorators/excludes
215
		 * @see client/html/checkout/standard/payment/decorators/global
216
		 */
217
218
		return $this->createSubClient( 'checkout/standard/payment/' . $type, $name );
219
	}
220
221
222
	/**
223
	 * Processes the input, e.g. store given values.
224
	 *
225
	 * A view must be available and this method doesn't generate any output
226
	 * besides setting view variables.
227
	 */
228
	public function init()
229
	{
230
		$view = $this->view();
231
		$context = $this->context();
232
233
		try
234
		{
235
			$basketCtrl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
236
			$servCtrl = \Aimeos\Controller\Frontend::create( $context, 'service' )->uses( ['media', 'price', 'text'] );
237
238
			// only start if there's something to do
239
			if( ( $serviceIds = $view->param( 'c_paymentoption', null ) ) !== null )
240
			{
241
				$basketCtrl->deleteService( 'payment' );
242
243
				foreach( (array) $serviceIds as $idx => $id )
244
				{
245
					try
246
					{
247
						$basketCtrl->addService( $servCtrl->get( $id ), $view->param( 'c_payment/' . $id, [] ), $idx );
248
					}
249
					catch( \Aimeos\Controller\Frontend\Basket\Exception $e )
250
					{
251
						$view->deliveryError = $e->getErrors();
252
						$view->standardErrorList = array_merge( $view->get( 'standardErrorList', [] ), $e->getErrors() );
253
254
						throw $e;
255
					}
256
				}
257
			}
258
259
260
			parent::init();
261
262
263
			if( !isset( $view->standardStepActive ) && !$this->call( 'isAvailable', $basketCtrl->get() ) ) {
264
				$view->standardStepActive = 'payment';
265
			}
266
		}
267
		catch( \Exception $e )
268
		{
269
			$view->standardStepActive = 'payment';
270
			throw $e;
271
		}
272
	}
273
274
275
	/**
276
	 * Returns the list of sub-client names configured for the client.
277
	 *
278
	 * @return array List of HTML client names
279
	 */
280
	protected function getSubClientNames() : array
281
	{
282
		return $this->context()->config()->get( $this->subPartPath, $this->subPartNames );
283
	}
284
285
286
	/**
287
	 * Sets the necessary parameter values in the view.
288
	 *
289
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
290
	 * @param array &$tags Result array for the list of tags that are associated to the output
291
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
292
	 * @return \Aimeos\MW\View\Iface Modified view object
293
	 */
294
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
295
	{
296
		$context = $this->context();
297
		$domains = ['media', 'price', 'text'];
298
299
		/** client/html/checkout/standard/payment/domains
300
		 * List of domain names whose items should be available in the checkout payment templates
301
		 *
302
		 * The templates rendering checkout payment related data usually add the
303
		 * images, prices and texts associated to each item. If you want to display
304
		 * additional content like the attributes, you can configure your own list
305
		 * of domains (attribute, media, price, text, etc. are domains) whose items
306
		 * are fetched from the storage.
307
		 *
308
		 * @param array List of domain names
309
		 * @since 2019.04
310
		 * @category Developer
311
		 * @see client/html/checkout/standard/delivery/domains
312
		 */
313
		$domains = $context->config()->get( 'client/html/checkout/standard/payment/domains', $domains );
314
315
		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
316
		$serviceCntl = \Aimeos\Controller\Frontend::create( $context, 'service' );
317
318
		$services = [];
319
		$basket = $basketCntl->get();
320
		$providers = $serviceCntl->uses( $domains )->type( 'payment' )->getProviders();
321
		$orderServices = map( $basket->getService( 'payment' ) )->col( null, 'order.base.service.serviceid' );
322
323
		foreach( $providers as $id => $provider )
324
		{
325
			if( $provider->isAvailable( $basket ) === true )
326
			{
327
				$attr = $provider->getConfigFE( $basket );
328
329
				if( $oservice = $orderServices->get( $id ) )
330
				{
331
					foreach( $attr as $key => $item )
332
					{
333
						$value = is_array( $item->getDefault() ) ? key( $item->getDefault() ) : $item->getDefault();
334
						$value = $oservice->getAttribute( $key, 'payment' ) ?: $value;
335
						$item->value = $oservice->getAttribute( $key . '/hidden', 'payment' ) ?: $value;
336
					}
337
				}
338
339
				$services[$id] = $provider->getServiceItem()->set( 'attributes', $attr )
340
					->set( 'price', $provider->calcPrice( $basket ) );
341
			}
342
		}
343
344
		$view->paymentServices = $services;
345
		$view->paymentOption = $view->param( 'c_paymentoption', $orderServices->firstKey() ?: key( $services ) );
346
347
		return parent::data( $view, $tags, $expire );
348
	}
349
350
351
	/**
352
	 * Tests if an item is available and the step can be skipped
353
	 *
354
	 * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object
355
	 * @return bool TRUE if step can be skipped, FALSE if not
356
	 */
357
	protected function isAvailable( \Aimeos\MShop\Order\Item\Base\Iface $basket ) : bool
358
	{
359
		return !empty( $basket->getService( 'payment' ) );
360
	}
361
}
362