Passed
Push — master ( 0f1828...ed6c8e )
by Aimeos
04:20
created

Standard::process()   B

Complexity

Conditions 10
Paths 33

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 33
nop 0
dl 0
loc 41
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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 Aimeos (aimeos.org), 2019-2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Account\Profile\Address;
12
13
14
/**
15
 * Default implementation of acount profile address HTML client.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Summary\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/account/profile/address/subparts
25
	 * List of HTML sub-clients rendered within the account profile address section
26
	 *
27
	 * The output of the frontend is composed of the code generated by the HTML
28
	 * clients. Each HTML client can consist of serveral (or none) sub-clients
29
	 * that are responsible for rendering certain sub-parts of the output. The
30
	 * sub-clients can contain HTML clients themselves and therefore a
31
	 * hierarchical tree of HTML clients is composed. Each HTML client creates
32
	 * the output that is placed inside the container of its parent.
33
	 *
34
	 * At first, always the HTML code generated by the parent is printed, then
35
	 * the HTML code of its sub-clients. The address of the HTML sub-clients
36
	 * determines the address of the output of these sub-clients inside the parent
37
	 * container. If the configured list of clients is
38
	 *
39
	 *  array( "subclient1", "subclient2" )
40
	 *
41
	 * you can easily change the address of the output by readdressing the subparts:
42
	 *
43
	 *  client/html/<clients>/subparts = array( "subclient1", "subclient2" )
44
	 *
45
	 * You can also remove one or more parts if they shouldn't be rendered:
46
	 *
47
	 *  client/html/<clients>/subparts = array( "subclient1" )
48
	 *
49
	 * As the clients only generates structural HTML, the layout defined via CSS
50
	 * should support adding, removing or readdressing content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2019.07
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/account/profile/address/subparts';
58
	private $subPartNames = [];
59
60
61
	/**
62
	 * Returns the HTML code for insertion into the body.
63
	 *
64
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
65
	 * @return string HTML code
66
	 */
67
	public function body( string $uid = '' ) : string
68
	{
69
		$view = $this->getView();
70
71
		$html = '';
72
		foreach( $this->getSubClients() as $subclient ) {
73
			$html .= $subclient->setView( $view )->body( $uid );
74
		}
75
		$view->addressBody = $html;
76
77
		/** client/html/account/profile/address/template-body
78
		 * Relative path to the HTML body template of the account profile address client.
79
		 *
80
		 * The template file contains the HTML code and processing instructions
81
		 * to generate the result shown in the body of the frontend. The
82
		 * configuration string is the path to the template file relative
83
		 * to the templates directory (usually in client/html/templates).
84
		 *
85
		 * You can overwrite the template file configuration in extensions and
86
		 * provide alternative templates. These alternative templates should be
87
		 * named like the default one but with the string "standard" replaced by
88
		 * an unique name. You may use the name of your project for this. If
89
		 * you've implemented an alternative client class as well, "standard"
90
		 * should be replaced by the name of the new class.
91
		 *
92
		 * @param string Relative path to the template creating code for the HTML page body
93
		 * @since 2019.07
94
		 * @category Developer
95
		 * @see client/html/account/profile/address/template-header
96
		 */
97
		$tplconf = 'client/html/account/profile/address/template-body';
98
		$default = 'account/profile/address-body-standard';
99
100
		return $view->render( $view->config( $tplconf, $default ) );
101
	}
102
103
104
	/**
105
	 * Returns the sub-client given by its name.
106
	 *
107
	 * @param string $type Name of the client type
108
	 * @param string|null $name Name of the sub-client (Default if null)
109
	 * @return \Aimeos\Client\Html\Iface Sub-client object
110
	 */
