Passed
Push — master ( 48d2af...a4afe2 )
by Aimeos
11:39
created

Context   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
eloc 62
dl 0
loc 215
rs 10
c 13
b 0
f 0
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addDatabaseManager() 0 5 1
A addFilesystemManager() 0 5 1
A addLogger() 0 5 1
A addMailer() 0 6 1
A addMessageQueueManager() 0 5 1
A addProcess() 0 10 1
A addSession() 0 5 1
A addCache() 0 5 1
B addUserGroups() 0 24 7
A get() 0 38 4
A __construct() 0 3 1
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->addProcess( $context );
63
64
			self::$context = $context;
65
		}
66
67
		$context = self::$context;
68
		$context->setConfig( $config );
69
70
		if( $locale === true )
71
		{
72
			$localeItem = $this->container->get( 'aimeos.locale' )->get( $context );
73
			$context->setI18n( $this->container->get( 'aimeos.i18n' )->get( array( $localeItem->getLanguageId() ) ) );
74
			$context->setLocale( $localeItem );
75
76
			foreach( $localeItem->getSiteItem()->getConfig() as $key => $value ) {
77
				$config->set( $key, $value );
78
			}
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 process 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 addProcess( \Aimeos\MShop\Context\Item\Iface $context )
181
	{
182
		$config = $context->getConfig();
183
		$max = $config->get( 'pcntl_max', 4 );
184
		$prio = $config->get( 'pcntl_priority', 19 );
185
186
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
187
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
188
189
		return $context->setProcess( $process );
190
	}
191
192
193
	/**
194
	 * Adds the session object to the context
195
	 *
196
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
197
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
198
	 */
199
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
200
	{
201
		$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

201
		$session = new \Aimeos\MW\Session\Symfony2( /** @scrutinizer ignore-type */ $this->container->get( 'session' ) );
Loading history...
202
203
		return $context->setSession( $session );
204
	}
205
206
207
	/**
208
	 * Adds the user ID and name if available
209
	 *
210
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
211
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
212
	 */
213
	protected function addUserGroups( \Aimeos\MShop\Context\Item\Iface $context )
214
	{
215
		$username = '';
216
		$token = $this->container->get( 'security.token_storage' )->getToken();
217
218
		if( is_object( $token ) && is_object( $token->getUser() ) && method_exists( $token->getUser(), 'getId' ) )
219
		{
220
			$username = $token->getUser()->getUsername();
221
			$userid = $token->getUser()->getId();
222
			$context->setUserId( $userid );
223
			$context->setGroupIds( function() use ( $context, $userid )
224
			{
225
				$manager = \Aimeos\MShop::create( $context, 'customer' );
226
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
227
			} );
228
		}
229
230
		if( $username === '' && $this->container->has( 'request_stack' )
231
			&& ( $request = $this->container->get( 'request_stack' )->getMasterRequest() ) !== null
232
		) {
233
			$username = $request->getClientIp();
234
		}
235
236
		return $context->setEditor( $username );
237
	}
238
}
239