Completed
Push — master ( ad6ad2...4f1a9d )
by Aimeos
02:36
created

Context::addDatabaseManager()   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
63
			$this->context = $context;
64
		}
65
66
		$this->context->setConfig( $config );
67
68
		if( $locale === true )
69
		{
70
			$localeItem = $this->container->get( 'aimeos_locale' )->get( $this->context, $attributes );
71
			$this->context->setLocale( $localeItem );
72
			$this->context->setI18n( $this->container->get( 'aimeos_i18n' )->get( array( $localeItem->getLanguageId() ) ) );
73
		}
74
75
		return $this->context;
76
	}
77
78
79
	/**
80
	 * Adds the cache object to the context
81
	 *
82
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
83
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
84
	 */
85
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
86
	{
87
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
88
		$context->setCache( $cache );
89
90
		return $context;
91
	}
92
93
94
	/**
95
	 * Adds the database manager object to the context
96
	 *
97
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
98
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
99
	 */
100
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context )
101
	{
102
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
103
		$context->setDatabaseManager( $dbm );
104
105
		return $context;
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
		$context->setFilesystemManager( $fs );
119
120
		return $context;
121
	}
122
123
124
	/**
125
	 * Adds the logger object to the context
126
	 *
127
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
128
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
129
	 */
130
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
131
	{
132
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
133
		$context->setLogger( $logger );
134
135
		return $context;
136
	}
137
138
139
140
	/**
141
	 * Adds the mailer object to the context
142
	 *
143
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
144
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
145
	 */
146
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
147
	{
148
		$mail = new \Aimeos\MW\Mail\Swift( $this->container->get( 'mailer' ) );
149
		$context->setMail( $mail );
150
151
		return $context;
152
	}
153
154
155
	/**
156
	 * Adds the message queue manager object to the context
157
	 *
158
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
159
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
160
	 */
161
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
162
	{
163
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
164
		$context->setMessageQueueManager( $mq );
165
166
		return $context;
167
	}
168
169
170
	/**
171
	 * Adds the session 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 addSession( \Aimeos\MShop\Context\Item\Iface $context )
177
	{
178
		$session = new \Aimeos\MW\Session\PHP();
179
		$context->setSession( $session );
180
181
		return $context;
182
	}
183
}
184