Passed
Push — master ( 390a06...2b1a03 )
by Aimeos
04:14
created

View::addUrl()   C

Complexity

Conditions 15
Paths 85

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 15
eloc 28
c 3
b 0
f 0
nc 85
nop 4
dl 0
loc 51
rs 5.9166

How to fix   Long Method    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 GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package TYPO3
7
 */
8
9
10
namespace Aimeos\Aimeos\Base;
11
12
use \TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
15
/**
16
 * Create Aimeos views
17
 *
18
 * @package TYPO3
19
 */
20
class View
21
{
22
	/**
23
	 * Creates the view object for the HTML client.
24
	 *
25
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
26
	 * @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\TYPO3\CMS\Core\Routing\RouterInterface $uriBuilder URL builder
27
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
28
	 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface|null $request Request object
29
	 * @param string|null $locale Code of the current language or null for no translation
30
	 * @return \Aimeos\MW\View\Iface View object
31
	 */
32
	public static function get( \Aimeos\MShop\Context\Item\Iface $context, $uriBuilder, array $templatePaths,
33
		\TYPO3\CMS\Extbase\Mvc\RequestInterface $request = null, string $locale = null ) : \Aimeos\MW\View\Iface
34
	{
35
		$obj = GeneralUtility::makeInstance( \TYPO3\CMS\Extbase\Object\ObjectManager::class );
36
		$engines = ['.html' => new \Aimeos\MW\View\Engine\Typo3( $obj )];
37
38
		$prefix = 'ai';
39
		if( $uriBuilder instanceof \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder ) {
40
			$prefix = $uriBuilder->getArgumentPrefix();
41
		}
42
43
		$view = new \Aimeos\MW\View\Standard( $templatePaths, $engines );
44
		$view->prefix = $prefix;
45
46
		$config = $context->getConfig();
47
		$session = $context->getSession();
48
49
		self::addTranslate( $view, $locale, $config->get( 'i18n', [] ) );
50
		self::addParam( $view, $request );
51
		self::addConfig( $view, $config );
52
		self::addDate( $view, $config, $locale );
53
		self::addFormparam( $view, [$prefix] );
54
		self::addNumber( $view, $config, $locale );
55
		self::addUrl( $view, $config, $uriBuilder, $request );
56
		self::addSession( $view, $session );
57
		self::addRequest( $view, $request );
58
		self::addResponse( $view );
59
		self::addAccess( $view );
60
61
		return $view;
62
	}
63
64
65
	/**
66
	 * Adds the "access" helper to the view object
67
	 *
68
	 * @param \Aimeos\MW\View\Iface $view View object
69
	 * @return \Aimeos\MW\View\Iface Modified view object
70
	 */
71
	protected static function addAccess( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
72
	{
73
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_access'] )
74
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_access'] ) )
75
		) {
76
			return $fcn( $view );
77
		}
78
79
		if( TYPO3_MODE === 'BE' )
80
		{
81
			if( $GLOBALS['BE_USER']->isAdmin() === false )
82
			{
83
				$groups = [];
84
				foreach( (array) $GLOBALS['BE_USER']->userGroups as $entry ) {
85
					$groups[] = $entry['title'];
86
				}
87
				$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $groups );
88
			}
89
			else
90
			{
91
				$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
92
			}
93
		}
94
		else
95
		{
96
			$t3context = GeneralUtility::makeInstance( 'TYPO3\CMS\Core\Context\Context' );
97
98
			if( $t3context->getPropertyFromAspect( 'frontend.user', 'isLoggedIn' ) ) {
99
				$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $GLOBALS['TSFE']->fe_user->groupData['title'] );
100
			} else {
101
				$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, [] );
102
			}
103
		}
104
105
		$view->addHelper( 'access', $helper );
106
107
		return $view;
108
	}
109
110
111
	/**
112
	 * Adds the "config" helper to the view object
113
	 *
114
	 * @param \Aimeos\MW\View\Iface $view View object
115
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
116
	 * @return \Aimeos\MW\View\Iface Modified view object
117
	 */
118
	protected static function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config ) : \Aimeos\MW\View\Iface
119
	{
120
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_config'] )
121
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_config'] ) )
122
		) {
123
			return $fcn( $view, $config );
124
		}
125
126
		$conf = new \Aimeos\MW\Config\Decorator\Protect( clone $config, ['admin', 'client', 'resource/fs/baseurl'] );
127
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $conf );
128
		$view->addHelper( 'config', $helper );
129
130
		return $view;
131
	}
