Completed
Push — master ( e3f18b...dcdc51 )
by Aimeos
09:35
created

View::getGroups()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.4285
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
		$params = $fixed = array();
53
		$config = $context->getConfig();
54
		$request = $this->requestStack->getMasterRequest();
55
56
		if( $locale !== null )
57
		{
58
			$params = $request->request->all() + $request->query->all() + $request->attributes->get( '_route_params' );
59
			$fixed = $this->getFixedParams();
60
61
			$i18n = $this->container->get('aimeos_i18n')->get( array( $locale ) );
62
			$translation = $i18n[$locale];
63
		}
64
		else
65
		{
66
			$translation = new \Aimeos\MW\Translation\None( 'en' );
67
		}
68
69
70
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
71
72
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
73
		$view->addHelper( 'translate', $helper );
74
75
		$helper = new \Aimeos\MW\View\Helper\Url\Symfony2( $view, $this->container->get( 'router' ), $fixed );
76
		$view->addHelper( 'url', $helper );
77
78
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
79
		$view->addHelper( 'param', $helper );
80
81
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
82
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
83
		$view->addHelper( 'config', $helper );
84
85
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
86
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
87
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000 );
88
		$view->addHelper( 'number', $helper );
89
90
		if( $request !== null )
91
		{
92
			$helper = new \Aimeos\MW\View\Helper\Request\Symfony2( $view, $request );
93
			$view->addHelper( 'request', $helper );
94
		}
95
96
		$helper = new \Aimeos\MW\View\Helper\Response\Symfony2( $view );
97
		$view->addHelper( 'response', $helper );
98
99
		$token = $this->container->get( 'security.csrf.token_manager' )->getToken( '_token' );
100
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', $token->getValue() );
101
		$view->addHelper( 'csrf', $helper );
102
103
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $this->getGroups( $context ) );
104
		$view->addHelper( 'access', $helper );
105
106
		return $view;
107
	}
108
109
110
	/**
111
	 * Returns the routing parameters passed in the URL
112
	 *
113
	 * @return array Associative list of parameters with "site", "locale" and "currency" if available
114
	 */
115
	protected function getFixedParams()
116
	{
117
		$urlparams = array();
118
		$attr = $this->requestStack->getMasterRequest()->attributes;
119
120
		if( ( $site = $attr->get( 'site' ) ) !== null ) {
121
			$urlparams['site'] = $site;
122
		}
123
124
		if( ( $lang = $attr->get( 'locale' ) ) !== null ) {
125
			$urlparams['locale'] = $lang;
126
		}
127
128
		if( ( $currency = $attr->get( 'currency' ) ) !== null ) {
129
			$urlparams['currency'] = $currency;
130
		}
131
132
		return $urlparams;
133
	}
134
135
136
	/**
137
	 * Returns the closure for retrieving the user groups
138
	 *
139
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
140
	 * @return \Closure Function which returns the user group codes
141
	 */
142
	protected function getGroups( \Aimeos\MShop\Context\Item\Iface $context )
143
	{
144
		return function() use ( $context )
145
		{
146
			$list = array();
147
			$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer/group' );
148
149
			$search = $manager->createSearch();
150
			$search->setConditions( $search->compare( '==', 'customer.group.id', $context->getGroupIds() ) );
151
152
			foreach( $manager->searchItems( $search ) as $item ) {
153
				$list[] = $item->getCode();
154
			}
155
156
			return $list;
157
		};
158
	}
159
}
160