Completed
Push — master ( 24633a...ec1538 )
by Aimeos
11:26
created

View   C

Complexity

Total Complexity 24

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 19
dl 0
loc 266
rs 6.875
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 23 1
A addAccess() 0 7 1
A addConfig() 0 8 1
A addCsrf() 0 11 2
A addNumber() 0 11 1
A addParam() 0 8 2
B addRequest() 0 18 7
A addResponse() 0 7 1
A addSession() 0 7 1
A addTranslate() 0 17 2
B addUrl() 0 28 5
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Base
8
 */
9
10
11
namespace Aimeos\Shop\Base;
12
13
use Neos\Flow\Annotations as Flow;
14
15
16
/**
17
 * Class providing the view objects
18
 *
19
 * @package flow
20
 * @subpackage Base
21
 * @Flow\Scope("singleton")
22
 */
23
class View
24
{
25
	/**
26
	 * @var \Aimeos\Shop\Base\I18n
27
	 * @Flow\Inject
28
	 */
29
	protected $i18n;
30
31
	/**
32
	 * @var \Neos\FluidAdaptor\View\StandaloneView
33
	 * @Flow\Inject
34
	 */
35
	protected $view;
36
37
	/**
38
	 * @Flow\Inject
39
	 * @var \Neos\Flow\Security\Context
40
	 */
41
	protected $security;
42
43
44
	/**
45
	 * Creates the view object for the HTML client.
46
	 *
47
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
48
	 * @param \Neos\Flow\Mvc\Routing\UriBuilder $uriBuilder URL builder object
49
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
50
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
51
	 * @param string|null $langid Language ID
52
	 * @return \Aimeos\MW\View\Iface View object
53
	 */
54
	public function create( \Aimeos\MShop\Context\Item\Iface $context,
55
		\Neos\Flow\Mvc\Routing\UriBuilder $uriBuilder, array $templatePaths,
56
		\Neos\Flow\Mvc\RequestInterface $request = null, $langid = null )
57
	{
58
		$engines = array( '.html' => new \Aimeos\MW\View\Engine\Flow( $this->view ) );
59
		$view = new \Aimeos\MW\View\Standard( $templatePaths, $engines );
60
61
		$config = $context->getConfig();
62
		$session = $context->getSession();
63
64
		$this->addCsrf( $view );
65
		$this->addAccess( $view, $context );
66
		$this->addConfig( $view, $config );
67
		$this->addNumber( $view, $config );
68
		$this->addParam( $view, $request );
69
		$this->addRequest( $view, $request );
70
		$this->addResponse( $view );
71
		$this->addSession( $view, $session );
72
		$this->addTranslate( $view, $langid );
73
		$this->addUrl( $view, $uriBuilder, $request );
74
75
		return $view;
76
	}
77
78
79
	/**
80
	 * Adds the "access" helper to the view object
81
	 *
82
	 * @param \Aimeos\MW\View\Iface $view View object
83
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
84
	 * @return \Aimeos\MW\View\Iface Modified view object
85
	 */
86
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
0 ignored issues
show
Unused Code introduced by
The parameter $context 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...
87
	{
88
		$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
89
		$view->addHelper( 'access', $helper );
90
91
		return $view;
92
	}
93
94
95
	/**
96
	 * Adds the "config" helper to the view object
97
	 *
98
	 * @param \Aimeos\MW\View\Iface $view View object
99
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
100
	 * @return \Aimeos\MW\View\Iface Modified view object
101
	 */
102
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
103
	{
104
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
105
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
106
		$view->addHelper( 'config', $helper );
107
108
		return $view;
109
	}
110
111
112
	/**
113
	 * Adds the "access" helper to the view object
114
	 *
115
	 * @param \Aimeos\MW\View\Iface $view View object
116
	 * @return \Aimeos\MW\View\Iface Modified view object
117
	 */
118
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
119
	{
120
		if( $this->security->canBeInitialized() )
121
		{
122
			$token = $this->security->getCsrfProtectionToken();
123
			$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '__csrfToken', $token );
124
			$view->addHelper( 'csrf', $helper );
125
		}
126
127
		return $view;
128
	}
129
130
131
	/**
132
	 * Adds the "number" helper to the view object
133
	 *
134
	 * @param \Aimeos\MW\View\Iface $view View object
135
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
136
	 * @return \Aimeos\MW\View\Iface Modified view object
137
	 */
138
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
139
	{
140
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
141
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
142
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
143
144
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
145
		$view->addHelper( 'number', $helper );
146
147
		return $view;
148
	}
