Completed
Push — master ( 721c74...b7127a )
by Aimeos
02:51
created

Context::addProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 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
Unused Code introduced by
The property $locale is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can 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
77
		$this->addSession( $context );
78
		$this->addUserGroups( $context);
79
80
		return $context;
81
	}
82
83
84
	/**
85
	 * Adds the cache object to the context
86
	 *
87
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
88
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
89
	 */
90
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
91
	{
92
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
93
94
		return $context->setCache( $cache );
95
	}
96
97
98
	/**
99
	 * Adds the database manager object to the context
100
	 *
101
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
102
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
103
	 */
104
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context )
105
	{
106
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
107
108
		return $context->setDatabaseManager( $dbm );
109
	}
110
111
112
	/**
113
	 * Adds the filesystem manager object to the context
114
	 *
115
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
116
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
117
	 */
118
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
119
	{
120
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() );
121
122
		return $context->setFilesystemManager( $fs );
123
	}
124
125
126
	/**
127
	 * Adds the logger object to the context
128
	 *
129
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
130
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
131
	 */
132
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
133
	{
134
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
135
136
		return $context->setLogger( $logger );
137
	}
138
139
140
141
	/**
142
	 * Adds the mailer 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 addMailer( \Aimeos\MShop\Context\Item\Iface $context )
148
	{
149
		$container = $this->container;
150
		$mail = new \Aimeos\MW\Mail\Swift( function() use ( $container) { return $container->get( 'mailer' ); } );
151
152
		return $context->setMail( $mail );
153
	}
154
155
156
	/**
157
	 * Adds the message queue manager object to the context
158
	 *
159
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
160
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
161
	 */
162
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
163
	{
164
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
165
166
		return $context->setMessageQueueManager( $mq );
167
	}
168
169
170
	/**
171
	 * Adds the process object to the context
172
	 *
173
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
174
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
175
	 */
176
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context )
177
	{
178
		$config = $context->getConfig();
179
		$max = $config->get( 'pcntl_max', 4 );
180
		$prio = $config->get( 'pcntl_priority', 19 );
181
182
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
183
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
184
185
		return $context->setProcess( $process );
0 ignored issues
show
Bug introduced by
The method setProcess() does not seem to exist on object<Aimeos\MShop\Context\Item\Iface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
	}
187
188
189
	/**
190
	 * Adds the session object to the context
191
	 *
192
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
193
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
194
	 */
195
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
196
	{
197
		$session = new \Aimeos\MW\Session\Symfony2( $this->container->get( 'session' ) );
198
199
		return $context->setSession( $session );
200
	}
201
202
203
	/**
204
	 * Adds the user ID and name if available
205
	 *
206
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
207
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
208
	 */
209
	protected function addUserGroups( \Aimeos\MShop\Context\Item\Iface $context )
210
	{
211
		$username = '';
212
		$token = $this->container->get( 'security.token_storage' )->getToken();
213
214
		if( is_object( $token ) && is_object( $token->getUser() ) && method_exists( $token->getUser(), 'getId' ) )
215
		{
216
			$username =  $token->getUser()->getUsername();
217
			$userid = $token->getUser()->getId();
218
			$context->setUserId( $userid );
219
			$context->setGroupIds( function() use ( $context, $userid )
220
			{
221
				$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
222
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Aimeos\MShop\Common\Item\Iface as the method getGroups() does only exist in the following implementations of said interface: Aimeos\MShop\Customer\Item\Base, Aimeos\MShop\Customer\Item\FosUser, Aimeos\MShop\Customer\Item\Standard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
223
			} );
224
		}
225
226
		if( $username === '' && $this->container->has( 'request_stack' )
227
			&& ( $request = $this->container->get('request_stack')->getMasterRequest() ) !== null
228
		) {
229
			$username = $request->getClientIp();
230
		}
231
232
		return $context->setEditor( $username );
233
	}
234
}
235