Passed
Push — master ( 061b3a...c2be73 )
by Aimeos
04:39
created

Context::languages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 */
7
8
namespace Aimeos\Shop\Base;
9
10
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Route;
13
use Illuminate\Support\Facades\Session;
14
15
16
/**
17
 * Service providing the context objects
18
 */
19
class Context
20
{
21
	/**
22
	 * @var \Aimeos\MShop\ContextIface
23
	 */
24
	private $context;
25
26
	/**
27
	 * @var \Aimeos\Shop\Base\Config
28
	 */
29
	private $config;
30
31
	/**
32
	 * @var \Aimeos\Shop\Base\I18n
33
	 */
34
	private $i18n;
35
36
	/**
37
	 * @var \Aimeos\Shop\Base\Locale
38
	 */
39
	private $locale;
40
41
	/**
42
	 * @var \Illuminate\Session\Store
43
	 */
44
	private $session;
45
46
47
	/**
48
	 * Initializes the object
49
	 *
50
	 * @param \Illuminate\Session\Store $session Laravel session object
51
	 * @param \Aimeos\Shop\Base\Config $config Configuration object
52
	 * @param \Aimeos\Shop\Base\Locale $locale Locale object
53
	 * @param \Aimeos\Shop\Base\I18n $i18n Internationalisation object
54
	 */
55
	public function __construct( \Illuminate\Session\Store $session, \Aimeos\Shop\Base\Config $config, \Aimeos\Shop\Base\Locale $locale, \Aimeos\Shop\Base\I18n $i18n )
56
	{
57
		$this->session = $session;
58
		$this->config = $config;
59
		$this->locale = $locale;
60
		$this->i18n = $i18n;
61
	}
62
63
64
	/**
65
	 * Returns the current context
66
	 *
67
	 * @param bool $locale True to add locale object to context, false if not (deprecated, use \Aimeos\Shop\Base\Locale)
68
	 * @param string $type Configuration type, i.e. "frontend" or "backend" (deprecated, use \Aimeos\Shop\Base\Config)
69
	 * @return \Aimeos\MShop\ContextIface Context object
70
	 */
71
	public function get( bool $locale = true, string $type = 'frontend' ) : \Aimeos\MShop\ContextIface
72
	{
73
		$config = $this->config->get( $type );
74
75
		if( $this->context === null )
76
		{
77
			$context = new \Aimeos\MShop\Context();
78
			$context->setConfig( $config );
79
80
			$this->addDataBaseManager( $context );
81
			$this->addFilesystemManager( $context );
82
			$this->addMessageQueueManager( $context );
83
			$this->addLogger( $context );
84
			$this->addCache( $context );
85
			$this->addMailer( $context );
86
			$this->addNonce( $context );
87
			$this->addPassword( $context );
88
			$this->addProcess( $context );
89
			$this->addSession( $context );
90
			$this->addToken( $context );
91
			$this->addUser( $context );
92
			$this->addGroups( $context );
93
94
			$this->context = $context;
95
		}
96
97
		$this->context->setConfig( $config );
98
99
		if( $locale === true )
100
		{
101
			$localeItem = $this->locale->get( $this->context );
102
			$this->context->setLocale( $localeItem );
103
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
104
105
			$config->apply( $localeItem->getSiteItem()->getConfig() );
106
		}
107
108
		return $this->context;
109
	}
110
111
112
	/**
113
	 * Adds the cache object to the context
114
	 *
115
	 * @param \Aimeos\MShop\ContextIface $context Context object including config
116
	 * @return \Aimeos\MShop\ContextIface Modified context object
117
	 */
118
	protected function addCache( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
119
	{
120
		$cache = (new \Aimeos\MAdmin\Cache\Manager\Standard( $context ))->getCache();
121
122
		return $context->setCache( $cache );
123
	}
124
125
126
	/**
127
	 * Adds the database manager object to the context
128
	 *
129
	 * @param \Aimeos\MShop\ContextIface $context Context object
130
	 * @return \Aimeos\MShop\ContextIface Modified context object
131
	 */
132
	protected function addDatabaseManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
133
	{
134
		$dbm = new \Aimeos\Base\DB\Manager\Standard( $context->config()->get( 'resource', [] ), 'DBAL' );
135
136
		return $context->setDatabaseManager( $dbm );
137
	}
138
139
140
	/**
141
	 * Adds the filesystem manager object to the context
142
	 *
143
	 * @param \Aimeos\MShop\ContextIface $context Context object
144
	 * @return \Aimeos\MShop\ContextIface Modified context object
145
	 */
146
	protected function addFilesystemManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
147
	{
148
		$config = $context->config()->get( 'resource' );
149
		$fs = new \Aimeos\Base\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) );
150
151
		return $context->setFilesystemManager( $fs );
152
	}
153
154
155
	/**
156
	 * Adds the logger object to the context
157
	 *
158
	 * @param \Aimeos\MShop\ContextIface $context Context object
159
	 * @return \Aimeos\MShop\ContextIface Modified context object
160
	 */