149
150
151
	/**
152
	 * Adds the "param" helper to the view object
153
	 *
154
	 * @param \Aimeos\MW\View\Iface $view View object
155
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
156
	 * @return \Aimeos\MW\View\Iface Modified view object
157
	 */
158
	protected function addParam( \Aimeos\MW\View\Iface $view, \Neos\Flow\Mvc\RequestInterface $request = null )
159
	{
160
		$params = ( $request !== null ? $request->getArguments() : array() );
161
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
162
		$view->addHelper( 'param', $helper );
163
164
		return $view;
165
	}
166
167
168
	/**
169
	 * Adds the "request" helper to the view object
170
	 *
171
	 * @param \Aimeos\MW\View\Iface $view View object
172
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
173
	 * @return \Aimeos\MW\View\Iface Modified view object
174
	 */
175
	protected function addRequest( \Aimeos\MW\View\Iface $view, \Neos\Flow\Mvc\RequestInterface $request = null )
176
	{
177
		if( $request !== null )
178
		{
179
			$req = $request->getHttpRequest();
180
181
			$files = ( is_array( $_FILES ) ? $_FILES : array() );
182
			$query = ( is_array( $_GET ) ? $_GET : array() );
183
			$post = ( is_array( $_POST ) ? $_POST : array() );
184
			$cookie = ( is_array( $_COOKIE ) ? $_COOKIE : array() );
185
			$server = ( is_array( $_SERVER ) ? $_SERVER : array() );
186
187
			$helper = new \Aimeos\MW\View\Helper\Request\Flow( $view, $req, $files, $query, $post, $cookie, $server );
188
			$view->addHelper( 'request', $helper );
189
		}
190
191
		return $view;
192
	}
193
194
195
	/**
196
	 * Adds the "response" helper to the view object
197
	 *
198
	 * @param \Aimeos\MW\View\Iface $view View object
199
	 * @return \Aimeos\MW\View\Iface Modified view object
200
	 */
201
	protected function addResponse( \Aimeos\MW\View\Iface $view )
202
	{
203
		$helper = new \Aimeos\MW\View\Helper\Response\Flow( $view );
204
		$view->addHelper( 'response', $helper );
205
206
		return $view;
207
	}
208
209
210
	/**
211
	 * Adds the "session" helper to the view object
212
	 *
213
	 * @param \Aimeos\MW\View\Iface $view View object
214
	 * @param \Aimeos\MW\Session\Iface $session Session object
215
	 * @return \Aimeos\MW\View\Iface Modified view object
216
	 */
217
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session )
218
	{
219
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
220
		$view->addHelper( 'session', $helper );
221
222
		return $view;
223
	}
224
225
226
	/**
227
	 * Adds the "translate" helper to the view object
228
	 *
229
	 * @param \Aimeos\MW\View\Iface $view View object
230
	 * @param string|null $langid ISO language code, e.g. "de" or "de_CH"
231
	 * @return \Aimeos\MW\View\Iface Modified view object
232
	 */
233
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $langid )
234
	{
235
		if( $langid !== null )
236
		{
237
			$i18n = $this->i18n->get( array( $langid ) );
238
			$translation = $i18n[$langid];
239
		}
240
		else
241
		{
242
			$translation = new \Aimeos\MW\Translation\None( 'en' );
243
		}
244
245
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
246
		$view->addHelper( 'translate', $helper );
247
248
		return $view;
249
	}
250
251
252
	/**
253
	 * Adds the "url" helper to the view object
254
	 *
255
	 * @param \Aimeos\MW\View\Iface $view View object
256
	 * @param \Neos\Flow\Mvc\Routing\UriBuilder $uriBuilder URL builder object
257
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
258
	 * @return \Aimeos\MW\View\Iface Modified view object
259
	 */
260
	protected function addUrl( \Aimeos\MW\View\Iface $view,
261
		\Neos\Flow\Mvc\Routing\UriBuilder $uriBuilder,
262
		\Neos\Flow\Mvc\RequestInterface $request = null )
263
	{
264
		$fixed = array();
265
266
		if( $request !== null )
267
		{
268
			$params = $request->getArguments();
269
270
			if( isset( $params['site'] ) ) {
271
				$fixed['site'] = $params['site'];
272
			}
273
274
			if( isset( $params['locale'] ) ) {
275
				$fixed['locale'] = $params['locale'];
276
			}
277
278
			if( isset( $params['currency'] ) ) {
279
				$fixed['currency'] = $params['currency'];
280
			}
281
		}
282
283
		$helper = new \Aimeos\MW\View\Helper\Url\Flow( $view, $uriBuilder, $fixed );
284
		$view->addHelper( 'url', $helper );
285
286
		return $view;
287
	}
288
}