132
133
134
	/**
135
	 * Adds the "date" helper to the view object
136
	 *
137
	 * @param \Aimeos\MW\View\Iface $view View object
138
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
139
	 * @param string|null $locale (Country specific) language code
140
	 * @return \Aimeos\MW\View\Iface Modified view object
141
	 */
142
	protected static function addDate( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config,
143
		string $locale = null ) : \Aimeos\MW\View\Iface
144
	{
145
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_date'] )
146
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_date'] ) )
147
		) {
148
			return $fcn( $view, $config, $locale );
149
		}
150
151
		$format = $config->get( 'client/html/common/date/format' );
152
153
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, $format );
154
		$view->addHelper( 'date', $helper );
155
156
		return $view;
157
	}
158
159
160
	/**
161
	 * Adds the "formparam" helper to the view object
162
	 *
163
	 * @param \Aimeos\MW\View\Iface $view View object
164
	 * @param array $prefixes List of prefixes for the form name to build multi-dimensional arrays
165
	 * @return \Aimeos\MW\View\Iface Modified view object
166
	 */
167
	protected static function addFormparam( \Aimeos\MW\View\Iface $view, array $prefixes ) : \Aimeos\MW\View\Iface
168
	{
169
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_formparam'] )
170
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_formparam'] ) )
171
		) {
172
			return $fcn( $view, $prefixes );
173
		}
174
175
		$helper = new \Aimeos\MW\View\Helper\Formparam\Standard( $view, $prefixes );
176
		$view->addHelper( 'formparam', $helper );
177
178
		return $view;
179
	}
180
181
182
	/**
183
	 * Adds the "number" helper to the view object
184
	 *
185
	 * @param \Aimeos\MW\View\Iface $view View object
186
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
187
	 * @param string|null $locale (Country specific) language code
188
	 * @return \Aimeos\MW\View\Iface Modified view object
189
	 */
190
	protected static function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config,
191
		string $locale = null ) : \Aimeos\MW\View\Iface
192
	{
193
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_number'] )
194
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_number'] ) )
195
		) {
196
			return $fcn( $view, $config, $locale );
197
		}
198
199
		$format = $config->get( 'client/html/common/number/format' );
200
201
		$helper = new \Aimeos\MW\View\Helper\Number\Locale( $view, $locale ?? 'en', $format );
202
		$view->addHelper( 'number', $helper );
203
204
		return $view;
205
	}
206
207
208
	/**
209
	 * Adds the "param" helper to the view object
210
	 *
211
	 * @param \Aimeos\MW\View\Iface $view View object
212
	 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface|null $request Request object or null if not available
213
	 * @return \Aimeos\MW\View\Iface Modified view object
214
	 */
215
	protected static function addParam( \Aimeos\MW\View\Iface $view,
216
		\TYPO3\CMS\Extbase\Mvc\RequestInterface $request = null ) : \Aimeos\MW\View\Iface
217
	{
218
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_param'] )
219
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_param'] ) )
220
		) {
221
			return $fcn( $view, $request );
222
		}
223
224
		$params = $request ? $request->getArguments() : [];
225
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
226
		$view->addHelper( 'param', $helper );
227
228
		return $view;
229
	}
230
231
232
	/**
233
	 * Adds the "request" helper to the view object
234
	 *
235
	 * @param \Aimeos\MW\View\Iface $view View object
236
	 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface|null $request Request object
237
	 * @return \Aimeos\MW\View\Iface Modified view object
238
	 */
239
	protected static function addRequest( \Aimeos\MW\View\Iface $view,
240
		\TYPO3\CMS\Extbase\Mvc\RequestInterface $request = null ) : \Aimeos\MW\View\Iface
241
	{
242
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_request'] )
243
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_request'] ) )
244
		) {
245
			return $fcn( $view, $request );
246
		}
247
248
		$target = $GLOBALS["TSFE"]->id;
249
		$get = GeneralUtility::_GET();
250
		$post = GeneralUtility::_POST();
251
252
		$helper = new \Aimeos\MW\View\Helper\Request\Typo3( $view, $target, $_FILES, $get, $post, $_COOKIE, $_SERVER );
253
		$view->addHelper( 'request', $helper );
254
255
		return $view;
256
	}
257
258
259
	/**
260
	 * Adds the "response" helper to the view object
261
	 *
262
	 * @param \Aimeos\MW\View\Iface $view View object
263
	 * @return \Aimeos\MW\View\Iface Modified view object
264
	 */
