Completed
Push — master ( ad6ad2...4f1a9d )
by Aimeos
02:36
created

View::addUrl()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 8
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Slim\Base;
11
12
use Interop\Container\ContainerInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
17
/**
18
 * Service providing the view objects
19
 *
20
 * @package Slim
21
 * @subpackage Base
22
 */
23
class View
24
{
25
	private $container;
26
27
28
	/**
29
	 * Initializes the object
30
	 *
31
	 * @param ContainerInterface $container Dependency container
32
	 */
33
	public function __construct( ContainerInterface $container )
34
	{
35
		$this->container = $container;
36
	}
37
38
39
	/**
40
	 * Creates the view object for the HTML client.
41
	 *
42
	 * @param \Aimeos\MW\Config\Iface $config Config object
43
	 * @param ServerRequestInterface $request Request object
44
	 * @param ResponseInterface $response Response object
45
	 * @param array $attributes Associative list of URI parameters
46
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
47
	 * @param string|null $locale Code of the current language or null for no translation
48
	 * @return \Aimeos\MW\View\Iface View object
49
	 */
50
	public function create( \Aimeos\MW\Config\Iface $config, ServerRequestInterface $request,
51
		ResponseInterface $response, array $attributes, array $templatePaths, $locale = null )
52
	{
53
		$params = $attributes + (array) $request->getParsedBody() + (array) $request->getQueryParams();
54
55
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
56
57
		$this->addAccess( $view );
58
		$this->addConfig( $view, $config );
59
		$this->addNumber( $view, $config );
60
		$this->addRequest( $view, $request );
61
		$this->addResponse( $view, $response );
62
		$this->addParam( $view, $params );
63
		$this->addUrl( $view, $attributes );
64
		$this->addCsrf( $view, $request );
65
		$this->addTranslate( $view, $config, $locale );
66
67
		return $view;
68
	}
69
70
71
	/**
72
	 * Adds the "access" helper to the view object
73
	 *
74
	 * @param \Aimeos\MW\View\Iface $view View object
75
	 * @return \Aimeos\MW\View\Iface Modified view object
76
	 */
77
	protected function addAccess( \Aimeos\MW\View\Iface $view )
78
	{
79
		$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
80
		$view->addHelper( 'access', $helper );
81
82
		return $view;
83
	}
84
85
86
	/**
87
	 * Adds the "config" helper to the view object
88
	 *
89
	 * @param \Aimeos\MW\View\Iface $view View object
90
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
91
	 * @return \Aimeos\MW\View\Iface Modified view object
92
	 */
93
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
94
	{
95
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
96
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
97
		$view->addHelper( 'config', $helper );
98
99
		return $view;
100
	}
101
102
103
	/**
104
	 * Adds the "access" helper to the view object
105
	 *
106
	 * @param \Aimeos\MW\View\Iface $view View object
107
	 * @param ServerRequestInterface $request Request object
108
	 * @return \Aimeos\MW\View\Iface Modified view object
109
	 */
110
	protected function addCsrf( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request )
111
	{
112
		$name = $request->getAttribute( 'csrf_name' );
113
		$value = $request->getAttribute( 'csrf_value' );
114
115
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, $name, $value );
116
		$view->addHelper( 'csrf', $helper );
117
118
		return $view;
119
	}
120
121
122
	/**
123
	 * Adds the "number" helper to the view object
124
	 *
125
	 * @param \Aimeos\MW\View\Iface $view View object
126
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
127
	 * @return \Aimeos\MW\View\Iface Modified view object
128
	 */
129
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
130
	{
131
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
132
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
133
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
134
135
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
136
		$view->addHelper( 'number', $helper );
137
138
		return $view;
139
	}
140
141
142
	/**
143
	 * Adds the "param" helper to the view object
144
	 *
145
	 * @param \Aimeos\MW\View\Iface $view View object
146
	 * @param array $attributes Associative list of request parameters
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
147
	 * @return \Aimeos\MW\View\Iface Modified view object
148
	 */
149
	protected static function addParam( \Aimeos\MW\View\Iface $view, array $params )
150
	{
151
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
152
		$view->addHelper( 'param', $helper );
153
154
		return $view;
155
	}
156
157
158
	/**
159
	 * Adds the "request" helper to the view object
160
	 *
161
	 * @param \Aimeos\MW\View\Iface $view View object
162
	 * @param ServerRequestInterface $request Request object
163
	 * @return \Aimeos\MW\View\Iface Modified view object
164
	 */
165
	protected static function addRequest( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request )
166
	{
167
		$helper = new \Aimeos\MW\View\Helper\Request\Slim( $view, $request );
168
		$view->addHelper( 'request', $helper );
169
170
		return $view;
171
	}
172
173
174
	/**
175
	 * Adds the "response" helper to the view object
176
	 *
177
	 * @param \Aimeos\MW\View\Iface $view View object
178
	 * @param ResponseInterface $response Response object
179
	 * @return \Aimeos\MW\View\Iface Modified view object
180
	 */
181
	protected static function addResponse( \Aimeos\MW\View\Iface $view, ResponseInterface $response )
182
	{
183
		$helper = new \Aimeos\MW\View\Helper\Response\Slim( $view, $response );
184
		$view->addHelper( 'response', $helper );
185
186
		return $view;
187
	}
188
189
190
	/**
191
	 * Adds the "url" helper to the view object
192
	 *
193
	 * @param \Aimeos\MW\View\Iface $view View object
194
	 * @param array $attributes Associative list of URI parameters
195
	 * @return \Aimeos\MW\View\Iface Modified view object
196
	 */
197
	protected function addUrl( \Aimeos\MW\View\Iface $view, array $attributes )
198
	{
199
		$fixed = array();
200
201
		if( isset( $attributes['site'] ) ) {
202
			$fixed['site'] = $attributes['site'];
203
		}
204
205
		if( isset( $attributes['locale'] ) ) {
206
			$fixed['locale'] = $attributes['locale'];
207
		}
208
209
		if( isset( $attributes['currency'] ) ) {
210
			$fixed['currency'] = $attributes['currency'];
211
		}
212
213
		$helper = new \Aimeos\MW\View\Helper\Url\Slim( $view, $this->container->get( 'router' ), $fixed );
214
		$view->addHelper( 'url', $helper );
215
216
		return $view;
217
	}
218
219
220
	/**
221
	 * Adds the "translate" helper to the view object
222
	 *
223
	 * @param \Aimeos\MW\View\Iface $view View object
224
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
225
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
226
	 * @return \Aimeos\MW\View\Iface Modified view object
227
	 */
228
	protected function addTranslate( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config, $locale )
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
229
	{
230
		if( $locale !== null )
231
		{
232
			$i18n = $this->container->get( 'aimeos_i18n' )->get( array( $locale ) );
233
			$translation = $i18n[$locale];
234
		}
235
		else
236
		{
237
			$translation = new \Aimeos\MW\Translation\None( 'en' );
238
		}
239
240
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
241
		$view->addHelper( 'translate', $helper );
242
243
		return $view;
244
	}
245
}
246