161
	protected function addLogger( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
162
	{
163
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
164
165
		return $context->setLogger( $logger );
0 ignored issues
show
Bug introduced by
$logger of type Aimeos\MShop\Common\Manager\Iface is incompatible with the type Aimeos\Base\Logger\Iface expected by parameter $logger of Aimeos\MShop\ContextIface::setLogger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
		return $context->setLogger( /** @scrutinizer ignore-type */ $logger );
Loading history...
166
	}
167
168
169
170
	/**
171
	 * Adds the mailer object to the context
172
	 *
173
	 * @param \Aimeos\MShop\ContextIface $context Context object
174
	 * @return \Aimeos\MShop\ContextIface Modified context object
175
	 */
176
	protected function addMailer( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
177
	{
178
		$mail = new \Aimeos\Base\Mail\Laravel( function() { return app( 'mailer' ); } );
179
180
		return $context->setMail( $mail );
181
	}
182
183
184
	/**
185
	 * Adds the message queue manager object to the context
186
	 *
187
	 * @param \Aimeos\MShop\ContextIface $context Context object
188
	 * @return \Aimeos\MShop\ContextIface Modified context object
189
	 */
190
	protected function addMessageQueueManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
191
	{
192
		$mq = new \Aimeos\Base\MQueue\Manager\Standard( $context->config()->get( 'resource', [] ) );
193
194
		return $context->setMessageQueueManager( $mq );
195
	}
196
197
198
	/**
199
	 * Adds the nonce value for inline JS to the context
200
	 *
201
	 * @param \Aimeos\MShop\ContextIface $context Context object
202
	 * @return \Aimeos\MShop\ContextIface Modified context object
203
	 */
204
	protected function addNonce( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
205
	{
206
		return $context->setNonce( base64_encode( random_bytes( 16 ) ) );
207
	}
208
209
210
	/**
211
	 * Adds the password hasher object to the context
212
	 *
213
	 * @param \Aimeos\MShop\ContextIface $context Context object
214
	 * @return \Aimeos\MShop\ContextIface Modified context object
215
	 */
216
	protected function addPassword( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
217
	{
218
		return $context->setPassword( new \Aimeos\Base\Password\Standard() );
219
	}
220
221
222
	/**
223
	 * Adds the process object to the context
224
	 *
225
	 * @param \Aimeos\MShop\ContextIface $context Context object
226
	 * @return \Aimeos\MShop\ContextIface Modified context object
227
	 */
228
	protected function addProcess( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
229
	{
230
		$config = $context->config();
231
		$max = $config->get( 'pcntl_max', 4 );
232
		$prio = $config->get( 'pcntl_priority', 19 );
233
234
		$process = new \Aimeos\Base\Process\Pcntl( $max, $prio );
235
		$process = new \Aimeos\Base\Process\Decorator\Check( $process );
236
237
		return $context->setProcess( $process );
238
	}
239
240
241
	/**
242
	 * Adds the session object to the context
243
	 *
244
	 * @param \Aimeos\MShop\ContextIface $context Context object
245
	 * @return \Aimeos\MShop\ContextIface Modified context object
246
	 */
247
	protected function addSession( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
248
	{
249
		$session = new \Aimeos\Base\Session\Laravel( $this->session );
250
251
		return $context->setSession( $session );
252
	}
253
254
255
	/**
256
	 * Adds the session token to the context
257
	 *
258
	 * @param \Aimeos\MShop\ContextIface $context Context object
259
	 * @return \Aimeos\MShop\ContextIface Modified context object
260
	 */
261
	protected function addToken( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
262
	{
263
		if( ( $token = Session::get( 'token' ) ) === null ) {
264
			Session::put( 'token', $token = Session::getId() );
265
		}
266
267
		return $context->setToken( $token );
268
	}
269
270
271
	/**
272
	 * Adds the user ID and name if available
273
	 *
274
	 * @param \Aimeos\MShop\ContextIface $context Context object
275
	 * @return \Aimeos\MShop\ContextIface Modified context object
276
	 */
277
	protected function addUser( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
278
	{
279
		$key = collect( config( 'shop.routes' ) )
0 ignored issues
show
Bug introduced by
'shop.routes' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

279
		$key = collect( config( /** @scrutinizer ignore-type */ 'shop.routes' ) )
Loading history...
280
			->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )
281
			->keys()->first();
282
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
283
284
		if( $user = Auth::guard( $guard )->user() ) {
285
			$context->setEditor( $user->name ?? (string) \Request::ip() );
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
286
			$context->setUserId( $user->getAuthIdentifier() );
287
		} elseif( $ip = \Request::ip() ) {
288
			$context->setEditor( $ip );
289
		}
290
291
		return $context;
292
	}
293
294
295
	/**
296
	 * Adds the group IDs if available
297
	 *
298
	 * @param \Aimeos\MShop\ContextIface $context Context object
299
	 * @return \Aimeos\MShop\ContextIface Modified context object
300
	 */
301
	protected function addGroups( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
302
	{
303
		$key = collect( config( 'shop.routes' ) )
0 ignored issues
show
Bug introduced by
'shop.routes' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

303
		$key = collect( config( /** @scrutinizer ignore-type */ 'shop.routes' ) )
Loading history...
304
			->where( 'prefix', optional( Route::getCurrentRoute() )
305
			->getPrefix() )
306
			->keys()->first();
307
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
308
309
		if( $userid = Auth::guard( $guard )->id() )
310
		{
311
			$context->setGroupIds( function() use ( $context, $userid ) {
312
				try {
313
					return \Aimeos\MShop::create( $context, 'customer' )->get( $userid, ['customer/group'] )->getGroups();
314
				} catch( \Exception $e ) {
315
					return [];
316
				}
317
			} );
318
		}
319
320
		return $context;
321
	}
322
}
323