Completed
Push — master ( 5b2742...9e7f7e )
by Aimeos
02:35
created

View::addAccess()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 12
nc 2
nop 2
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
		$config = $context->getConfig();
53
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
54
55
		$this->addCsrf( $view );
56
		$this->addAccess( $view, $context );
57
		$this->addConfig( $view, $config );
58
		$this->addNumber( $view, $config );
59
		$this->addParam( $view );
60
		$this->addRequest( $view );
61
		$this->addResponse( $view );
62
		$this->addTranslate( $view, $locale );
63
		$this->addUrl( $view );
64
65
		return $view;
66
	}
67
68
69
	/**
70
	 * Adds the "access" helper to the view object
71
	 *
72
	 * @param \Aimeos\MW\View\Iface $view View object
73
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
74
	 * @return \Aimeos\MW\View\Iface Modified view object
75
	 */
76
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
77
	{
78
		$container = $this->container;
79
		$token = $this->container->get( 'security.token_storage' )->getToken();
80
81
		if( is_object( $token ) && is_object( $token->getUser() )
82
			&& in_array( 'ROLE_ADMIN', (array) $token->getUser()->getRoles() ) )
83
		{
84
			$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
85
		}
86
		else
87
		{
88
			$fcn = function() use ( $container, $context ) {
89
				return $container->get( 'aimeos_support' )->getGroups( $context );
90
			};
91
92
			$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
93
		}
94
95
		$view->addHelper( 'access', $helper );
96
97
		return $view;
98
	}
99
100
101
	/**
102
	 * Adds the "config" helper to the view object
103
	 *
104
	 * @param \Aimeos\MW\View\Iface $view View object
105
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
106
	 * @return \Aimeos\MW\View\Iface Modified view object
107
	 */
108
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
109
	{
110
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
111
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
112
		$view->addHelper( 'config', $helper );
113
114
		return $view;
115
	}
116
117
118
	/**
119
	 * Adds the "access" helper to the view object
120
	 *
121
	 * @param \Aimeos\MW\View\Iface $view View object
122
	 * @return \Aimeos\MW\View\Iface Modified view object
123
	 */
124
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
125
	{
126
		$token = $this->container->get( 'security.csrf.token_manager' )->getToken( '_token' );
127
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', $token->getValue() );
128
		$view->addHelper( 'csrf', $helper );
129
130
		return $view;
131
	}
132
133
134
	/**
135
	 * Adds the "number" helper to the view object
136
	 *
137
	 * @param \Aimeos\MW\View\Iface $view View object
138
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
139
	 * @return \Aimeos\MW\View\Iface Modified view object
140
	 */
141
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
142
	{
143
		$sepDec = $config->get( 'client/html/common/format/separatorDecimal', '.' );
144
		$sep1000 = $config->get( 'client/html/common/format/separator1000', ' ' );
145
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
146
147
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
148
		$view->addHelper( 'number', $helper );
149
150
		return $view;
151
	}
152
153
154
	/**
155
	 * Adds the "param" helper to the view object
156
	 *
157
	 * @param \Aimeos\MW\View\Iface $view View object
158
	 * @return \Aimeos\MW\View\Iface Modified view object
159
	 */
160
	protected function addParam( \Aimeos\MW\View\Iface $view )
161
	{
162
		$params = array();
163
		$request = $this->requestStack->getMasterRequest();
164
165
		if( $request !== null ) {
166
			$params = $request->request->all() + $request->query->all() + $request->attributes->get( '_route_params' );
167
		}
168
169
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
170
		$view->addHelper( 'param', $helper );
171
172
		return $view;
173
	}
174
175
176
	/**
177
	 * Adds the "request" helper to the view object
178
	 *
179
	 * @param \Aimeos\MW\View\Iface $view View object
180
	 * @return \Aimeos\MW\View\Iface Modified view object
181
	 */
182
	protected function addRequest( \Aimeos\MW\View\Iface $view )
183
	{
184
		$request = $this->requestStack->getMasterRequest();
185
186
		if( $request !== null )
187
		{
188
			$helper = new \Aimeos\MW\View\Helper\Request\Symfony2( $view, $request );
189
			$view->addHelper( 'request', $helper );
190
		}
191
192
		return $view;
193
	}
194
195
196
	/**
197
	 * Adds the "response" helper to the view object
198
	 *
199
	 * @param \Aimeos\MW\View\Iface $view View object
200
	 * @return \Aimeos\MW\View\Iface Modified view object
201
	 */
202
	protected function addResponse( \Aimeos\MW\View\Iface $view )
203
	{
204
		$helper = new \Aimeos\MW\View\Helper\Response\Symfony2( $view );
205
		$view->addHelper( 'response', $helper );
206
207
		return $view;
208
	}
209
210
211
	/**
212
	 * Adds the "translate" helper to the view object
213
	 *
214
	 * @param \Aimeos\MW\View\Iface $view View object
215
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
216
	 * @return \Aimeos\MW\View\Iface Modified view object
217
	 */
218
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale )
219
	{
220
		if( $locale !== null )
221
		{
222
			$i18n = $this->container->get( 'aimeos_i18n' )->get( array( $locale ) );
223
			$translation = $i18n[$locale];
224
		}
225
		else
226
		{
227
			$translation = new \Aimeos\MW\Translation\None( 'en' );
228
		}
229
230
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
231
		$view->addHelper( 'translate', $helper );
232
233
		return $view;
234
	}
235
236
237
	/**
238
	 * Adds the "url" helper to the view object
239
	 *
240
	 * @param \Aimeos\MW\View\Iface $view View object
241
	 * @return \Aimeos\MW\View\Iface Modified view object
242
	 */
243
	protected function addUrl( \Aimeos\MW\View\Iface $view )
244
	{
245
		$fixed = array();
246
		$request = $this->requestStack->getMasterRequest();
247
248
		if( $request !== null )
249
		{
250
			$attr = $request->attributes;
251
252
			if( ( $site = $attr->get( 'site' ) ) !== null ) {
253
				$fixed['site'] = $site;
254
			}
255
256
			if( ( $lang = $attr->get( 'locale' ) ) !== null ) {
257
				$fixed['locale'] = $lang;
258
			}
259
260
			if( ( $currency = $attr->get( 'currency' ) ) !== null ) {
261
				$fixed['currency'] = $currency;
262
			}
263
		}
264
265
		$helper = new \Aimeos\MW\View\Helper\Url\Symfony2( $view, $this->container->get( 'router' ), $fixed );
266
		$view->addHelper( 'url', $helper );
267
268
		return $view;
269
	}
270
}
271