Passed
Push — master ( df431e...5639b9 )
by Aimeos
10:51
created

Standard::getBody()   B

Complexity

Conditions 7
Paths 44

Size

Total Lines 104
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 33
c 3
b 0
f 0
nc 44
nop 1
dl 0
loc 104
rs 8.4586

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), 2020-2021
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Cms\Page;
12
13
14
/**
15
 * Default implementation of cms page section HTML clients.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Standard
21
	extends \Aimeos\Client\Html\Common\Client\Factory\Base
22
	implements \Aimeos\Client\Html\Common\Client\Factory\Iface
23
{
24
	/** client/html/cms/page/subparts
25
	 * List of HTML sub-clients rendered within the cms page 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 order of the HTML sub-clients
36
	 * determines the order 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 order of the output by reordering 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 reordering content by a fluid like
51
	 * design.
52
	 *
53
	 * @param array List of sub-client names
54
	 * @since 2021.04
55
	 * @category Developer
56
	 */
57
	private $subPartPath = 'client/html/cms/page/subparts';
58
	private $subPartNames = ['contact', 'cataloglist'];
59
60
	private $tags = [];
61
	private $expire;
62
	private $view;
63
64
65
	/**
66
	 * Returns the HTML code for insertion into the body.
67
	 *
68
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
69
	 * @return string HTML code
70
	 */
71
	public function body( string $uid = '' ) : string
72
	{
73
		$prefixes = ['path'];
74
		$context = $this->getContext();
75
76
		/** client/html/cms/page/cache
77
		 * Enables or disables caching only for the cms page component
78
		 *
79
		 * Disable caching for components can be useful if you would have too much
80
		 * entries to cache or if the component contains non-cacheable parts that
81
		 * can't be replaced using the modifyBody() and modifyHeader() methods.
82
		 *
83
		 * @param boolean True to enable caching, false to disable
84
		 * @category Developer
85
		 * @category User
86
		 * @see client/html/cms/page/cache
87
		 * @see client/html/cms/filter/cache
88
		 * @see client/html/cms/lists/cache
89
		 */
90
91
		/** client/html/cms/page
92
		 * All parameters defined for the cms page component and its subparts
93
		 *
94
		 * This returns all settings related to the page component.
95
		 * Please refer to the single settings for pages.
96
		 *
97
		 * @param array Associative list of name/value settings
98
		 * @category Developer
99
		 * @see client/html/cms#page
100
		 */
101
		$confkey = 'client/html/cms/page';
102
103
		if( ( $html = $this->getCached( 'body', $uid, $prefixes, $confkey ) ) === null )
104
		{
105
			$view = $this->getView();
106
107
			/** client/html/cms/page/template-body
108
			 * Relative path to the HTML body template of the cms page client.
109
			 *
110
			 * The template file contains the HTML code and processing instructions
111
			 * to generate the result shown in the body of the frontend. The
112
			 * configuration string is the path to the template file relative
113
			 * to the templates directory (usually in client/html/templates).
114
			 *
115
			 * You can overwrite the template file configuration in extensions and
116
			 * provide alternative templates. These alternative templates should be
117
			 * named like the default one but with the string "standard" replaced by
118
			 * an unique name. You may use the name of your project for this. If
119
			 * you've implemented an alternative client class as well, "standard"
120
			 * should be replaced by the name of the new class.
121
			 *
122
			 * @param string Relative path to the template creating code for the HTML page body
123
			 * @since 2021.04
124
			 * @category Developer
125
			 * @see client/html/cms/page/template-header
126
			 */
127
			$tplconf = 'client/html/cms/page/template-body';
128
			$default = 'cms/page/body-standard';
129
130
			try
131
			{
132
				$html = '';
133
				$view = $this->view = $this->view ?? $this->getObject()->data( $view, $this->tags, $this->expire );
134
135
				foreach( $this->getSubClients() as $subclient ) {
136
					$html .= $subclient->setView( $view )->body( $uid );
137
				}
138
				$view->pageBody = $html;
139
140
				$html = $view->render( $view->config( $tplconf, $default ) );
141
				$this->setCached( 'body', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
142
143
				return $this->modifyBody( $html, $uid );
144
			}
145
			catch( \Aimeos\Client\Html\Exception $e )
146
			{
147
				$error = array( $context->translate( 'client', $e->getMessage() ) );
148
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
149
			}
150
			catch( \Aimeos\Controller\Frontend\Exception $e )
151
			{
152
				$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
153
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
154
			}
155
			catch( \Aimeos\MShop\Exception $e )
156
			{
157
				$error = array( $context->translate( 'mshop', $e->getMessage() ) );
158
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
159
			}
160
			catch( \Exception $e )
161
			{
162
				$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
163
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
164
				$this->logException( $e );
165
			}
166
167
			$html = $view->render( $view->config( $tplconf, $default ) );
168
		}
169
		else
170
		{
171
			$html = $this->modifyBody( $html, $uid );
172
		}
173
174
		return $html;
175
	}
176
177
178
	/**
179
	 * Returns the HTML string for insertion into the header.
180
	 *
181
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
182
	 * @return string|null String including HTML tags for the header on error
183
	 */
184
	public function header( string $uid = '' ) : ?string
185
	{
186
		$prefixes = ['page'];
187
		$confkey = 'client/html/cms/page';
188
189
		if( ( $html = $this->getCached( 'header', $uid, $prefixes, $confkey ) ) === null )
190
		{
191
			$view = $this->getView();
192
193
			/** client/html/cms/page/template-header
194
			 * Relative path to the HTML header template of the cms page client.
195
			 *
196
			 * The template file contains the HTML code and processing instructions
197
			 * to generate the HTML code that is inserted into the HTML page header
198
			 * of the rendered page in the frontend. The configuration string is the
199
			 * path to the template file relative to the templates directory (usually
200
			 * in client/html/templates).
201
			 *
202
			 * You can overwrite the template file configuration in extensions and
203
			 * provide alternative templates. These alternative templates should be
204
			 * named like the default one but with the string "standard" replaced by
205
			 * an unique name. You may use the name of your project for this. If
206
			 * you've implemented an alternative client class as well, "standard"
207
			 * should be replaced by the name of the new class.
208
			 *
209
			 * @param string Relative path to the template creating code for the HTML page head
210
			 * @since 2021.04
211
			 * @category Developer
212
			 * @see client/html/cms/page/template-body
213
			 */
214
			$tplconf = 'client/html/cms/page/template-header';
215
			$default = 'cms/page/header-standard';
216
217
			try
218
			{
219
				$html = '';
220
				$view = $this->view = $this->view ?? $this->getObject()->data( $view, $this->tags, $this->expire );
221
222
				foreach( $this->getSubClients() as $subclient ) {
223
					$html .= $subclient->setView( $view )->header( $uid );
224
				}
225
				$view->pageHeader = $html;
226
227
				$html = $view->render( $view->config( $tplconf, $default ) );
228
				$this->setCached( 'header', $uid, $prefixes, $confkey, $html, $this->tags, $this->expire );
229
230
				return $html;
231
			}
232
			catch( \Exception $e )
233
			{
234
				$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), [$e->getMessage()] );
235
			}
236
		}
