Completed
Push — master ( 6c20d5...504679 )
by Aimeos
02:30
created

Context::addProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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->addProcess( $context );
62
			$this->addSession( $context );
63
			$this->addUser( $context );
64
65
			$this->context = $context;
66
		}
67
68
		$this->context->setConfig( $config );
69
70
		if( $locale === true )
71
		{
72
			$localeItem = $this->container->get( 'aimeos_locale' )->get( $this->context, $attributes );
73
			$this->context->setLocale( $localeItem );
74
			$this->context->setI18n( $this->container->get( 'aimeos_i18n' )->get( array( $localeItem->getLanguageId() ) ) );
75
		}
76
77
		return $this->context;
78
	}
79
80
81
	/**
82
	 * Adds the cache object to the context
83
	 *
84
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
85
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
86
	 */
87
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
88
	{
89
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
90
91
		return $context->setCache( $cache );
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
105
		return $context->setDatabaseManager( $dbm );
106
	}
107
108
109
	/**
110
	 * Adds the filesystem manager object to the context
111
	 *
112
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
113
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
114
	 */
115
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
116
	{
117
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() );
118
119
		return $context->setFilesystemManager( $fs );
120
	}
121
122
123
	/**
124
	 * Adds the logger object to the context
125
	 *
126
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
127
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
128
	 */
129
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
130
	{
131
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
132
133
		return $context->setLogger( $logger );
134
	}
135
136
137
138
	/**
139
	 * Adds the mailer object to the context
140
	 *
141
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
142
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
143
	 */
144
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
145
	{
146
		$mail = new \Aimeos\MW\Mail\Swift( $this->container->get( 'mailer' ) );
147
148
		return $context->setMail( $mail );
149
	}
150
151
152
	/**
153
	 * Adds the message queue manager object to the context
154
	 *
155
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
156
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
157
	 */
158
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
159
	{
160
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
161
162
		return $context->setMessageQueueManager( $mq );
163
	}
164
165
166
	/**
167
	 * Adds the process object to the context
168
	 *
169
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
170
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
171
	 */
172
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context )
173
	{
174
		$config = $context->getConfig();
175
		$max = $config->get( 'pcntl_max', 4 );
176
		$prio = $config->get( 'pcntl_priority', 19 );
177
178
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
179
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
180
181
		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...
182
	}
183
184
185
	/**
186
	 * Adds the session object to the context
187
	 *
188
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
189
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
190
	 */
191
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
192
	{
193
		$session = new \Aimeos\MW\Session\PHP();
194
195
		return $context->setSession( $session );
196
	}
197
198
199
	/**
200
	 * Adds user information to the context
201
	 *
202
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
203
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
204
	 */
205
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
206
	{
207
		$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...
208
209
		return $context->setEditor( $ipaddr );
210
	}
211
}
212