View::addParam()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
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, $langid );
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 )
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, ['admin', 'client', 'resource/fs/baseurl'] );
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
	 * @param string|null $locale Code of the current language or null for no translation
137
	 * @return \Aimeos\MW\View\Iface Modified view object
138
	 */
139
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config, $locale )
140
	{
141
		$pattern = $config->get( 'client/html/common/format/pattern' );
142
143
		$helper = new \Aimeos\MW\View\Helper\Number\Locale( $view, $locale, $pattern );
144
		$view->addHelper( 'number', $helper );
145
146
		return $view;
147
	}
148
149
150
	/**
151
	 * Adds the "param" helper to the view object
152
	 *
153
	 * @param \Aimeos\MW\View\Iface $view View object
154
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
155
	 * @return \Aimeos\MW\View\Iface Modified view object
156
	 */
157
	protected function addParam( \Aimeos\MW\View\Iface $view, \Neos\Flow\Mvc\RequestInterface $request = null )
158
	{
159
		$params = ( $request !== null ? $request->getArguments() + ['action' => $request->getControllerActionName()] : [] );
0 ignored issues
show
Bug introduced by
The method getControllerActionName() does not exist on Neos\Flow\Mvc\RequestInterface. Did you maybe mean getControllerObjectName()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
		$params = ( $request !== null ? $request->getArguments() + ['action' => $request->/** @scrutinizer ignore-call */ getControllerActionName()] : [] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
161
		$view->addHelper( 'param', $helper );
162
163
		return $view;
164
	}
165
166
167
	/**
168
	 * Adds the "request" helper to the view object
169
	 *
170
	 * @param \Aimeos\MW\View\Iface $view View object
171
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
172
	 * @return \Aimeos\MW\View\Iface Modified view object
173
	 */
174
	protected function addRequest( \Aimeos\MW\View\Iface $view, \Neos\Flow\Mvc\RequestInterface $request = null )
175
	{
176
		if( $request !== null )
177
		{
178
			$req = $request->getHttpRequest();
0 ignored issues
show
Bug introduced by
The method getHttpRequest() does not exist on Neos\Flow\Mvc\RequestInterface. It seems like you code against a sub-type of Neos\Flow\Mvc\RequestInterface such as Neos\Flow\Mvc\ActionRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
			/** @scrutinizer ignore-call */ 
179
   $req = $request->getHttpRequest();
Loading history...
179
180
			$files = ( is_array( $_FILES ) ? $_FILES : array() );
181
			$query = ( is_array( $_GET ) ? $_GET : array() );
182
			$post = ( is_array( $_POST ) ? $_POST : array() );
183
			$cookie = ( is_array( $_COOKIE ) ? $_COOKIE : array() );
184
			$server = ( is_array( $_SERVER ) ? $_SERVER : array() );
185
186
			$helper = new \Aimeos\MW\View\Helper\Request\Flow( $view, $req, $files, $query, $post, $cookie, $server );
187
			$view->addHelper( 'request', $helper );
188
		}
189
190
		return $view;
191
	}
192
193
194
	/**
195
	 * Adds the "response" helper to the view object
196
	 *
197
	 * @param \Aimeos\MW\View\Iface $view View object
198
	 * @return \Aimeos\MW\View\Iface Modified view object
199
	 */
200
	protected function addResponse( \Aimeos\MW\View\Iface $view )
201
	{
202
		$helper = new \Aimeos\MW\View\Helper\Response\Flow( $view );
203
		$view->addHelper( 'response', $helper );
204
205
		return $view;
206
	}
207
208
209
	/**
210
	 * Adds the "session" helper to the view object
211
	 *
212
	 * @param \Aimeos\MW\View\Iface $view View object
213
	 * @param \Aimeos\MW\Session\Iface $session Session object
214
	 * @return \Aimeos\MW\View\Iface Modified view object
215
	 */
216
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session )
217
	{
218
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
219
		$view->addHelper( 'session', $helper );
220
221
		return $view;
222
	}
223
224
225
	/**
226
	 * Adds the "translate" helper to the view object
227
	 *
228
	 * @param \Aimeos\MW\View\Iface $view View object
229
	 * @param string|null $langid ISO language code, e.g. "de" or "de_CH"
230
	 * @return \Aimeos\MW\View\Iface Modified view object
231
	 */
232
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $langid )
233
	{
234
		if( $langid !== null )
235
		{
236
			$i18n = $this->i18n->get( array( $langid ) );
237
			$translation = $i18n[$langid];
238
		}
239
		else
240
		{
241
			$translation = new \Aimeos\MW\Translation\None( 'en' );
242
		}
243
244
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
245
		$view->addHelper( 'translate', $helper );
246
247
		return $view;
248
	}
249
250
251
	/**
252
	 * Adds the "url" helper to the view object
253
	 *
254
	 * @param \Aimeos\MW\View\Iface $view View object
255
	 * @param \Neos\Flow\Mvc\Routing\UriBuilder $uriBuilder URL builder object
256
	 * @param \Neos\Flow\Mvc\RequestInterface|null $request Request object
257
	 * @return \Aimeos\MW\View\Iface Modified view object
258
	 */
259
	protected function addUrl( \Aimeos\MW\View\Iface $view,
260
		\Neos\Flow\Mvc\Routing\UriBuilder $uriBuilder,
261
		\Neos\Flow\Mvc\RequestInterface $request = null )
262
	{
263
		$fixed = array();
264
265
		if( $request !== null )
266
		{
267
			$params = $request->getArguments();
268
269
			if( isset( $params['site'] ) ) {
270
				$fixed['site'] = $params['site'];
271
			}
272
273
			if( isset( $params['locale'] ) ) {
274
				$fixed['locale'] = $params['locale'];
275
			}
276
277
			if( isset( $params['currency'] ) ) {
278
				$fixed['currency'] = $params['currency'];
279
			}
280
		}
281
282
		$helper = new \Aimeos\MW\View\Helper\Url\Flow( $view, $uriBuilder, $fixed );
283
		$view->addHelper( 'url', $helper );
284
285
		return $view;
286
	}
287
}