Passed
Push — master ( 84367b...2c80aa )
by Aimeos
03:28
created

Standard::getSubClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 76
rs 10

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), 2021
6
 * @package Admin
7
 * @subpackage JQAdm
8
 */
9
10
11
namespace Aimeos\Admin\JQAdm\Settings;
12
13
sprintf( 'settings' ); // for translation
14
15
16
/**
17
 * Default implementation of settings 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
	/**
27
	 * Adds the required data used in the template
28
	 *
29
	 * @param \Aimeos\MW\View\Iface $view View object
30
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
31
	 */
32
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
33
	{
34
		$view->itemSubparts = $this->getSubClientNames();
35
		return $view;
36
	}
37
38
39
	/**
40
	 * Saves the data
41
	 *
42
	 * @return string|null HTML output
43
	 */
44
	public function save() : ?string
45
	{
46
		$view = $this->getView();
47
48
		$manager = \Aimeos\MShop::create( $this->getContext(), 'locale/site' );
49
		$manager->begin();
50
51
		try
52
		{
53
			$view->item = $this->fromArray( $view->param( 'item', [] ) );
54
			$view->itemBody = parent::save();
55
56
			$manager->save( clone $view->item );
57
			$manager->commit();
58
59
			return $this->redirect( 'settings', 'search', null, 'save' );
60
		}
61
		catch( \Exception $e )
62
		{
63
			$manager->rollback();
64
			$this->report( $e, 'save' );
65
		}
66
67
		return $this->search();
68
	}
69
70
71
	/**
72
	 * Returns the settings root node
73
	 *
74
	 * @return string|null HTML output
75
	 */
76
	public function search() : ?string
77
	{
78
		$view = $this->getView();
79
80
		try
81
		{
82
			$view->item = $this->getContext()->getLocale()->getSiteItem();
83
			$view->itemBody = parent::search();
84
		}
85
		catch( \Exception $e )
86
		{
87
			$this->report( $e, 'search' );
88
		}
89
90
		return $this->render( $view );
91
	}
92
93
94
	/**
95
	 * Returns the sub-client given by its name.
96
	 *
97
	 * @param string $type Name of the client type
98
	 * @param string|null $name Name of the sub-client (Default if null)
99
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
100
	 */
101
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
102
	{
103
		/** admin/jqadm/settings/decorators/excludes
104
		 * Excludes decorators added by the "common" option from the settings JQAdm client
105
		 *
106
		 * Decorators extend the functionality of a class by adding new aspects
107
		 * (e.g. log what is currently done), executing the methods of the underlying
108
		 * class only in certain conditions (e.g. only for logged in users) or
109
		 * modify what is returned to the caller.
110
		 *
111
		 * This option allows you to remove a decorator added via
112
		 * "client/jqadm/common/decorators/default" before they are wrapped
113
		 * around the JQAdm client.
114
		 *
115
		 *  admin/jqadm/settings/decorators/excludes = array( 'decorator1' )
116
		 *
117
		 * This would remove the decorator named "decorator1" from the list of
118
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
119
		 * "client/jqadm/common/decorators/default" to the JQAdm client.
120
		 *
121
		 * @param array List of decorator names
122
		 * @since 2021.07
123
		 * @category Developer
124
		 * @see admin/jqadm/common/decorators/default
125
		 * @see admin/jqadm/settings/decorators/global
126
		 * @see admin/jqadm/settings/decorators/local
127
		 */
128
129
		/** admin/jqadm/settings/decorators/global
130
		 * Adds a list of globally available decorators only to the settings JQAdm client
131
		 *
132
		 * Decorators extend the functionality of a class by adding new aspects
133
		 * (e.g. log what is currently done), executing the methods of the underlying
134
		 * class only in certain conditions (e.g. only for logged in users) or
135
		 * modify what is returned to the caller.
136
		 *
137
		 * This option allows you to wrap global decorators
138
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
139
		 *
140
		 *  admin/jqadm/settings/decorators/global = array( 'decorator1' )
141
		 *
142
		 * This would add the decorator named "decorator1" defined by
143
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
144
		 *
145
		 * @param array List of decorator names
146
		 * @since 2021.07
147
		 * @category Developer
148
		 * @see admin/jqadm/common/decorators/default
149
		 * @see admin/jqadm/settings/decorators/excludes
150
		 * @see admin/jqadm/settings/decorators/local
151
		 */
152
153
		/** admin/jqadm/settings/decorators/local
154
		 * Adds a list of local decorators only to the settings JQAdm client
155
		 *
156
		 * Decorators extend the functionality of a class by adding new aspects
157
		 * (e.g. log what is currently done), executing the methods of the underlying
158
		 * class only in certain conditions (e.g. only for logged in users) or
159
		 * modify what is returned to the caller.
160
		 *
161
		 * This option allows you to wrap local decorators
162
		 * ("\Aimeos\Admin\JQAdm\Settings\Decorator\*") around the JQAdm client.
163
		 *
164
		 *  admin/jqadm/settings/decorators/local = array( 'decorator2' )
165
		 *
166
		 * This would add the decorator named "decorator2" defined by
167
		 * "\Aimeos\Admin\JQAdm\Settings\Decorator\Decorator2" only to the JQAdm client.
168
		 *
169
		 * @param array List of decorator names
170
		 * @since 2021.07
171
		 * @category Developer
172
		 * @see admin/jqadm/common/decorators/default
173
		 * @see admin/jqadm/settings/decorators/excludes
174
		 * @see admin/jqadm/settings/decorators/global
175
		 */
176
		return $this->createSubClient( 'settings/' . $type, $name );
177
	}
