Passed
Push — master ( fa79ba...4fcbee )
by Aimeos
08:39 queued 05:58
created

Typo3::transform()   B

Complexity

Conditions 9
Paths 48

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 48
nop 6
dl 0
loc 37
rs 8.0555
c 0
b 0
f 0
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-2022
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 $uriBuilder;
26
	private $prefix;
27
	private $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
		$params['controller'] = $controller !== null ? ucfirst( $controller ) : null;
63
		$params['action'] = $action;
64
65
		if( $this->prefix != '' && (bool) $this->getValue( $config, 'namespace', true ) === true ) {
66
			$params = [$this->prefix => $params];
67
		}
68
		$params += $this->fixed;
69
70
		if( ( $eid = $this->getValue( $config, 'eID' ) ) !== null ) {
71
			$params['eID'] = $eid;
72
		}
73
74
		if( $locale !== null ) {
75
			$params['L'] = $locale;
76
		}
77
78
		$useCHash = (bool) $this->getValue( $config, 'chash', false );
79
80
		$this->uriBuilder->reset()
81
			->setTargetPageUid( (int) ( $target ?: ( $GLOBALS['TSFE']->id ?? 0 ) ) )
82
			->setCreateAbsoluteUri( (bool) $this->getValue( $config, 'absoluteUri', false ) )
83
			->setTargetPageType( (int) $this->getValue( $config, 'type', 0 ) )
84
			->setNoCache( (bool) $this->getValue( $config, 'nocache', false ) )
85
			->setFormat( (string) $this->getValue( $config, 'format', '' ) )
86
			->setArguments( $this->sanitize( $params ) )
87
			->setSection( join( '/', $trailing ) );
88
89
		if( (bool) $this->getValue( $config, 'BE', false ) === true ) {
90
			return (string) $this->uriBuilder->buildBackendUri();
91
		}
92
93
		$url = (string) $this->uriBuilder->buildFrontendUri();
94
		return $useCHash ? $url : preg_replace( '/\&cHash=[0-9a-f]{32}/', '', $url );
95
	}
96
97
98
	/**
99
	 * Returns the sanitized configuration values.
100
	 *
101
	 * @param array $config Associative list of key/value pairs
102
	 * @param string $key Key of the value to retrieve
103
	 * @param mixed $default Default value if value for key isn't found
104
	 * @return mixed Configuration value for the given key or default value
105
	 */
106
	protected function getValue( array $config, string $key, $default = null )
107
	{
108
		if( isset( $config[$key] ) ) {
109
			return $config[$key];
110
		}
111
112
		return $default;
113
	}
114
}
115