Completed
Push — master ( 67c4a6...e61cbc )
by Aimeos
01:50
created

src/Aimeos/Shop/Base/View.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 \Illuminate\Contracts\Config\Repository
29
	 */
30
	private $config;
31
32
	/**
33
	 * @var \Aimeos\Shop\Base\I18n
34
	 */
35
	private $i18n;
36
37
	/**
38
	 * @var \Aimeos\Shop\Base\Support
39
	 */
40
	private $support;
41
42
43
	/**
44
	 * Initializes the object
45
	 *
46
	 * @param \Illuminate\Contracts\Config\Repository $config Configuration object
47
	 * @param \Aimeos\Shop\Base\I18n $i18n I18n object
48
	 * @param \Aimeos\Shop\Base\Support $support Support object
49
	 */
50
	public function __construct( \Illuminate\Contracts\Config\Repository $config,
51
		\Aimeos\Shop\Base\I18n $i18n, \Aimeos\Shop\Base\Support $support )
52
	{
53
		$this->i18n = $i18n;
54
		$this->config = $config;
55
		$this->support = $support;
56
	}
57
58
59
	/**
60
	 * Creates the view object for the HTML client.
61
	 *
62
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
63
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
64
	 * @param string|null $locale Code of the current language or null for no translation
65
	 * @return \Aimeos\MW\View\Iface View object
66
	 */
67
	public function create( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths, $locale = null )
68
	{
69
		$engine = new \Aimeos\MW\View\Engine\Blade( app( 'Illuminate\Contracts\View\Factory' ) );
70
		$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.blade.php' => $engine ) );
71
72
		$config = $context->getConfig();
73
		$session = $context->getSession();
74
75
		$this->addCsrf( $view );
76
		$this->addAccess( $view, $context );
77
		$this->addConfig( $view, $config );
78
		$this->addNumber( $view, $config );
79
		$this->addParam( $view );
80
		$this->addRequest( $view );
81
		$this->addResponse( $view );
82
		$this->addSession( $view, $session );
83
		$this->addTranslate( $view, $locale );
84
		$this->addUrl( $view );
85
86
		return $view;
87
	}
88
89
90
	/**
91
	 * Adds the "access" helper to the view object
92
	 *
93
	 * @param \Aimeos\MW\View\Iface $view View object
94
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
95
	 * @return \Aimeos\MW\View\Iface Modified view object
96
	 */
97
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
98
	{
99
		if( $this->config->get( 'shop.accessControl', true ) === false
100
			|| ( ( $user = \Illuminate\Support\Facades\Auth::user() ) !== null && $user->superuser )
0 ignored issues
show
Accessing superuser on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
101
		) {
102
			$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
103
		}
104
		else
105
		{
106
			$support = $this->support;
107
108
			$fcn = function() use ( $support, $context ) {
109
				return $support->getGroups( $context );
110
			};
111
112
			$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
113
		}
114
115
		$view->addHelper( 'access', $helper );
116
117
		return $view;
118
	}
119
120
121
	/**
122
	 * Adds the "config" helper to the view object
123
	 *
124
	 * @param \Aimeos\MW\View\Iface $view View object
125
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
126
	 * @return \Aimeos\MW\View\Iface Modified view object
127
	 */
128
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
129
	{
130
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
131
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
132
		$view->addHelper( 'config', $helper );
133
134
		return $view;
135
	}
136
137
138
	/**
139
	 * Adds the "access" helper to the view object
140
	 *
141
	 * @param \Aimeos\MW\View\Iface $view View object
142
	 * @return \Aimeos\MW\View\Iface Modified view object
143
	 */
144
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
145
	{
146
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
147
		$view->addHelper( 'csrf', $helper );
148
149
		return $view;
150
	}
151
152
153
	/**
154
	 * Adds the "number" helper to the view object
155
	 *
156
	 * @param \Aimeos\MW\View\Iface $view View object
157
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
158
	 * @return \Aimeos\MW\View\Iface Modified view object
159
	 */