237
		else
238
		{
239
			$html = $this->modifyHeader( $html, $uid );
240
		}
241
242
		return $html;
243
	}
244
245
246
	/**
247
	 * Returns the sub-client given by its name.
248
	 *
249
	 * @param string $type Name of the client type
250
	 * @param string|null $name Name of the sub-client (Default if null)
251
	 * @return \Aimeos\Client\Html\Iface Sub-client object
252
	 */
253
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface
254
	{
255
		/** client/html/cms/page/decorators/excludes
256
		 * Excludes decorators added by the "common" option from the cms page html client
257
		 *
258
		 * Decorators extend the functionality of a class by adding new aspects
259
		 * (e.g. log what is currently done), executing the methods of the underlying
260
		 * class only in certain conditions (e.g. only for logged in users) or
261
		 * modify what is returned to the caller.
262
		 *
263
		 * This option allows you to remove a decorator added via
264
		 * "client/html/common/decorators/default" before they are wrapped
265
		 * around the html client.
266
		 *
267
		 *  client/html/cms/page/decorators/excludes = array( 'decorator1' )
268
		 *
269
		 * This would remove the decorator named "decorator1" from the list of
270
		 * common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via
271
		 * "client/html/common/decorators/default" to the html client.
272
		 *
273
		 * @param array List of decorator names
274
		 * @since 2021.04
275
		 * @category Developer
276
		 * @see client/html/common/decorators/default
277
		 * @see client/html/cms/page/decorators/global
278
		 * @see client/html/cms/page/decorators/local
279
		 */
280
281
		/** client/html/cms/page/decorators/global
282
		 * Adds a list of globally available decorators only to the cms page html client
283
		 *
284
		 * Decorators extend the functionality of a class by adding new aspects
285
		 * (e.g. log what is currently done), executing the methods of the underlying
286
		 * class only in certain conditions (e.g. only for logged in users) or
287
		 * modify what is returned to the caller.
288
		 *
289
		 * This option allows you to wrap global decorators
290
		 * ("\Aimeos\Client\Html\Common\Decorator\*") around the html client.
291
		 *
292
		 *  client/html/cms/page/decorators/global = array( 'decorator1' )
293
		 *
294
		 * This would add the decorator named "decorator1" defined by
295
		 * "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client.
296
		 *
297
		 * @param array List of decorator names
298
		 * @since 2021.04
299
		 * @category Developer
300
		 * @see client/html/common/decorators/default
301
		 * @see client/html/cms/page/decorators/excludes
302
		 * @see client/html/cms/page/decorators/local
303
		 */
304
305
		/** client/html/cms/page/decorators/local
306
		 * Adds a list of local decorators only to the cms page html client
307
		 *
308
		 * Decorators extend the functionality of a class by adding new aspects
309
		 * (e.g. log what is currently done), executing the methods of the underlying
310
		 * class only in certain conditions (e.g. only for logged in users) or
311
		 * modify what is returned to the caller.
312
		 *
313
		 * This option allows you to wrap local decorators
314
		 * ("\Aimeos\Client\Html\Cms\Decorator\*") around the html client.
315
		 *
316
		 *  client/html/cms/page/decorators/local = array( 'decorator2' )
317
		 *
318
		 * This would add the decorator named "decorator2" defined by
319
		 * "\Aimeos\Client\Html\Cms\Decorator\Decorator2" only to the html client.
320
		 *
321
		 * @param array List of decorator names
322
		 * @since 2021.04
323
		 * @category Developer
324
		 * @see client/html/common/decorators/default
325
		 * @see client/html/cms/page/decorators/excludes
326
		 * @see client/html/cms/page/decorators/global
327
		 */
328
		return $this->createSubClient( 'cms/page/' . $type, $name );
329
	}
