Passed
Push — master ( 269272...ea560c )
by Aimeos
02:34
created

Typo3::transform()   C

Complexity

Conditions 11
Paths 216

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 27
c 4
b 0
f 0
dl 0
loc 45
rs 6.2833
cc 11
nc 216
nop 6

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 Metaways Infosystems GmbH, 2012
6
 * @copyright Aimeos (aimeos.org), 2014-2024
7
 * @package MW
8
 * @subpackage View
9
 */
10
11
12
namespace Aimeos\Base\View\Helper\Url;
13
14
15
/**
16
 * View helper class for building URLs.
17
 *
18
 * @package MW
19
 * @subpackage View
20
 */
21
class Typo3
22
	extends \Aimeos\Base\View\Helper\Url\Base
23
	implements \Aimeos\Base\View\Helper\Url\Iface
24
{
25
	private \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder;
26
	private ?string $prefix;
27
	private array $fixed;
28
29
30
	/**
31
	 * Initializes the URL view helper.
32
	 *
33
	 * @param \Aimeos\Base\View\Iface $view View instance with registered view helpers
34
	 * @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder TYPO3 URI builder
35
	 * @param array $fixed Fixed parameters that should be added to each URL
36
	 */
37
	public function __construct( \Aimeos\Base\View\Iface $view, \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder, array $fixed )
38
	{
39
		parent::__construct( $view );
40
41
		$this->prefix = $uriBuilder->getArgumentPrefix();
42
		$this->uriBuilder = clone $uriBuilder;
43
		$this->fixed = $fixed;
44
	}
45
46
47
	/**
48
	 * Returns the URL assembled from the given arguments.
49
	 *
50
	 * @param string|null $target Route or page which should be the target of the link (if any)
51
	 * @param string|null $controller Name of the controller which should be part of the link (if any)
52
	 * @param string|null $action Name of the action which should be part of the link (if any)
53
	 * @param array $params Associative list of parameters that should be part of the URL
54
	 * @param array $trailing Trailing URL parts that are not relevant to identify the resource (for pretty URLs)
55
	 * @param array $config Additional configuration parameter per URL
56
	 * @return string Complete URL that can be used in the template
57
	 */
58
	public function transform( string $target = null, string $controller = null, string $action = null,
59
		array $params = [], array $trailing = [], array $config = [] ) : string
60
	{
61
		$locale = $this->getValue( $params, 'locale' );
62
63
		if( (bool) $this->getValue( $config, 'BE', false ) === false ) {
64
			$params['controller'] = $controller !== null ? ucfirst( $controller ) : null;
65
			$params['action'] = $action;
66
		}
67
68
		if( $this->prefix != '' && (bool) $this->getValue( $config, 'namespace', true ) === true ) {
69
			$params = [$this->prefix => $params];
70
		}
71
		$params += $this->fixed;
72
73
		if( (bool) $this->getValue( $config, 'BE', false ) === true ) {
74
			$params['controller'] = $controller !== null ? ucfirst( $controller ) : null;
75
			$params['action'] = $action;
76
		}
77
78
		if( ( $eid = $this->getValue( $config, 'eID' ) ) !== null ) {
79
			$params['eID'] = $eid;
80
		}
81
82
		if( $locale !== null ) {
83
			$params['L'] = $locale;
84
		}
85
86
		$useCHash = (bool) $this->getValue( $config, 'chash', false );
87
88
		$this->uriBuilder->reset()
89
			->setTargetPageUid( (int) $target )
90
			->setCreateAbsoluteUri( (bool) $this->getValue( $config, 'absoluteUri', false ) )
91
			->setTargetPageType( (int) $this->getValue( $config, 'type', 0 ) )
92
			->setNoCache( (bool) $this->getValue( $config, 'nocache', false ) )
93
			->setFormat( (string) $this->getValue( $config, 'format', '' ) )
94
			->setArguments( $this->sanitize( $params ) )
95
			->setSection( join( '/', $trailing ) );
96
97
		if( (bool) $this->getValue( $config, 'BE', false ) === true ) {
98
			return (string) $this->uriBuilder->buildBackendUri();
99
		}
100
101
		$url = (string) $this->uriBuilder->buildFrontendUri();
102
		return $useCHash ? $url : preg_replace( '/\&cHash=[0-9a-f]{32}/', '', $url ); // wokaround for bad TYPO3 behavior
103
	}
104
105
106
	/**
107
	 * Returns the sanitized configuration values.
108
	 *
109
	 * @param array $config Associative list of key/value pairs
110
	 * @param string $key Key of the value to retrieve
111
	 * @param mixed $default Default value if value for key isn't found
112
	 * @return mixed Configuration value for the given key or default value
113
	 */
114
	protected function getValue( array $config, string $key, $default = null )
115
	{
116
		if( isset( $config[$key] ) ) {
117
			return $config[$key];
118
		}
119
120
		return $default;
121
	}
122
}
123