Passed
Push — master ( 48d2af...a4afe2 )
by Aimeos
11:39
created

View::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 3
dl 0
loc 23
rs 9.7
c 10
b 0
f 0
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package symfony
7
 * @subpackage Service
8
 */
9
10
namespace Aimeos\ShopBundle\Service;
11
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
use Symfony\Component\DependencyInjection\Container;
15
16
17
/**
18
 * Service providing the view objects
19
 *
20
 * @package symfony
21
 * @subpackage Service
22
 */
23
class View
24
{
25
	private $requestStack;
26
	private $container;
27
28
29
	/**
30
	 * Initializes the context manager object
31
	 *
32
	 * @param RequestStack $requestStack Current request stack
33
	 * @param Container $container Container object to access parameters
34
	 */
35
	public function __construct( RequestStack $requestStack, Container $container )
36
	{
37
		$this->requestStack = $requestStack;
38
		$this->container = $container;
39
	}
40
41
42
	/**
43
	 * Creates the view object for the HTML client.
44
	 *
45
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
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\MShop\Context\Item\Iface $context, array $templatePaths, $locale = null )
51
	{
52
		$twig = $this->container->get( 'twig' );
53
		$engine = new \Aimeos\MW\View\Engine\Twig( $twig );
0 ignored issues
show
Bug introduced by
It seems like $twig can also be of type null; however, parameter $env of Aimeos\MW\View\Engine\Twig::__construct() does only seem to accept Twig_Environment, maybe add an additional type check? ( Ignorable by Annotation )

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

53
		$engine = new \Aimeos\MW\View\Engine\Twig( /** @scrutinizer ignore-type */ $twig );
Loading history...
54
		$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.html.twig' => $engine ) );
55
56
		$config = $context->getConfig();
57
		$session = $context->getSession();
58
59
		$this->addCsrf( $view );
60
		$this->addAccess( $view, $context );
61
		$this->addConfig( $view, $config );
62
		$this->addNumber( $view, $config, $locale );
63
		$this->addParam( $view );
64
		$this->addRequest( $view );
65
		$this->addResponse( $view );
66
		$this->addSession( $view, $session );
67
		$this->addTranslate( $view, $locale );
68
		$this->addUrl( $view );
69
70
		$this->initTwig( $view, $twig );
0 ignored issues
show
Bug introduced by
It seems like $twig can also be of type null; however, parameter $twig of Aimeos\ShopBundle\Service\View::initTwig() does only seem to accept Twig_Environment, maybe add an additional type check? ( Ignorable by Annotation )

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

70
		$this->initTwig( $view, /** @scrutinizer ignore-type */ $twig );
Loading history...
71
72
		return $view;
73
	}
74
75
76
	/**
77
	 * Adds the "access" helper to the view object
78
	 *
79
	 * @param \Aimeos\MW\View\Iface $view View object
80
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
81
	 * @return \Aimeos\MW\View\Iface Modified view object
82
	 */
83
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
84
	{
85
		$container = $this->container;
86
		$token = $this->container->get( 'security.token_storage' )->getToken();
87
88
		if( is_object( $token ) && is_object( $token->getUser() )
89
			&& in_array( 'ROLE_SUPER_ADMIN', (array) $token->getUser()->getRoles() ) )
90
		{
91
			$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
92
		}
93
		else
94
		{
95
			$fcn = function() use ( $container, $context ) {
96
				return $container->get( 'aimeos.support' )->getGroups( $context );
97
			};
98
99
			$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
100
		}
101
102
		$view->addHelper( 'access', $helper );
103
104
		return $view;
105
	}
106
107
108
	/**
109
	 * Adds the "config" helper to the view object
110
	 *
111
	 * @param \Aimeos\MW\View\Iface $view View object
112
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
113
	 * @return \Aimeos\MW\View\Iface Modified view object
114
	 */
115
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
116
	{
117
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, ['admin', 'client', 'resource/fs/baseurl'] );
118
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
119
		$view->addHelper( 'config', $helper );
120
121
		return $view;
122
	}
123
124
125
	/**
126
	 * Adds the "access" helper to the view object
127
	 *
128
	 * @param \Aimeos\MW\View\Iface $view View object
129
	 * @return \Aimeos\MW\View\Iface Modified view object
130
	 */
131
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
132
	{
133
		$token = $this->container->get( 'security.csrf.token_manager' )->getToken( '_token' );
134
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', $token->getValue() );
135
		$view->addHelper( 'csrf', $helper );
136
137
		return $view;
138
	}
139
140
141
	/**
142
	 * Adds the "number" helper to the view object
143
	 *
144
	 * @param \Aimeos\MW\View\Iface $view View object
145
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
146
	 * @param string|null $locale Code of the current language or null for no translation
147
	 * @return \Aimeos\MW\View\Iface Modified view object
148
	 */
149
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config, $locale )
150
	{
151
		$pattern = $config->get( 'client/html/common/format/pattern' );
152
153
		$helper = new \Aimeos\MW\View\Helper\Number\Locale( $view, $locale, $pattern );
154
		$view->addHelper( 'number', $helper );
155
156
		return $view;
157
	}
158
159
160
	/**
161
	 * Adds the "param" helper to the view object
162
	 *
163
	 * @param \Aimeos\MW\View\Iface $view View object
164
	 * @return \Aimeos\MW\View\Iface Modified view object
165
	 */
166
	protected function addParam( \Aimeos\MW\View\Iface $view )
