Completed
Push — master ( fe1a0b...681530 )
by Aimeos
14:27
created

Context::addUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Slim\Base;
11
12
use Interop\Container\ContainerInterface;
13
14
15
/**
16
 * Service providing the context objects
17
 *
18
 * @package Slim
19
 * @subpackage Base
20
 */
21
class Context
22
{
23
	private $container;
24
	private $context;
25
26
27
	/**
28
	 * Initializes the object
29
	 *
30
	 * @param ContainerInterface $container Dependency container
31
	 */
32
	public function __construct( ContainerInterface $container )
33
	{
34
		$this->container = $container;
35
	}
36
37
38
	/**
39
	 * Returns the current context
40
	 *
41
	 * @param boolean $locale True to add locale object to context, false if not
42
	 * @param array $attributes Associative list of URL parameter
43
	 * @param string $type Configuration type ("frontend" or "backend")
44
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
45
	 */
46
	public function get( $locale = true, array $attributes = array(), $type = 'frontend' )
47
	{
48
		$config = $this->container->get( 'aimeos_config' )->get( $type );
49
50
		if( $this->context === null )
51
		{
52
			$context = new \Aimeos\MShop\Context\Item\Standard();
53
			$context->setConfig( $config );
54
55
			$this->addDataBaseManager( $context );
56
			$this->addFilesystemManager( $context );
57
			$this->addMessageQueueManager( $context );
58
			$this->addLogger( $context );
59
			$this->addCache( $context );
60
			$this->addMailer( $context);
61
			$this->addSession( $context );
62
			$this->addUser( $context );
63
64
			$this->context = $context;
65
		}
66
67
		$this->context->setConfig( $config );
68
69
		if( $locale === true )
70
		{
71
			$localeItem = $this->container->get( 'aimeos_locale' )->get( $this->context, $attributes );
72
			$this->context->setLocale( $localeItem );
73
			$this->context->setI18n( $this->container->get( 'aimeos_i18n' )->get( array( $localeItem->getLanguageId() ) ) );
74
		}
75
76
		return $this->context;
77
	}
78
79
80
	/**
81
	 * Adds the cache object to the context
82
	 *
83
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
84
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
85
	 */
86
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
87
	{
88
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
89
		$context->setCache( $cache );
90
91
		return $context;
92
	}
93
94
95
	/**
96
	 * Adds the database manager object to the context
97
	 *
98
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
99
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
100
	 */
101
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context )
102
	{
103
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
104
		$context->setDatabaseManager( $dbm );
105
106
		return $context;
107
	}
108
109
110
	/**
111
	 * Adds the filesystem manager object to the context
112
	 *
113
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
114
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
115
	 */
116
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
117
	{
118
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() );
119
		$context->setFilesystemManager( $fs );
120
121
		return $context;
122
	}
123
124
125
	/**
126
	 * Adds the logger object to the context
127
	 *
128
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
129
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
130
	 */
131
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
132
	{
133
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
134
		$context->setLogger( $logger );
135
136
		return $context;
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
		$mail = new \Aimeos\MW\Mail\Swift( $this->container->get( 'mailer' ) );
150
		$context->setMail( $mail );
151
152
		return $context;
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
		$context->setMessageQueueManager( $mq );
166
167
		return $context;
168
	}
169
170
171
	/**
172
	 * Adds the session object to the context
173
	 *
174
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
175
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
176
	 */
177
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
178
	{
179
		$session = new \Aimeos\MW\Session\PHP();
180
		$context->setSession( $session );
181
182
		return $context;
183
	}
184
185
186
	/**
187
	 * Adds user information 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 addUser( \Aimeos\MShop\Context\Item\Iface $context )
193
	{
194
		$ipaddr = $this->container->request->getAttribute('ip_address');
0 ignored issues
show
Bug introduced by
Accessing request on the interface Interop\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
195
		$context->setEditor( $ipaddr );
196
197
		return $context;
198
	}
199
}
200