Completed
Push — master ( 22fee6...a6e7a8 )
by Aimeos
02:32
created

Context::addCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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
63
			self::$context = $context;
64
		}
65
66
		$context = self::$context;
67
		$context->setConfig( $config );
68
69
		if( $locale === true )
70
		{
71
			$localeItem = $this->container->get('aimeos_locale')->get( $context );
72
			$context->setI18n( $this->container->get('aimeos_i18n')->get( array( $localeItem->getLanguageId() ) ) );
73
			$context->setLocale( $localeItem );
74
		}
75
76
		$this->addSession( $context );
77
		$this->addUserGroups( $context);
78
79
		return $context;
80
	}
81
82
83
	/**
84
	 * Adds the cache object to the context
85
	 *
86
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
87
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
88
	 */
89
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
90
	{
91
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
92
		$context->setCache( $cache );
93
94
		return $context;
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
		$context->setDatabaseManager( $dbm );
108
109
		return $context;
110
	}
111
112
113
	/**
114
	 * Adds the filesystem manager object to the context
115
	 *
116
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
117
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
118
	 */
119
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
120
	{
121
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() );
122
		$context->setFilesystemManager( $fs );
123
124
		return $context;
125
	}
126
127
128
	/**
129
	 * Adds the logger object to the context
130
	 *
131
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
132
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
133
	 */
134
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
135
	{
136
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
137
		$context->setLogger( $logger );
138
139
		return $context;
140
	}
141
142
143
144
	/**
145
	 * Adds the mailer object to the context
146
	 *
147
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
148
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
149
	 */
150
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
151
	{
152
		$container = $this->container;
153
		$mail = new \Aimeos\MW\Mail\Swift( function() use ( $container) { return $container->get( 'mailer' ); } );
154
		$context->setMail( $mail );
155
156
		return $context;
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
		$context->setMessageQueueManager( $mq );
170
171
		return $context;
172
	}
173
174
175
	/**
176
	 * Adds the session object to the context
177
	 *
178
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
179
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
180
	 */
181
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
182
	{
183
		$session = new \Aimeos\MW\Session\Symfony2( $this->container->get( 'session' ) );
184
		$context->setSession( $session );
185
186
		return $context;
187
	}
188
189
190
	/**
191
	 * Adds the user ID and name if available
192
	 *
193
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
194
	 */
195
	protected function addUserGroups( \Aimeos\MShop\Context\Item\Iface $context )
196
	{
197
		$token = null;
198
		$username = '';
199
200
		if( $this->container->has( 'security.token_storage' ) ) {
201
			$token = $this->container->get( 'security.token_storage' )->getToken();
202
		}
203
		else if( $this->container->has( 'security.context' ) ) {
204
			$token = $this->container->get( 'security.context' )->getToken();
205
		}
206
207
		if( is_object( $token ) )
208
		{
209
			if( method_exists( $token->getUser(), 'getId' ) )
210
			{
211
				$userid = $token->getUser()->getId();
212
				$context->setUserId( $userid );
213
				$context->setGroupIds( function() use ( $context, $userid )
214
				{
215
					$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
216
					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...
217
				} );
218
			}
219
220
			if( is_object( $token->getUser() ) ) {
221
				$username =  $token->getUser()->getUsername();
222
			} else {
223
				$username = $token->getUser();
224
			}
225
		}
226
227
		$context->setEditor( $username );
228
	}
229
}
230