111
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
112
	{
113
		/** client/html/account/profile/address/decorators/excludes
114
		 * Excludes decorators added by the "common" option from the account profile address html client
115
		 *
116
		 * Decorators extend the functionality of a class by adding new aspects
117
		 * (e.g. log what is currently done), executing the methods of the underlying
118
		 * class only in certain conditions (e.g. only for logged in users) or
119
		 * modify what is returned to the caller.
120
		 *
121
		 * This option allows you to remove a decorator added via
122
		 * "client/html/common/decorators/default" before they are wrapped
123
		 * around the html client.
124
		 *
125
		 *  client/html/account/profile/address/decorators/excludes = array( 'decorator1' )
126
		 *
127
		 * This would remove the decorator named "decorator1" from the list of
128
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
129
		 * "client/html/common/decorators/default" to the html client.
130
		 *
131
		 * @param array List of decorator names
132
		 * @since 2019.07
133
		 * @category Developer
134
		 * @see client/html/common/decorators/default
135
		 * @see client/html/account/profile/address/decorators/global
136
		 * @see client/html/account/profile/address/decorators/local
137
		 */
138
139
		/** client/html/account/profile/address/decorators/global
140
		 * Adds a list of globally available decorators only to the account profile address html client
141
		 *
142
		 * Decorators extend the functionality of a class by adding new aspects
143
		 * (e.g. log what is currently done), executing the methods of the underlying
144
		 * class only in certain conditions (e.g. only for logged in users) or
145
		 * modify what is returned to the caller.
146
		 *
147
		 * This option allows you to wrap global decorators
148
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
149
		 *
150
		 *  client/html/account/profile/address/decorators/global = array( 'decorator1' )
151
		 *
152
		 * This would add the decorator named "decorator1" defined by
153
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
154
		 *
155
		 * @param array List of decorator names
156
		 * @since 2019.07
157
		 * @category Developer
158
		 * @see client/html/common/decorators/default
159
		 * @see client/html/account/profile/address/decorators/excludes
160
		 * @see client/html/account/profile/address/decorators/local
161
		 */
162
163
		/** client/html/account/profile/address/decorators/local
164
		 * Adds a list of local decorators only to the account profile address html client
165
		 *
166
		 * Decorators extend the functionality of a class by adding new aspects
167
		 * (e.g. log what is currently done), executing the methods of the underlying
168
		 * class only in certain conditions (e.g. only for logged in users) or
169
		 * modify what is returned to the caller.
170
		 *
171
		 * This option allows you to wrap local decorators
172
		 * ("\Aimeos\Client\Html\Account\Decorator\*") around the html client.
173
		 *
174
		 *  client/html/account/profile/address/decorators/local = array( 'decorator2' )
175
		 *
176
		 * This would add the decorator named "decorator2" defined by
177
		 * "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client.
178
		 *
179
		 * @param array List of decorator names
180
		 * @since 2019.07
181
		 * @category Developer
182
		 * @see client/html/common/decorators/default
183
		 * @see client/html/account/profile/address/decorators/excludes
184
		 * @see client/html/account/profile/address/decorators/global
185
		 */
186
187
		return $this->createSubClient( 'account/profile/address/' . $type, $name );
188
	}
189
190
191
	/**
192
	 * Processes the input, e.g. store given values.
193
	 *
194
	 * A view must be available and this method doesn't generate any output
195
	 * besides setting view variables if necessary.
196
	 */
197
	public function init()
198
	{
199
		$view = $this->getView();
200
201
		if( !$view->param( 'address/save' ) && !$view->param( 'address/delete' ) ) {
202
			return parent::init();
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::init() targeting Aimeos\Client\Html\Base::init() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
203
		}
204
205
		$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'customer' );
206
		$addrItems = $cntl->uses( ['customer/address'] )->get()->getAddressItems();
207
		$cntl->add( $view->param( 'address/payment', [] ) );
208
		$map = [];
209
210
		foreach( $view->param( 'address/delivery/customer.address.id', [] ) as $pos => $id )
211
		{
212
			foreach( $view->param( 'address/delivery', [] ) as $key => $list )
213
			{
214
				if( array_key_exists( $pos, $list ) ) {
215
					$map[$pos][$key] = $list[$pos];
216
				}
217
			}
218
		}
219
220
		if( $pos = $view->param( 'address/delete' ) ) {
221
			unset( $map[$pos] );
222
		}
223
224
		foreach( $map as $pos => $data )
225
		{
226
			$addrItem = $addrItems->get( $pos ) ?: $cntl->createAddressItem();
227
			$cntl->addAddressItem( $addrItem->fromArray( $data ), $pos );
228
			$addrItems->remove( $pos );
229
		}
230
231
		foreach( $addrItems as $addrItem ) {
232
			$cntl->deleteAddressItem( $addrItem );
233
		}
234
235
		$cntl->store();
236
237
		parent::init();
238
	}
239
240
241
	/**
242
	 * Sets the necessary parameter values in the view.
243
	 *
244
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
245
	 * @param array &$tags Result array for the list of tags that are associated to the output
246
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
247
	 * @return \Aimeos\MW\View\Iface Modified view object
248
	 */
