Context::addMailer()   A
last analyzed

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->addUserGroups( $context );
92
93
			$this->context = $context;
94
		}
95
96
		$this->context->setConfig( $config );
97
98
		if( $locale === true )
99
		{
100
			$localeItem = $this->locale->get( $this->context );
101
			$this->context->setLocale( $localeItem );
102
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
103
104
			$config->apply( $localeItem->getSiteItem()->getConfig() );
105
		}
106
107
		return $this->context;
108
	}
109
110
111
	/**
112
	 * Adds the cache object to the context
113
	 *
114
	 * @param \Aimeos\MShop\ContextIface $context Context object including config
115
	 * @return \Aimeos\MShop\ContextIface Modified context object
116
	 */
117
	protected function addCache( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
118
	{
119
		$cache = \Aimeos\MAdmin::create( $context, 'cache' )->getCache();
0 ignored issues
show
Bug introduced by
The method getCache() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Coupon\Manager\Code\Iface or Aimeos\MShop\Product\Manager\Iface or Aimeos\MShop\Supplier\Manager\Iface or Aimeos\MShop\Price\Manager\Property\Type\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Cms\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Attribute\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Customer\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Group\Manager\Iface or Aimeos\MShop\Catalog\Manager\Iface or Aimeos\MShop\Locale\Manager\Site\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Stock\Manager\Type\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MShop\Common\Manager\Type\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Cms\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Cms\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MShop\Order\Manager\Basket\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Index\Manager\Iface or Aimeos\MShop\Index\Manager\Attribute\Iface or Aimeos\MShop\Index\Manager\Text\Iface or Aimeos\MShop\Index\Manager\Supplier\Iface or Aimeos\MShop\Index\Manager\Catalog\Iface or Aimeos\MShop\Index\Manager\Price\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

119
		$cache = \Aimeos\MAdmin::create( $context, 'cache' )->/** @scrutinizer ignore-call */ getCache();
Loading history...
120
121
		return $context->setCache( $cache );
122
	}
123
124
125
	/**
126
	 * Adds the database manager object to the context
127
	 *
128
	 * @param \Aimeos\MShop\ContextIface $context Context object
129
	 * @return \Aimeos\MShop\ContextIface Modified context object
130
	 */
131
	protected function addDatabaseManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
132
	{
133
		$dbm = new \Aimeos\Base\DB\Manager\Standard( $context->config()->get( 'resource' ), 'DBAL' );
134
135
		return $context->setDatabaseManager( $dbm );
136
	}
137
138
139
	/**
140
	 * Adds the filesystem manager object to the context
141
	 *
142
	 * @param \Aimeos\MShop\ContextIface $context Context object
143
	 * @return \Aimeos\MShop\ContextIface Modified context object
144
	 */
145
	protected function addFilesystemManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
146
	{
147
		$config = $context->config()->get( 'resource' );
148
		$fs = new \Aimeos\Base\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) );
149
150
		return $context->setFilesystemManager( $fs );
151
	}
152
153
154
	/**
155
	 * Adds the logger object to the context
156
	 *
157
	 * @param \Aimeos\MShop\ContextIface $context Context object
158
	 * @return \Aimeos\MShop\ContextIface Modified context object
159
	 */
160
	protected function addLogger( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface
161
	{
162
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
163
164
		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

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

278
		$key = collect( config( /** @scrutinizer ignore-type */ 'shop.routes' ) )
Loading history...
279
			->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )
280
			->keys()->first();
281
		$gname = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
282
283
		if( ( $guard = Auth::guard( $gname ) ) && ( $userid = $guard->id() ) )
284
		{
285
			try
286
			{
287
				$context->setUser( function() use ( $context, $userid ) {
288
					return \Aimeos\MShop::create( $context, 'customer' )->get( $userid, ['group'] );
289
				} );
290
291
				$context->setGroups( function() use ( $context ) {
292
					return $context->user()?->getGroups() ?? [];
293
				} );
294
			}
295
			catch( \Exception $e ) {} // avoid errors if user is assigned to another site
296
297
			$context->setEditor( $guard->user()?->email ?: \Request::ip() );
0 ignored issues
show
Bug introduced by
It seems like $guard->user()->email ?: Request::ip() can also be of type null; however, parameter $name of Aimeos\MShop\ContextIface::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

297
			$context->setEditor( /** @scrutinizer ignore-type */ $guard->user()?->email ?: \Request::ip() );
Loading history...
Bug introduced by
Accessing email on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
298
		}
299
		elseif( $ip = \Request::ip() )
300
		{
301
			$context->setEditor( $ip );
302
		}
303
304
		return $context;
305
	}
306
}
307