Completed
Push — master ( 844367...ab725b )
by Aimeos
02:57
created

View::addCsrf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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 laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
13
use Illuminate\Support\Facades\Input;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Request;
16
use Illuminate\Support\Facades\Response;
17
18
19
/**
20
 * Service providing the view objects
21
 *
22
 * @package laravel
23
 * @subpackage Base
24
 */
25
class View
26
{
27
	/**
28
	 * @var \Aimeos\Shop\Base\I18n
29
	 */
30
	private $i18n;
31
32
	/**
33
	 * @var \Aimeos\Shop\Base\Support
34
	 */
35
	private $support;
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\Shop\Base\I18n $i18n I18n object
42
	 * @param \Aimeos\Shop\Base\Support $support Support object
43
	 */
44
	public function __construct( \Aimeos\Shop\Base\I18n $i18n, \Aimeos\Shop\Base\Support $support )
45
	{
46
		$this->i18n = $i18n;
47
		$this->support = $support;
48
	}
49
50
51
	/**
52
	 * Creates the view object for the HTML client.
53
	 *
54
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
55
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
56
	 * @param string|null $locale Code of the current language or null for no translation
57
	 * @return \Aimeos\MW\View\Iface View object
58
	 */
59
	public function create( \Aimeos\MW\Config\Iface $config, array $templatePaths, $locale = null )
60
	{
61
		$view = new \Aimeos\MW\View\Standard( $templatePaths );
62
63
		$this->addConfig( $view, $config );
64
		$this->addNumber( $view, $config );
65
		$this->addRequest( $view );
66
		$this->addResponse( $view );
67
		$this->addParam( $view );
68
		$this->addUrl( $view );
69
		$this->addCsrf( $view );
70
		$this->addAccess( $view );
71
		$this->addTranslate( $view, $config, $locale );
72
73
		return $view;
74
	}
75
76
77
	/**
78
	 * Adds the "access" helper to the view object
79
	 *
80
	 * @param \Aimeos\MW\View\Iface $view View object
81
	 * @return \Aimeos\MW\View\Iface Modified view object
82
	 */
83
	protected function addAccess( \Aimeos\MW\View\Iface $view )
84
	{
85
		$support = $this->support;
86
87
		$fcn = function() use ( $support ) {
88
			return $support->getGroups();
89
		};
90
91
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
92
		$view->addHelper( 'access', $helper );
93
94
		return $view;
95
	}
96
97
98
	/**
99
	 * Adds the "config" helper to the view object
100
	 *
101
	 * @param \Aimeos\MW\View\Iface $view View object
102
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
103
	 * @return \Aimeos\MW\View\Iface Modified view object
104
	 */
105
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
106
	{
107
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
108
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
109
		$view->addHelper( 'config', $helper );
110
111
		return $view;
112
	}
113
114
115
	/**
116
	 * Adds the "access" helper to the view object
117
	 *
118
	 * @param \Aimeos\MW\View\Iface $view View object
119
	 * @return \Aimeos\MW\View\Iface Modified view object
120
	 */
121
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
122
	{
123
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
124
		$view->addHelper( 'csrf', $helper );
125
126
		return $view;
127
	}
128
129
130
	/**
131
	 * Adds the "number" helper to the view object
132
	 *
133
	 * @param \Aimeos\MW\View\Iface $view View object
134
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
135
	 * @return \Aimeos\MW\View\Iface Modified view object
136
	 */
137
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
138
	{
139
		$sepDec = $config->get( 'client/html/common/format/seperatorDecimal', '.' );
140
		$sep1000 = $config->get( 'client/html/common/format/seperator1000', ' ' );
141
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
142
143
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
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
	 * @return \Aimeos\MW\View\Iface Modified view object
155
	 */
156
	protected static function addParam( \Aimeos\MW\View\Iface $view )
157
	{
158
		$params = ( Route::current() ? Route::current()->parameters() + Input::all() : array() );
159
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
160
		$view->addHelper( 'param', $helper );
161
162
		return $view;
163
	}
164
165
166
	/**
167
	 * Adds the "request" helper to the view object
168
	 *
169
	 * @param \Aimeos\MW\View\Iface $view View object
170
	 * @return \Aimeos\MW\View\Iface Modified view object
171
	 */
172
	protected static function addRequest( \Aimeos\MW\View\Iface $view )
173
	{
174
		$helper = new \Aimeos\MW\View\Helper\Request\Laravel5( $view, Request::instance() );
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
175
		$view->addHelper( 'request', $helper );
176
177
		return $view;
178
	}
179
180
181
	/**
182
	 * Adds the "response" helper to the view object
183
	 *
184
	 * @param \Aimeos\MW\View\Iface $view View object
185
	 * @return \Aimeos\MW\View\Iface Modified view object
186
	 */
187
	protected static function addResponse( \Aimeos\MW\View\Iface $view )
188
	{
189
		$helper = new \Aimeos\MW\View\Helper\Response\Laravel5( $view );
190
		$view->addHelper( 'response', $helper );
191
192
		return $view;
193
	}
194
195
196
	/**
197
	 * Adds the "url" 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 addUrl( \Aimeos\MW\View\Iface $view )
203
	{
204
		$fixed = array();
205
206
		if( Route::current() )
207
		{
208
			if( ( $value = Route::input( 'site' ) ) !== null ) {
209
				$fixed['site'] = $value;
210
			}
211
212
			if( ( $value = Route::input( 'locale' ) ) !== null ) {
213
				$fixed['locale'] = $value;
214
			}
215
216
			if( ( $value = Route::input( 'currency' ) ) !== null ) {
217
				$fixed['currency'] = $value;
218
			}
219
		}
220
221
		$helper = new \Aimeos\MW\View\Helper\Url\Laravel5( $view, app('url'), $fixed );
222
		$view->addHelper( 'url', $helper );
223
224
		return $view;
225
	}
226
227
228
	/**
229
	 * Adds the "translate" helper to the view object
230
	 *
231
	 * @param \Aimeos\MW\View\Iface $view View object
232
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
233
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
234
	 * @return \Aimeos\MW\View\Iface Modified view object
235
	 */
236
	protected function addTranslate( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config, $locale )
237
	{
238
		if( $locale !== null )
239
		{
240
			$i18n = $this->i18n->get( array( $locale ), $config->get( 'i18n', array() ) );
0 ignored issues
show
Unused Code introduced by
The call to I18n::get() has too many arguments starting with $config->get('i18n', array()).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
241
			$translation = $i18n[$locale];
242
		}
243
		else
244
		{
245
			$translation = new \Aimeos\MW\Translation\None( 'en' );
246
		}
247
248
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
249
		$view->addHelper( 'translate', $helper );
250
251
		return $view;
252
	}
253
}