Typo3::transform()   F
last analyzed

Complexity

Conditions 13
Paths 432

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 28
c 5
b 0
f 0
dl 0
loc 48
rs 3.2388
cc 13
nc 432
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-2025
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 && (bool) $this->getValue( $config, 'BE', false ) === false ) {
83
			$params['L'] = $locale;
84
		}
85
86
		$useCHash = (bool) $this->getValue( $config, 'chash', false );
87
88
		$this->uriBuilder->reset()
89
			->setCreateAbsoluteUri( (bool) $this->getValue( $config, 'absoluteUri', false ) )
90
			->setTargetPageType( (int) $this->getValue( $config, 'type', 0 ) )
91
			->setNoCache( (bool) $this->getValue( $config, 'nocache', false ) )
92
			->setFormat( (string) $this->getValue( $config, 'format', '' ) )
93
			->setArguments( $this->sanitize( $params ) )
94
			->setSection( join( '/', $trailing ) );
95
96
		if( $target ) {
97
			$this->uriBuilder->setTargetPageUid( (int) $target );
98
		}
99
100
		if( (bool) $this->getValue( $config, 'BE', false ) === true ) {
101
			return (string) $this->uriBuilder->buildBackendUri();
102
		}
103
104
		$url = (string) $this->uriBuilder->buildFrontendUri();
105
		return $useCHash ? $url : preg_replace( '/\&cHash=[0-9a-f]{32}/', '', $url ); // wokaround for bad TYPO3 behavior
106
	}
107
108
109
	/**
110
	 * Returns the sanitized configuration values.
111
	 *
112
	 * @param array $config Associative list of key/value pairs
113
	 * @param string $key Key of the value to retrieve
114
	 * @param mixed $default Default value if value for key isn't found
115
	 * @return mixed Configuration value for the given key or default value
116
	 */
117
	protected function getValue( array $config, string $key, $default = null )
118
	{
119
		if( isset( $config[$key] ) ) {
120
			return $config[$key];
121
		}
122
123
		return $default;
124
	}
125
}
126