Issues (76)

src/Aimeos/Shop/Base/Context.php (2 issues)

Labels
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->addProcess( $context );
91
			$this->addSession( $context );
92
			$this->addUser( $context );
93
			$this->addGroups( $context );
94
95
			$this->context = $context;
96
		}
97
98
		$this->context->setConfig( $config );
99
100
		if( $locale === true )
101
		{
102
			$localeItem = $this->locale->get( $this->context );
103
			$this->context->setLocale( $localeItem );
104
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
105
106
			$config->apply( $localeItem->getSiteItem()->getConfig() );
107
		}
108
109
		return $this->context;
110
	}
111
112
113
	/**
114
	 * Adds the cache object to the context
115
	 *
116
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
117
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
118
	 */
119
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
120
	{
121
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
122
123
		return $context->setCache( $cache );
124
	}
125
126
127
	/**
128
	 * Adds the database manager object to the context
129
	 *
130
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
131
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
132
	 */
133
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
134
	{
135
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
136
137
		return $context->setDatabaseManager( $dbm );
138
	}
139
140
141
	/**
142
	 * Adds the filesystem manager object to the context
143
	 *
144
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
145
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
146
	 */
147
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
148
	{
149
		$config = $context->getConfig();
150
		$path = storage_path( 'aimeos' );
151
152
		$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, $path );
153
154
		return $context->setFilesystemManager( $fs );
155
	}
156
157
158
	/**
159
	 * Adds the logger object to the context
160
	 *
161
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
162
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
163
	 */
164
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
165
	{
166
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
167
168
		return $context->setLogger( $logger );
0 ignored issues
show
$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

168
		return $context->setLogger( /** @scrutinizer ignore-type */ $logger );
Loading history...
169
	}
170
171
172
173
	/**
174
	 * Adds the mailer object to the context
175
	 *
176
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
177
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
178
	 */
179
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
180
	{
181
		$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
182
183
		return $context->setMail( $mail );
184
	}
185
186
187
	/**
188
	 * Adds the message queue manager object to the context
189
	 *
190
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
191
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
192
	 */
193
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
194
	{
195
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
196
197
		return $context->setMessageQueueManager( $mq );
198
	}
199
200
201
	/**
202
	 * Adds the process object to the context
203
	 *
204
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
205
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
206
	 */
207
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
208
	{
209
		$config = $context->getConfig();
210
		$max = $config->get( 'pcntl_max', 4 );
211
		$prio = $config->get( 'pcntl_priority', 19 );
212
213
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
214
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
215
216
		return $context->setProcess( $process );
217
	}
218
219
220
	/**
221
	 * Adds the session object to the context
222
	 *
223
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
224
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
225
	 */
226
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
227
	{
228
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
229
230
		return $context->setSession( $session );
231
	}
232
233
234
	/**
235
	 * Adds the user ID and name if available
236
	 *
237
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
238
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
239
	 */
240
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
241
	{
242
		$key = collect( config( 'shop.routes' ) )->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )->keys()->first();
243
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
244
245
		if( ( $userid = Auth::guard( $guard )->id() ) !== null ) {
246
			$context->setUserId( $userid );
247
		}
248
249
		if( ( $user = Auth::guard( $guard )->user() ) !== null ) {
250
			$context->setEditor( $user->name );
251
		} else {
252
			$context->setEditor( \Request::ip() );
0 ignored issues
show
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

252
			$context->setEditor( /** @scrutinizer ignore-type */ \Request::ip() );
Loading history...
253
		}
254
255
		return $context;
256
	}
257
258
259
	/**
260
	 * Adds the group IDs if available
261
	 *
262
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
263
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
264
	 */
265
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
266
	{
267
		$key = collect( config( 'shop.routes' ) )->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )->keys()->first();
268
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
269
270
		if( ( $userid = Auth::guard( $guard )->id() ) !== null )
271
		{
272
			$context->setGroupIds( function() use ( $context, $userid ) {
273
				$manager = \Aimeos\MShop::create( $context, 'customer' );
274
				return $manager->get( $userid, ['customer/group'] )->getGroups();
275
			} );
276
		}
277
278
		return $context;
279
	}
280
}
281