Passed
Push — master ( bc9215...41255a )
by Aimeos
03:43
created

Context::addMessageQueueManager()   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-2016
6
 * @package laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\Support\Facades\Route;
15
16
17
/**
18
 * Service providing the context objects
19
 *
20
 * @package laravel
21
 * @subpackage Base
22
 */
23
class Context
24
{
25
	/**
26
	 * @var \Aimeos\MShop\Context\Item\Iface
27
	 */
28
	private $context;
29
30
	/**
31
	 * @var \Aimeos\Shop\Base\Config
32
	 */
33
	private $config;
34
35
	/**
36
	 * @var \Aimeos\Shop\Base\I18n
37
	 */
38
	private $i18n;
39
40
	/**
41
	 * @var \Aimeos\Shop\Base\Locale
42
	 */
43
	private $locale;
44
45
	/**
46
	 * @var \Illuminate\Session\Store
47
	 */
48
	private $session;
49
50
51
	/**
52
	 * Initializes the object
53
	 *
54
	 * @param \Illuminate\Session\Store $session Laravel session object
55
	 * @param \Aimeos\Shop\Base\Config $config Configuration object
56
	 * @param \Aimeos\Shop\Base\Locale $locale Locale object
57
	 * @param \Aimeos\Shop\Base\I18n $i18n Internationalisation object
58
	 */
59
	public function __construct( \Illuminate\Session\Store $session, \Aimeos\Shop\Base\Config $config, \Aimeos\Shop\Base\Locale $locale, \Aimeos\Shop\Base\I18n $i18n )
60
	{
61
		$this->session = $session;
62
		$this->config = $config;
63
		$this->locale = $locale;
64
		$this->i18n = $i18n;
65
	}
66
67
68
	/**
69
	 * Returns the current context
70
	 *
71
	 * @param bool $locale True to add locale object to context, false if not (deprecated, use \Aimeos\Shop\Base\Locale)
72
	 * @param string $type Configuration type, i.e. "frontend" or "backend" (deprecated, use \Aimeos\Shop\Base\Config)
73
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
74
	 */
75
	public function get( bool $locale = true, string $type = 'frontend' ) : \Aimeos\MShop\Context\Item\Iface
76
	{
77
		$config = $this->config->get( $type );
78
79
		if( $this->context === null )
80
		{
81
			$context = new \Aimeos\MShop\Context\Item\Standard();
82
			$context->setConfig( $config );
83
84
			$this->addDataBaseManager( $context );
85
			$this->addFilesystemManager( $context );
86
			$this->addMessageQueueManager( $context );
87
			$this->addLogger( $context );
88
			$this->addCache( $context );
89
			$this->addMailer( $context );
90
			$this->addNonce( $context );
91
			$this->addProcess( $context );
92
			$this->addSession( $context );
93
			$this->addUser( $context );
94
			$this->addGroups( $context );
95
96
			$this->context = $context;
97
		}
98
99
		$this->context->setConfig( $config );
100
101
		if( $locale === true )
102
		{
103
			$localeItem = $this->locale->get( $this->context );
104
			$this->context->setLocale( $localeItem );
105
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
106
107
			$config->apply( $localeItem->getSiteItem()->getConfig() );
108
		}
109
110
		return $this->context;
111
	}
112
113
114
	/**
115
	 * Adds the cache object to the context
116
	 *
117
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
118
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
119
	 */
120
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
121
	{
122
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
123
124
		return $context->setCache( $cache );
125
	}
126
127
128
	/**
129
	 * Adds the database manager object to the context
130
	 *
131
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
132
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
133
	 */
134
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
135
	{
136
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
137
138
		return $context->setDatabaseManager( $dbm );
139
	}
140
141
142
	/**
143
	 * Adds the filesystem manager object to the context
144
	 *
145
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
146
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
147
	 */
148
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
149
	{
150
		$config = $context->getConfig();
151
		$path = storage_path( 'aimeos' );
152
153
		$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, $path );
154
155
		return $context->setFilesystemManager( $fs );
156
	}
157
158
159
	/**
160
	 * Adds the logger object to the context
161
	 *
162
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
163
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
164
	 */
165
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
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\MW\Logger\Iface expected by parameter $logger of Aimeos\MShop\Context\Item\Iface::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\Context\Item\Iface $context Context object
178
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
179
	 */
180
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
181
	{
182
		$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
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\Context\Item\Iface $context Context object
192
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
193
	 */
194
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
195
	{
196
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
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\Context\Item\Iface $context Context object
206
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
207
	 */
208
	protected function addNonce( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
209
	{
210
		return $context->setNonce( base64_encode( random_bytes( 16 ) ) );
211
	}
212
213
214
	/**
215
	 * Adds the process object to the context
216
	 *
217
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
218
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
219
	 */
220
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
221
	{
222
		$config = $context->getConfig();
223
		$max = $config->get( 'pcntl_max', 4 );
224
		$prio = $config->get( 'pcntl_priority', 19 );
225
226
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
227
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
228
229
		return $context->setProcess( $process );
230
	}
231
232
233
	/**
234
	 * Adds the session object to the context
235
	 *
236
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
237
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
238
	 */
239
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
240
	{
241
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
242
243
		return $context->setSession( $session );
244
	}
245
246
247
	/**
248
	 * Adds the user ID and name if available
249
	 *
250
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
251
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
252
	 */
253
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
254
	{
255
		$key = collect( config( 'shop.routes' ) )->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )->keys()->first();
256
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
257
258
		if( ( $userid = Auth::guard( $guard )->id() ) !== null ) {
259
			$context->setUserId( $userid );
260
		}
261
262
		if( ( $user = Auth::guard( $guard )->user() ) !== null ) {
263
			$context->setEditor( $user->name );
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...
264
		} else {
265
			$context->setEditor( \Request::ip() );
0 ignored issues
show
Bug introduced by
It seems like Request::ip() can also be of type null; however, parameter $name of Aimeos\MShop\Context\Item\Iface::setEditor() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

265
			$context->setEditor( /** @scrutinizer ignore-type */ \Request::ip() );
Loading history...
266
		}
267
268
		return $context;
269
	}
270
271
272
	/**
273
	 * Adds the group IDs if available
274
	 *
275
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
276
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
277
	 */
278
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
279
	{
280
		$key = collect( config( 'shop.routes' ) )->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )->keys()->first();
281
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
282
283
		if( ( $userid = Auth::guard( $guard )->id() ) !== null )
284
		{
285
			$context->setGroupIds( function() use ( $context, $userid ) {
286
				$manager = \Aimeos\MShop::create( $context, 'customer' );
287
				$filter = $manager->filter()->add( 'customer.id', '==', $userid );
288
				return $manager->search( $filter, ['customer/group'] )->getGroups()->first();
289
			} );
290
		}
291
292
		return $context;
293
	}
294
}
295