265
	protected static function addResponse( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
266
	{
267
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_response'] )
268
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_response'] ) )
269
		) {
270
			return $fcn( $view );
271
		}
272
273
		$helper = new \Aimeos\MW\View\Helper\Response\Typo3( $view );
274
		$view->addHelper( 'response', $helper );
275
276
		return $view;
277
	}
278
279
280
	/**
281
	 * Adds the "session" helper to the view object
282
	 *
283
	 * @param \Aimeos\MW\View\Iface $view View object
284
	 * @param \Aimeos\MW\Session\Iface $session Session object
285
	 * @return \Aimeos\MW\View\Iface Modified view object
286
	 */
287
	protected static function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session ) : \Aimeos\MW\View\Iface
288
	{
289
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
290
		$view->addHelper( 'session', $helper );
291
292
		return $view;
293
	}
294
295
296
	/**
297
	 * Adds the "translate" helper to the view object
298
	 *
299
	 * @param \Aimeos\MW\View\Iface $view View object
300
	 * @param string|null $langid ISO language code, e.g. "de" or "de_CH"
301
	 * @param array $local Local translations
302
	 * @return \Aimeos\MW\View\Iface Modified view object
303
	 */
304
	protected static function addTranslate( \Aimeos\MW\View\Iface $view, string $langid = null, array $local ) : \Aimeos\MW\View\Iface
305
	{
306
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_translate'] )
307
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_translate'] ) )
308
		) {
309
			return $fcn( $view, $langid, $local );
310
		}
311
312
		if( $langid )
313
		{
314
			$i18n = \Aimeos\Aimeos\Base::getI18n( [$langid], $local );
315
			$translation = $i18n[$langid];
316
		}
317
		else
318
		{
319
			$translation = new \Aimeos\MW\Translation\None( 'en' );
320
		}
321
322
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
323
		$view->addHelper( 'translate', $helper );
324
325
		return $view;
326
	}
327
328
329
	/**
330
	 * Adds the "url" helper to the view object
331
	 *
332
	 * @param \Aimeos\MW\View\Iface $view View object
333
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
334
	 * @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\TYPO3\CMS\Core\Routing\RouterInterface $uriBuilder URI builder
335
	 * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface|null $request Request object
336
	 * @return \Aimeos\MW\View\Iface Modified view object
337
	 */
338
	protected static function addUrl( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config, $uriBuilder,
339
		\TYPO3\CMS\Extbase\Mvc\RequestInterface $request = null ) : \Aimeos\MW\View\Iface
340
	{
341
		if( isset( $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_url'] )
342
			&& is_callable( ( $fcn = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view_url'] ) )
343
		) {
344
			return $fcn( $view, $config, $uriBuilder, $request );
345
		}
346
347
		$fixed = [];
348
349
		if( $request )
350
		{
351
			$name = $config->get( 'typo3/param/name/site', 'site' );
352
353
			if( $request !== null && $request->hasArgument( $name ) === true ) {
354
				$fixed[$name] = $request->getArgument( $name );
355
			} elseif( ( $value = GeneralUtility::_GP( 'S' ) ) !== null ) {
356
				$fixed['S'] = $value;
357
			}
358
359
360
			$name = $config->get( 'typo3/param/name/language', 'locale' );
361
362
			if( $request !== null && $request->hasArgument( $name ) === true ) {
363
				$fixed[$name] = $request->getArgument( $name );
364
			} elseif( ( $value = GeneralUtility::_GP( 'L' ) ) !== null ) {
365
				$fixed['L'] = $value;
366
			}
367
368
369
			$name = $config->get( 'typo3/param/name/currency', 'currency' );
370
371
			if( $request !== null && $request->hasArgument( $name ) === true ) {
372
				$fixed[$name] = $request->getArgument( $name );
373
			} elseif( ( $value = GeneralUtility::_GP( 'C' ) ) !== null ) {
374
				$fixed['C'] = $value;
375
			}
376
		}
377
378
		if( $uriBuilder instanceof \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder ) {
379
			$url = new \Aimeos\MW\View\Helper\Url\Typo3( $view, $uriBuilder, $fixed );
380
		} elseif( $uriBuilder instanceof \TYPO3\CMS\Core\Routing\RouterInterface ) {
0 ignored issues
show
introduced by
$uriBuilder is always a sub-type of TYPO3\CMS\Core\Routing\RouterInterface.
Loading history...
381
			$url = new \Aimeos\MW\View\Helper\Url\T3Router( $view, $uriBuilder, $fixed );
382
		} else {
383
			throw new \RuntimeException( 'No router for generating URLs available' );
384
		}
385
386
		$view->addHelper( 'url', $url );
387
388
		return $view;
389
	}
390
}
391