167
	{
168
		$params = array();
169
		$request = $this->requestStack->getMasterRequest();
170
171
		if( $request !== null ) {
172
			$params = $request->request->all() + $request->query->all() + $request->attributes->get( '_route_params' );
173
		}
174
175
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
176
		$view->addHelper( 'param', $helper );
177
178
		return $view;
179
	}
180
181
182
	/**
183
	 * Adds the "request" helper to the view object
184
	 *
185
	 * @param \Aimeos\MW\View\Iface $view View object
186
	 * @return \Aimeos\MW\View\Iface Modified view object
187
	 */
188
	protected function addRequest( \Aimeos\MW\View\Iface $view )
189
	{
190
		$request = $this->requestStack->getMasterRequest();
191
192
		if( $request !== null )
193
		{
194
			$helper = new \Aimeos\MW\View\Helper\Request\Symfony2( $view, $request );
195
			$view->addHelper( 'request', $helper );
196
		}
197
198
		return $view;
199
	}
200
201
202
	/**
203
	 * Adds the "response" helper to the view object
204
	 *
205
	 * @param \Aimeos\MW\View\Iface $view View object
206
	 * @return \Aimeos\MW\View\Iface Modified view object
207
	 */
208
	protected function addResponse( \Aimeos\MW\View\Iface $view )
209
	{
210
		$helper = new \Aimeos\MW\View\Helper\Response\Symfony2( $view );
211
		$view->addHelper( 'response', $helper );
212
213
		return $view;
214
	}
215
216
217
	/**
218
	 * Adds the "session" helper to the view object
219
	 *
220
	 * @param \Aimeos\MW\View\Iface $view View object
221
	 * @param \Aimeos\MW\Session\Iface $session Session object
222
	 * @return \Aimeos\MW\View\Iface Modified view object
223
	 */
224
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session )
225
	{
226
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
227
		$view->addHelper( 'session', $helper );
228
229
		return $view;
230
	}
231
232
233
	/**
234
	 * Adds the "translate" helper to the view object
235
	 *
236
	 * @param \Aimeos\MW\View\Iface $view View object
237
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
238
	 * @return \Aimeos\MW\View\Iface Modified view object
239
	 */
240
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale )
241
	{
242
		if( $locale !== null )
243
		{
244
			$i18n = $this->container->get( 'aimeos.i18n' )->get( array( $locale ) );
245
			$translation = $i18n[$locale];
246
		}
247
		else
248
		{
249
			$translation = new \Aimeos\MW\Translation\None( 'en' );
250
		}
251
252
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
253
		$view->addHelper( 'translate', $helper );
254
255
		return $view;
256
	}
257
258
259
	/**
260
	 * Adds the "url" helper to the view object
261
	 *
262
	 * @param \Aimeos\MW\View\Iface $view View object
263
	 * @return \Aimeos\MW\View\Iface Modified view object
264
	 */
265
	protected function addUrl( \Aimeos\MW\View\Iface $view )
266
	{
267
		$fixed = array();
268
		$request = $this->requestStack->getMasterRequest();
269
270
		if( $request !== null )
271
		{
272
			$attr = $request->attributes;
273
274
			if( ( $site = $attr->get( 'site' ) ) !== null ) {
275
				$fixed['site'] = $site;
276
			}
277
278
			if( ( $lang = $attr->get( 'locale' ) ) !== null ) {
279
				$fixed['locale'] = $lang;
280
			}
281
282
			if( ( $currency = $attr->get( 'currency' ) ) !== null ) {
283
				$fixed['currency'] = $currency;
284
			}
285
		}
286
287
		$helper = new \Aimeos\MW\View\Helper\Url\Symfony2( $view, $this->container->get( 'router' ), $fixed );
0 ignored issues
show
Bug introduced by
It seems like $this->container->get('router') can also be of type null; however, parameter $router of Aimeos\MW\View\Helper\Url\Symfony2::__construct() does only seem to accept Symfony\Component\Routing\RouterInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

287
		$helper = new \Aimeos\MW\View\Helper\Url\Symfony2( $view, /** @scrutinizer ignore-type */ $this->container->get( 'router' ), $fixed );
Loading history...
288
		$view->addHelper( 'url', $helper );
289
290
		return $view;
291
	}
292
293
294
	/**
295
	 * Adds the Aimeos template functions for Twig
296
	 *
297
	 * @param \Aimeos\MW\View\Iface $view View object
298
	 * @param \Twig_Environment $twig Twig environment object
299
	 */
300
	protected function initTwig( \Aimeos\MW\View\Iface $view, \Twig_Environment $twig )
301
	{
302
		$fcn = function( $key, $default = null ) use ( $view ) {
303
			return $view->config( $key, $default );
304
		};
305
		$twig->addFunction( new \Twig_SimpleFunction( 'aiconfig', $fcn ) );
306
307
		$fcn = function( $singular, array $values = array(), $domain = 'client' ) use ( $view ) {
308
			return vsprintf( $view->translate( $domain, $singular ), $values );
309
		};
310
		$twig->addFunction( new \Twig_SimpleFunction( 'aitrans', $fcn ) );
311
312
		$fcn = function( $singular, $plural, $number, array $values = array(), $domain = 'client' ) use ( $view ) {
313
			return vsprintf( $view->translate( $domain, $singular, $plural, $number ), $values );
314
		};
315
		$twig->addFunction( new \Twig_SimpleFunction( 'aitransplural', $fcn ) );
316
	}
317
}
318