178
179
180
	/**
181
	 * Returns the list of sub-client names configured for the client.
182
	 *
183
	 * @return array List of JQAdm client names
184
	 */
185
	protected function getSubClientNames() : array
186
	{
187
		/** admin/jqadm/settings/subparts
188
		 * List of JQAdm sub-clients rendered within the settings section
189
		 *
190
		 * The output of the frontend is composed of the code generated by the JQAdm
191
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
192
		 * that are responsible for rendering certain sub-parts of the output. The
193
		 * sub-clients can contain JQAdm clients themselves and therefore a
194
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
195
		 * the output that is placed inside the container of its parent.
196
		 *
197
		 * At first, always the JQAdm code generated by the parent is printed, then
198
		 * the JQAdm code of its sub-clients. The order of the JQAdm sub-clients
199
		 * determines the order of the output of these sub-clients inside the parent
200
		 * container. If the configured list of clients is
201
		 *
202
		 *  array( "subclient1", "subclient2" )
203
		 *
204
		 * you can easily change the order of the output by reordering the subparts:
205
		 *
206
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
207
		 *
208
		 * You can also remove one or more parts if they shouldn't be rendered:
209
		 *
210
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
211
		 *
212
		 * As the clients only generates structural JQAdm, the layout defined via CSS
213
		 * should support adding, removing or reordering content by a fluid like
214
		 * design.
215
		 *
216
		 * @param array List of sub-client names
217
		 * @since 2021.07
218
		 * @category Developer
219
		 */
220
		return $this->getContext()->getConfig()->get( 'admin/jqadm/settings/subparts', [] );
221
	}
222
223
224
	/**
225
	 * Creates new and updates existing items using the data array
226
	 *
227
	 * @param array $data Data array
228
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface New settings item object
229
	 */
230
	protected function fromArray( array $data ) : \Aimeos\MShop\Locale\Item\Site\Iface
231
	{
232
		return $this->getContext()->getLocale()->getSiteItem()->setConfig( $data );
233
	}
234
235
236
	/**
237
	 * Constructs the data array for the view from the given item
238
	 *
239
	 * @param \Aimeos\MShop\Locale\Item\Site\Iface $item Settings item object
240
	 * @return string[] Multi-dimensional associative list of item data
241
	 */
242
	protected function toArray( \Aimeos\MShop\Locale\Item\Site\Iface $item, bool $copy = false ) : array
243
	{
244
		return $item->getConfig();
245
	}
246
247
248
	/**
249
	 * Returns the rendered template including the view data
250
	 *
251
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
252
	 * @return string HTML output
253
	 */
254
	protected function render( \Aimeos\MW\View\Iface $view ) : string
255
	{
256
		/** admin/jqadm/settings/template-item
257
		 * Relative path to the HTML body template for the settings item.
258
		 *
259
		 * The template file contains the HTML code and processing instructions
260
		 * to generate the result shown in the body of the frontend. The
261
		 * configuration string is the path to the template file relative
262
		 * to the templates directory (usually in admin/jqadm/templates).
263
		 *
264
		 * You can overwrite the template file configuration in extensions and
265
		 * provide alternative templates. These alternative templates should be
266
		 * named like the default one but with the string "default" replaced by
267
		 * an unique name. You may use the name of your project for this. If
268
		 * you've implemented an alternative client class as well, "default"
269
		 * should be replaced by the name of the new class.
270
		 *
271
		 * @param string Relative path to the template creating the HTML code
272
		 * @since 2021.07
273
		 * @category Developer
274
		 */
275
		$tplconf = 'admin/jqadm/settings/template-item';
276
		$default = 'settings/item-standard';
277
278
		return $view->render( $view->config( $tplconf, $default ) );
279
	}
280
}
281