Passed
Push — master ( 09a009...1be945 )
by Aimeos
11:09
created

Context::addFilesystemManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package symfony
7
 * @subpackage Service
8
 */
9
10
namespace Aimeos\ShopBundle\Service;
11
12
use Symfony\Component\DependencyInjection\Container;
13
14
15
/**
16
 * Service providing the context based objects
17
 *
18
 * @author Garret Watkins <[email protected]>
19
 * @package symfony
20
 * @subpackage Service
21
 */
22
class Context
23
{
24
	private static $context;
25
	private $container;
26
	private $locale;
0 ignored issues
show
introduced by
The private property $locale is not used, and could be removed.
Loading history...
27
28
29
	/**
30
	 * Initializes the context manager object
31
	 *
32
	 * @param Container $container Container object to access parameters
33
	 */
34
	public function __construct( Container $container )
35
	{
36
		$this->container = $container;
37
	}
38
39
40
	/**
41
	 * Returns the current context.
42
	 *
43
	 * @param boolean $locale True to add locale object to context, false if not
44
	 * @param string $type Configuration type ("frontend" or "backend")
45
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
46
	 */
47
	public function get( $locale = true, $type = 'frontend' )
48
	{
49
		$config = $this->container->get( 'aimeos.config' )->get( $type );
50
51
		if( self::$context === null )
52
		{
53
			$context = new \Aimeos\MShop\Context\Item\Standard();
54
			$context->setConfig( $config );
55
56
			$this->addDataBaseManager( $context );
57
			$this->addFilesystemManager( $context );
58
			$this->addMessageQueueManager( $context );
59
			$this->addLogger( $context );
60
			$this->addCache( $context );
61
			$this->addMailer( $context );
62
			$this->addNonce( $context );
63
			$this->addProcess( $context );
64
			$this->addPassword( $context );
65
66
			self::$context = $context;
67
		}
68
69
		$context = self::$context;
70
		$context->setConfig( $config );
71
72
		if( $locale === true )
73
		{
74
			$localeItem = $this->container->get( 'aimeos.locale' )->get( $context );
75
			$context->setI18n( $this->container->get( 'aimeos.i18n' )->get( array( $localeItem->getLanguageId() ) ) );
76
			$context->setLocale( $localeItem );
77
78
			$config->apply( $localeItem->getSiteItem()->getConfig() );
79
		}
80
81
		$this->addSession( $context );
82
		$this->addUserGroups( $context );
83
84
		return $context;
85
	}
86
87
88
	/**
89
	 * Adds the cache object to the context
90
	 *
91
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
92
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
93
	 */
94
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
95
	{
96
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
97
98
		return $context->setCache( $cache );
99
	}
100
101
102
	/**
103
	 * Adds the database manager object to the context
104
	 *
105
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
106
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
107
	 */
108
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context )
109
	{
110
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
111
112
		return $context->setDatabaseManager( $dbm );
113
	}
114
115
116
	/**
117
	 * Adds the filesystem manager object to the context
118
	 *
119
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
120
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
121
	 */
122
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
123
	{
124
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() );
125
126
		return $context->setFilesystemManager( $fs );
127
	}
128
129
130
	/**
131
	 * Adds the logger object to the context
132
	 *
133
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
134
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
135
	 */
136
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
137
	{
138
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
139
140
		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

140
		return $context->setLogger( /** @scrutinizer ignore-type */ $logger );
Loading history...
141
	}
142
143
144
145
	/**
146
	 * Adds the mailer object to the context
147
	 *
148
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
149
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
150
	 */
151
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
152
	{
153
		$container = $this->container;
154
		$mail = new \Aimeos\MW\Mail\Swift( function() use ( $container ) { return $container->get( 'mailer' ); } );
155
156
		return $context->setMail( $mail );
157
	}
158
159
160
	/**
161
	 * Adds the message queue manager object to the context
162
	 *
163
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
164
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
165
	 */
166
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
167
	{
168
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
169
170
		return $context->setMessageQueueManager( $mq );
171
	}
172
173
174
	/**
175
	 * Adds the nonce value for inline JS 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 addNonce( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
181
	{
182
		return $context->setNonce( base64_encode( random_bytes( 16 ) ) );
183
	}
184
185
186
	/**
187
	 * Adds the password hasher object to the context
188
	 *
189
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
190
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
191
	 */
192
	protected function addPassword( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
193
	{
194
		return $context->setPassword( new \Aimeos\MW\Password\Standard() );
195
	}
196
197
198
	/**
199
	 * Adds the process object to the context
200
	 *
201
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
202
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
203
	 */
204
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context )
205
	{
206
		$config = $context->getConfig();
207
		$max = $config->get( 'pcntl_max', 4 );
208
		$prio = $config->get( 'pcntl_priority', 19 );
209
210
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
211
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
212
213
		return $context->setProcess( $process );
214
	}
215
216
217
	/**
218
	 * Adds the session object to the context
219
	 *
220
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
221
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
222
	 */
223
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
224
	{
225
		$session = new \Aimeos\MW\Session\Symfony2( $this->container->get( 'session' ) );
0 ignored issues
show
Bug introduced by
It seems like $this->container->get('session') can also be of type null; however, parameter $object of Aimeos\MW\Session\Symfony2::__construct() does only seem to accept Symfony\Component\HttpFo...ession\SessionInterface, 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

225
		$session = new \Aimeos\MW\Session\Symfony2( /** @scrutinizer ignore-type */ $this->container->get( 'session' ) );
Loading history...
226
227
		return $context->setSession( $session );
228
	}
229
230
231
	/**
232
	 * Adds the user ID and name if available
233
	 *
234
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
235
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
236
	 */
237
	protected function addUserGroups( \Aimeos\MShop\Context\Item\Iface $context )
238
	{
239
		$username = '';
240
		$token = $this->container->get( 'security.token_storage' )->getToken();
241
242
		if( is_object( $token ) && is_object( $token->getUser() ) && method_exists( $token->getUser(), 'getId' ) )
243
		{
244
			$username = $token->getUser()->getUsername();
245
			$userid = $token->getUser()->getId();
246
			$context->setUserId( $userid );
247
			$context->setGroupIds( function() use ( $context, $userid )
248
			{
249
				$manager = \Aimeos\MShop::create( $context, 'customer' );
250
				return $manager->get( $userid, array( 'customer/group' ) )->getGroups();
251
			} );
252
		}
253
254
		if( $username === '' && $this->container->has( 'request_stack' )
255
			&& ( $request = $this->container->get( 'request_stack' )->getMasterRequest() ) !== null
256
		) {
257
			$username = $request->getClientIp();
258
		}
259
260
		return $context->setEditor( $username );
261
	}
262
}
263