Passed
Branch master (d032f7)
by Aimeos
05:30
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 76
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 Aimeos (aimeos.org), 2017-2018
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Customer\Address;
12
13
sprintf( 'address' ); // for translation
14
15
16
/**
17
 * Default implementation of customer address JQAdm client.
18
 *
19
 * @package Admin
20
 * @subpackage JQAdm
21
 */
22
class Standard
23
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
24
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
25
{
26
	/** admin/jqadm/customer/address/name
27
	 * Name of the address subpart used by the JQAdm customer implementation
28
	 *
29
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Customer\Address\Myname".
30
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
31
	 *
32
	 * @param string Last part of the JQAdm class name
33
	 * @since 2017.06
34
	 * @category Developer
35
	 */
36
37
38
	/**
39
	 * Copies a resource
40
	 *
41
	 * @return string HTML output
42
	 */
43
	public function copy()
44
	{
45
		$view = $this->getView();
46
47
		$view->addressData = $this->toArray( $view->item, true );
48
		$view->addressBody = '';
49
50
		foreach( $this->getSubClients() as $client ) {
51
			$view->addressBody .= $client->copy();
52
		}
53
54
		return $this->render( $view );
55
	}
56
57
58
	/**
59
	 * Creates a new resource
60
	 *
61
	 * @return string HTML output
62
	 */
63
	public function create()
64
	{
65
		$view = $this->getView();
66
		$siteid = $this->getContext()->getLocale()->getSiteId();
67
		$data = $view->param( 'address', [] );
68
69
		foreach( $data as $idx => $entry ) {
70
			$data[$idx]['customer.address.siteid'] = $siteid;
71
		}
72
73
		$view->addressData = $data;
74
		$view->addressBody = '';
75
76
		foreach( $this->getSubClients() as $client ) {
77
			$view->addressBody .= $client->create();
78
		}
79
80
		return $this->render( $view );
81
	}
82
83
84
	/**
85
	 * Returns a single resource
86
	 *
87
	 * @return string HTML output
88
	 */
89
	public function get()
90
	{
91
		$view = $this->getView();
92
93
		$view->addressData = $this->toArray( $view->item );
94
		$view->addressBody = '';
95
96
		foreach( $this->getSubClients() as $client ) {
97
			$view->addressBody .= $client->get();
98
		}
99
100
		return $this->render( $view );
101
	}
102
103
104
	/**
105
	 * Saves the data
106
	 */
107
	public function save()
108
	{
109
		$view = $this->getView();
110
111
		try
112
		{
113
			$this->fromArray( $view->item, $view->param( 'address', [] ) );
114
			$view->addressBody = '';
115
116
			foreach( $this->getSubClients() as $client ) {
117
				$view->addressBody .= $client->save();
118
			}
119
120
			return;
121
		}
122
		catch( \Aimeos\MShop\Exception $e )
123
		{
124
			$error = array( 'customer-item-address' => $this->getContext()->getI18n()->dt( 'mshop', $e->getMessage() ) );
125
			$view->errors = $view->get( 'errors', [] ) + $error;
126
			$this->logException( $e );
127
		}
128
		catch( \Exception $e )
129
		{
130
			$error = array( 'customer-item-address' => $e->getMessage() . ', ' . $e->getFile() . ':' . $e->getLine() );
131
			$view->errors = $view->get( 'errors', [] ) + $error;
132
			$this->logException( $e );
133
		}
134
135
		throw new \Aimeos\Admin\JQAdm\Exception();
136
	}
137
138
139
	/**
140
	 * Returns the sub-client given by its name.
141
	 *
142
	 * @param string $type Name of the client type
143
	 * @param string|null $name Name of the sub-client (Default if null)
144
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
145
	 */
146
	public function getSubClient( $type, $name = null )
147
	{
148
		/** admin/jqadm/customer/address/decorators/excludes
149
		 * Excludes decorators added by the "common" option from the customer JQAdm client
150
		 *
151
		 * Decorators extend the functionality of a class by adding new aspects
152
		 * (e.g. log what is currently done), executing the methods of the underlying
153
		 * class only in certain conditions (e.g. only for logged in users) or
154
		 * modify what is returned to the caller.
155
		 *
156
		 * This option allows you to remove a decorator added via
157
		 * "admin/jqadm/common/decorators/default" before they are wrapped
158
		 * around the JQAdm client.
159
		 *
160
		 *  admin/jqadm/customer/address/decorators/excludes = array( 'decorator1' )
161
		 *
162
		 * This would remove the decorator named "decorator1" from the list of
163
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
164
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
165
		 *
166
		 * @param array List of decorator names
167
		 * @since 2016.01
168
		 * @category Developer
169
		 * @see admin/jqadm/common/decorators/default
170
		 * @see admin/jqadm/customer/address/decorators/global
171
		 * @see admin/jqadm/customer/address/decorators/local
172
		 */
173
174
		/** admin/jqadm/customer/address/decorators/global
175
		 * Adds a list of globally available decorators only to the customer JQAdm 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 global decorators
183
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
184
		 *
185
		 *  admin/jqadm/customer/address/decorators/global = array( 'decorator1' )
186
		 *
187
		 * This would add the decorator named "decorator1" defined by
188
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
189
		 *
190
		 * @param array List of decorator names
191
		 * @since 2016.01
192
		 * @category Developer
193
		 * @see admin/jqadm/common/decorators/default
194
		 * @see admin/jqadm/customer/address/decorators/excludes
195
		 * @see admin/jqadm/customer/address/decorators/local
196
		 */
197
198
		/** admin/jqadm/customer/address/decorators/local
199
		 * Adds a list of local decorators only to the customer JQAdm client
200
		 *
201
		 * Decorators extend the functionality of a class by adding new aspects
202
		 * (e.g. log what is currently done), executing the methods of the underlying
203
		 * class only in certain conditions (e.g. only for logged in users) or
204
		 * modify what is returned to the caller.
205
		 *
206
		 * This option allows you to wrap local decorators
207
		 * ("\Aimeos\Admin\JQAdm\Customer\Decorator\*") around the JQAdm client.
208
		 *
209
		 *  admin/jqadm/customer/address/decorators/local = array( 'decorator2' )
210
		 *
211
		 * This would add the decorator named "decorator2" defined by
212
		 * "\Aimeos\Admin\JQAdm\Customer\Decorator\Decorator2" only to the JQAdm client.
213
		 *
214
		 * @param array List of decorator names
215
		 * @since 2016.01
216
		 * @category Developer
217
		 * @see admin/jqadm/common/decorators/default
218
		 * @see admin/jqadm/customer/address/decorators/excludes
219
		 * @see admin/jqadm/customer/address/decorators/global
220
		 */
221
		return $this->createSubClient( 'customer/address/' . $type, $name );
222
	}
223
224
225
	/**
226
	 * Returns the list of sub-client names configured for the client.
227
	 *
228
	 * @return array List of JQAdm client names
229
	 */
230
	protected function getSubClientNames()
231
	{
232
		/** admin/jqadm/customer/address/standard/subparts
233
		 * List of JQAdm sub-clients rendered within the customer address section
234
		 *
235
		 * The output of the frontend is composed of the code generated by the JQAdm
236
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
237
		 * that are responsible for rendering certain sub-parts of the output. The
238
		 * sub-clients can contain JQAdm clients themselves and therefore a
239
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
240
		 * the output that is placed inside the container of its parent.
241
		 *
242
		 * At first, always the JQAdm code generated by the parent is printed, then
243
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
244
		 * determines the order of the output of these sub-clients inside the parent
245
		 * container. If the configured list of clients is
246
		 *
247
		 *  array( "subclient1", "subclient2" )
248
		 *
249
		 * you can easily change the order of the output by reordering the subparts:
250
		 *
251
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
252
		 *
253
		 * You can also remove one or more parts if they shouldn't be rendered:
254
		 *
255
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
256
		 *
257
		 * As the clients only generates structural JQAdm, the layout defined via CSS
258
		 * should support adding, removing or reordering content by a fluid like
259
		 * design.
260
		 *
261
		 * @param array List of sub-client names
262
		 * @since 2016.01
263
		 * @category Developer
264
		 */
265
		return $this->getContext()->getConfig()->get( 'admin/jqadm/customer/address/standard/subparts', [] );
266
	}
267
268
269
	/**
270
	 * Creates new and updates existing items using the data array
271
	 *
272
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object without referenced domain items
273
	 * @param string[] $data Data array
274
	 */
275
	protected function fromArray( \Aimeos\MShop\Customer\Item\Iface $item, array $data )
276
	{
277
		$manager = \Aimeos\MShop::create( $this->getContext(), 'customer/address' );
278
279
		$addrItems = $item->getAddressItems();
280
281
		foreach( $data as $entry )
282
		{
283
			if( isset( $addrItems[$entry['customer.address.id']] ) )
284
			{
285
				$addrItem = $addrItems[$entry['customer.address.id']];
286
				unset( $addrItems[$entry['customer.address.id']] );
287
			}
288
			else
289
			{
290
				$addrItem = $manager->createItem();
291
			}
292
293
			$addrItem->fromArray( $entry );
0 ignored issues
show
Bug introduced by
$entry of type string is incompatible with the type array expected by parameter $list of Aimeos\MShop\Common\Item\Iface::fromArray(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

293
			$addrItem->fromArray( /** @scrutinizer ignore-type */ $entry );
Loading history...
294
			$item->addAddressItem( $addrItem );
295
		}
296
297
		$item->deleteAddressItems( $addrItems );
298
	}
299
300
301
	/**
302
	 * Constructs the data array for the view from the given item
303
	 *
304
	 * @param \Aimeos\MShop\Customer\Item\Iface $item Customer item object including referenced domain items
305
	 * @param boolean $copy True if items should be copied, false if not
306
	 * @return string[] Multi-dimensional associative list of item data
307
	 */
308
	protected function toArray( \Aimeos\MShop\Customer\Item\Iface $item, $copy = false )
309
	{
310
		$siteId = $this->getContext()->getLocale()->getSiteId();
311
		$data = [];
312
313
		foreach( $item->getAddressItems() as $addrItem )
314
		{
315
			$list = $addrItem->toArray( true );
316
317
			if( $copy === true )
318
			{
319
				$list['customer.address.siteid'] = $siteId;
320
				$list['customer.address.id'] = '';
321
			}
322
323
			$data[] = $list;
324
		}
325
326
		return $data;
327
	}
328
329
330
	/**
331
	 * Returns the rendered template including the view data
332
	 *
333
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
334
	 * @return string HTML output
335
	 */
336
	protected function render( \Aimeos\MW\View\Iface $view )
337
	{
338
		/** admin/jqadm/customer/address/template-item
339
		 * Relative path to the HTML body template of the address subpart for customers.
340
		 *
341
		 * The template file contains the HTML code and processing instructions
342
		 * to generate the result shown in the body of the frontend. The
343
		 * configuration string is the path to the template file relative
344
		 * to the templates directory (usually in admin/jqadm/templates).
345
		 *
346
		 * You can overwrite the template file configuration in extensions and
347
		 * provide alternative templates. These alternative templates should be
348
		 * named like the default one but with the string "default" replaced by
349
		 * an unique name. You may use the name of your project for this. If
350
		 * you've implemented an alternative client class as well, "default"
351
		 * should be replaced by the name of the new class.
352
		 *
353
		 * @param string Relative path to the template creating the HTML code
354
		 * @since 2016.04
355
		 * @category Developer
356
		 */
357
		$tplconf = 'admin/jqadm/customer/address/template-item';
358
		$default = 'customer/item-address-standard';
359
360
		return $view->render( $view->config( $tplconf, $default ) );
361
	}
362
}
363