249
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
250
	{
251
		$context = $this->getContext();
252
		$config = $context->getConfig();
253
		$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' );
0 ignored issues
show
Unused Code introduced by
The assignment to $cntl is dead and can be removed.
Loading history...
254
255
		/** client/html/common/address/salutations
256
		 * List of salutions the customers can select from in the HTML frontend
257
		 *
258
		 * The following salutations are available:
259
		 *
260
		 * * empty string for "unknown"
261
		 * * company
262
		 * * mr
263
		 * * ms
264
		 *
265
		 * You can modify the list of salutation codes and remove the ones
266
		 * which shouldn't be used or add new ones.
267
		 *
268
		 * @param array List of available salutation codes
269
		 * @since 2021.04
270
		 * @see client/html/account/profile/address/salutations
271
		 */
272
		$salutations = $config->get( 'client/html/common/address/salutations', ['', 'company', 'mr', 'ms'] );
273
274
		/** client/html/account/profile/address/salutations
275
		 * List of salutions the customers can select from in their account
276
		 *
277
		 * The following salutations are available:
278
		 *
279
		 * * empty string for "unknown"
280
		 * * company
281
		 * * mr
282
		 * * ms
283
		 *
284
		 * You can modify the list of salutation codes and remove the ones
285
		 * which shouldn't be used or add new ones.
286
		 *
287
		 * @param array List of available salutation codes
288
		 * @since 2021.04
289
		 * @see client/html/common/address/salutations
290
		 */
291
		$salutations = $config->get( 'client/html/account/profile/address/salutations', $salutations );
292
293
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
294
		$languages = $localeManager->search( $localeManager->filter( true ) )
295
			->col( 'locale.languageid', 'locale.languageid' );
296
297
298
		$deliveries = [];
299
		$addr = $view->profileCustomerItem->getPaymentAddress();
300
301
		if( !$addr->getLanguageId() ) {
302
			$addr->setLanguageId( $context->getLocale()->getLanguageId() );
303
		}
304
305
		$billing = $addr->toArray();
306
		$billing['string'] = $this->getAddressString( $view, $addr );
307
308
		foreach( $view->profileCustomerItem->getAddressItems() as $pos => $address )
309
		{
310
			$delivery = $address->toArray();
311
			$delivery['string'] = $this->getAddressString( $view, $address );
312
			$deliveries[$pos] = $delivery;
313
		}
314
315
316
		$view->addressBilling = $billing;
317
		$view->addressDelivery = $deliveries;
318
		$view->addressCountries = $view->config( 'client/html/checkout/standard/address/countries', [] );
319
		$view->addressStates = $view->config( 'client/html/checkout/standard/address/states', [] );
320
		$view->addressSalutations = $salutations;
321
		$view->addressLanguages = $languages;
322
323
		return parent::data( $view, $tags, $expire );
324
	}
325
326
327
	/**
328
	 * Returns the address as string
329
	 *
330
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
331
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $addr Order address item
332
	 * @return string Address as string
333
	 */
334
	protected function getAddressString( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Common\Item\Address\Iface $addr )
335
	{
336
		return preg_replace( "/\n+/m", "\n", trim( sprintf(
337
			/// Address format with company (%1$s), salutation (%2$s), title (%3$s), first name (%4$s), last name (%5$s),
338
			/// address part one (%6$s, e.g street), address part two (%7$s, e.g house number), address part three (%8$s, e.g additional information),
339
			/// postal/zip code (%9$s), city (%10$s), state (%11$s), country (%12$s), language (%13$s),
340
			/// e-mail (%14$s), phone (%15$s), facsimile/telefax (%16$s), web site (%17$s), vatid (%18$s)
341
			$view->translate( 'client', '%1$s
342
%2$s %3$s %4$s %5$s
343
%6$s %7$s
344
%8$s
345
%9$s %10$s
346
%11$s
347
%12$s
348
%13$s
349
%14$s
350
%15$s
351
%16$s
352
%17$s
353
%18$s
354
'
355
			),
356
			$addr->getCompany(),
357
			$view->translate( 'mshop/code', (string) $addr->getSalutation() ),
358
			$addr->getTitle(),
359
			$addr->getFirstName(),
360
			$addr->getLastName(),
361
			$addr->getAddress1(),
362
			$addr->getAddress2(),
363
			$addr->getAddress3(),
364
			$addr->getPostal(),
365
			$addr->getCity(),
366
			$addr->getState(),
367
			$view->translate( 'country', (string) $addr->getCountryId() ),
368
			$view->translate( 'language', (string) $addr->getLanguageId() ),
369
			$addr->getEmail(),
370
			$addr->getTelephone(),
371
			$addr->getTelefax(),
372
			$addr->getWebsite(),
373
			$addr->getVatID()
374
		) ) );
375
	}
376
377
378
	/**
379
	 * Returns the list of sub-client names configured for the client.
380
	 *
381
	 * @return array List of HTML client names
382
	 */
383
	protected function getSubClientNames() : array
384
	{
385
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
386
	}
387
}
388