Completed
Push — master ( 22fee6...a6e7a8 )
by Aimeos
02:32
created

View::getGroups()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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