330
331
332
	/**
333
	 * Processes the input, e.g. store given values.
334
	 *
335
	 * A view must be available and this method doesn't generate any output
336
	 * besides setting view variables if necessary.
337
	 */
338
	public function init()
339
	{
340
		$view = $this->getView();
341
		$context = $this->getContext();
342
343
		try
344
		{
345
			parent::init();
346
		}
347
		catch( \Aimeos\Client\Html\Exception $e )
348
		{
349
			$error = array( $context->translate( 'client', $e->getMessage() ) );
350
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
351
		}
352
		catch( \Aimeos\Controller\Frontend\Exception $e )
353
		{
354
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
355
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
356
		}
357
		catch( \Aimeos\MShop\Exception $e )
358
		{
359
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
360
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
361
		}
362
		catch( \Exception $e )
363
		{
364
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
365
			$view->pageErrorList = array_merge( $view->get( 'pageErrorList', [] ), $error );
366
			$this->logException( $e );
367
		}
368
	}
369
370
371
	/**
372
	 * Sets the necessary parameter values in the view.
373
	 *
374
	 * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output
375
	 * @param array &$tags Result array for the list of tags that are associated to the output
376
	 * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry)
377
	 * @return \Aimeos\MW\View\Iface Modified view object
378
	 */
379
	public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface
380
	{
381
		$context = $this->getContext();
382
		$controller = \Aimeos\Controller\Frontend::create( $context, 'cms' );
383
384
		/** client/html/cms/page/domains
385
		 * A list of domain names whose items should be available in the cms page view template
386
		 *
387
		 * The templates rendering the cms page section use the texts and
388
		 * maybe images and attributes associated to the categories. You can
389
		 * configure your own list of domains (attribute, media, price, product,
390
		 * text, etc. are domains) whose items are fetched from the storage.
391
		 * Please keep in mind that the more domains you add to the configuration,
392
		 * the more time is required for fetching the content!
393
		 *
394
		 * @param array List of domain names
395
		 * @since 2021.04
396
		 */
397
		$domains = $context->getConfig()->get( 'client/html/cms/page/domains', ['text'] );
398
399
		$path = '/' . trim( $view->param( 'path' ), '/' );
400
401
		if( $page = $controller->uses( $domains )->compare( '==', 'cms.url', $path )->search()->first() )
0 ignored issues
show
Bug introduced by
The method uses() does not exist on Aimeos\Controller\Frontend\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\Controller\Frontend\Common\Iface or Aimeos\Controller\Frontend\Common\Decorator\Iface or Aimeos\Controller\Fronte...ommon\Decorator\Example. Are you sure you never get one of those? ( Ignorable by Annotation )

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

401
		if( $page = $controller->/** @scrutinizer ignore-call */ uses( $domains )->compare( '==', 'cms.url', $path )->search()->first() )
Loading history...
402
		{
403
			$this->addMetaItems( $page, $expire, $tags );
404
405
			$view->pageCmsItem = $page;
406
			$view->pageContent = $page->getRefItems( 'text', 'content' )->map( function( $item ) {
407
				$data = ( $json = json_decode( $item->getContent(), true ) ) ? $json['html'] : $item->getContent();
408
				return '<div class="cms-content">' . $data . '</div>';
409
			} )->all();
410
		}
411
412
		return parent::data( $view, $tags, $expire );
413
	}
414
415
416
	/**
417
	 * Returns the list of sub-client names configured for the client.
418
	 *
419
	 * @return array List of HTML client names
420
	 */
421
	protected function getSubClientNames() : array
422
	{
423
		return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames );
424
	}
425
}
426