Passed
Push — master ( 55b905...061b3a )
by Aimeos
16:33
created

Context::addCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
		else
108
		{
109
			$this->context->setI18n( $this->i18n->get( $this->languages( $this->context ) ) );
110
		}
111
112
		return $this->context;
113
	}
114
115
116
	/**
117
	 * Adds the cache object to the context
118
	 *
119
	 * @param \Aimeos\MShop\ContextIface $context Context object including config
120
	 * @return \Aimeos\MShop\ContextIface Modified context object
121
	 */
122
	protected function addCache( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
123
	{
124
		$cache = (new \Aimeos\MAdmin\Cache\Manager\Standard( $context ))->getCache();
125
126
		return $context->setCache( $cache );
127
	}
128
129
130
	/**
131
	 * Adds the database manager object to the context
132
	 *
133
	 * @param \Aimeos\MShop\ContextIface $context Context object
134
	 * @return \Aimeos\MShop\ContextIface Modified context object
135
	 */
136
	protected function addDatabaseManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
137
	{
138
		$dbm = new \Aimeos\Base\DB\Manager\Standard( $context->config()->get( 'resource', [] ), 'DBAL' );
139
140
		return $context->setDatabaseManager( $dbm );
141
	}
142
143
144
	/**
145
	 * Adds the filesystem manager object to the context
146
	 *
147
	 * @param \Aimeos\MShop\ContextIface $context Context object
148
	 * @return \Aimeos\MShop\ContextIface Modified context object
149
	 */
150
	protected function addFilesystemManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
151
	{
152
		$config = $context->config()->get( 'resource' );
153
		$fs = new \Aimeos\Base\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) );
154
155
		return $context->setFilesystemManager( $fs );
156
	}
157
158
159
	/**
160
	 * Adds the logger object to the context
161
	 *
162
	 * @param \Aimeos\MShop\ContextIface $context Context object
163
	 * @return \Aimeos\MShop\ContextIface Modified context object
164
	 */
165
	protected function addLogger( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
166
	{
167
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
168
169
		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

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

283
		$key = collect( config( /** @scrutinizer ignore-type */ 'shop.routes' ) )
Loading history...
284
			->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )
285
			->keys()->first();
286
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
287
288
		if( $user = Auth::guard( $guard )->user() ) {
289
			$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...
290
			$context->setUserId( $user->getAuthIdentifier() );
291
		} elseif( $ip = \Request::ip() ) {
292
			$context->setEditor( $ip );
293
		}
294
295
		return $context;
296
	}
297
298
299
	/**
300
	 * Adds the group IDs if available
301
	 *
302
	 * @param \Aimeos\MShop\ContextIface $context Context object
303
	 * @return \Aimeos\MShop\ContextIface Modified context object
304
	 */
305
	protected function addGroups( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
306
	{
307
		$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

307
		$key = collect( config( /** @scrutinizer ignore-type */ 'shop.routes' ) )
Loading history...
308
			->where( 'prefix', optional( Route::getCurrentRoute() )
309
			->getPrefix() )
310
			->keys()->first();
311
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
312
313
		if( $userid = Auth::guard( $guard )->id() )
314
		{
315
			$context->setGroupIds( function() use ( $context, $userid ) {
316
				try {
317
					return \Aimeos\MShop::create( $context, 'customer' )->get( $userid, ['customer/group'] )->getGroups();
318
				} catch( \Exception $e ) {
319
					return [];
320
				}
321
			} );
322
		}
323
324
		return $context;
325
	}
326
327
328
	/**
329
	 * Returns all active languages
330
	 *
331
	 * @param \Aimeos\MShop\ContextIface $context Context object including database manager
332
	 * @return array List of two letter ISO language codes
333
	 */
334
	protected function languages( \Aimeos\MShop\ContextIface $context ) : array
335
	{
336
		$manager = \Aimeos\MShop::create( $context, 'locale/language' );
337
		$filter = $manager->filter( true )->slice( 0, 1000 );
338
339
		return $manager->search( $filter )->keys()->all();
340
	}
341
}
342