160
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
161
	{
162
		$sepDec = $config->get( 'client/html/common/format/separatorDecimal', '.' );
163
		$sep1000 = $config->get( 'client/html/common/format/separator1000', ' ' );
164
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
165
166
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
167
		$view->addHelper( 'number', $helper );
168
169
		return $view;
170
	}
171
172
173
	/**
174
	 * Adds the "param" helper to the view object
175
	 *
176
	 * @param \Aimeos\MW\View\Iface $view View object
177
	 * @return \Aimeos\MW\View\Iface Modified view object
178
	 */
179
	protected function addParam( \Aimeos\MW\View\Iface $view )
180
	{
181
		$params = ( Route::current() ? Route::current()->parameters() : array() ) + Input::all();
182
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
183
		$view->addHelper( 'param', $helper );
184
185
		return $view;
186
	}
187
188
189
	/**
190
	 * Adds the "request" helper to the view object
191
	 *
192
	 * @param \Aimeos\MW\View\Iface $view View object
193
	 * @return \Aimeos\MW\View\Iface Modified view object
194
	 */
195
	protected function addRequest( \Aimeos\MW\View\Iface $view )
196
	{
197
		$helper = new \Aimeos\MW\View\Helper\Request\Laravel5( $view, Request::instance() );
198
		$view->addHelper( 'request', $helper );
199
200
		return $view;
201
	}
202
203
204
	/**
205
	 * Adds the "response" helper to the view object
206
	 *
207
	 * @param \Aimeos\MW\View\Iface $view View object
208
	 * @return \Aimeos\MW\View\Iface Modified view object
209
	 */
210
	protected function addResponse( \Aimeos\MW\View\Iface $view )
211
	{
212
		$helper = new \Aimeos\MW\View\Helper\Response\Laravel5( $view );
213
		$view->addHelper( 'response', $helper );
214
215
		return $view;
216
	}
217
218
219
	/**
220
	 * Adds the "session" helper to the view object
221
	 *
222
	 * @param \Aimeos\MW\View\Iface $view View object
223
	 * @param \Aimeos\MW\Session\Iface $session Session object
224
	 * @return \Aimeos\MW\View\Iface Modified view object
225
	 */
226
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session )
227
	{
228
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
229
		$view->addHelper( 'session', $helper );
230
231
		return $view;
232
	}
233
234
235
	/**
236
	 * Adds the "translate" helper to the view object
237
	 *
238
	 * @param \Aimeos\MW\View\Iface $view View object
239
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
240
	 * @return \Aimeos\MW\View\Iface Modified view object
241
	 */
242
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale )
243
	{
244
		if( $locale !== null )
245
		{
246
			$i18n = $this->i18n->get( array( $locale ) );
247
			$translation = $i18n[$locale];
248
		}
249
		else
250
		{
251
			$translation = new \Aimeos\MW\Translation\None( 'en' );
252
		}
253
254
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
255
		$view->addHelper( 'translate', $helper );
256
257
		return $view;
258
	}
259
260
261
	/**
262
	 * Adds the "url" helper to the view object
263
	 *
264
	 * @param \Aimeos\MW\View\Iface $view View object
265
	 * @return \Aimeos\MW\View\Iface Modified view object
266
	 */
267
	protected function addUrl( \Aimeos\MW\View\Iface $view )
268
	{
269
		$fixed = array();
270
271
		if( Route::current() )
272
		{
273
			if( ( $value = Route::input( 'site' ) ) !== null ) {
274
				$fixed['site'] = $value;
275
			}
276
277
			if( ( $value = Route::input( 'locale' ) ) !== null ) {
278
				$fixed['locale'] = $value;
279
			}
280
281
			if( ( $value = Route::input( 'currency' ) ) !== null ) {
282
				$fixed['currency'] = $value;
283
			}
284
		}
285
286
		$helper = new \Aimeos\MW\View\Helper\Url\Laravel5( $view, app('url'), $fixed );
287
		$view->addHelper( 'url', $helper );
288
289
		return $view;
290
	}
291
}