Completed
Push — master ( a9aecd...d17ad7 )
by Aimeos
02:08
created

Context::addLogger()   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://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Base
8
 */
9
10
11
namespace Aimeos\Shop\Base;
12
13
use TYPO3\Flow\Annotations as Flow;
14
15
16
/**
17
 * Class providing the context object
18
 *
19
 * @package flow
20
 * @subpackage Base
21
 * @Flow\Scope("singleton")
22
 */
23
class Context
24
{
25
	/**
26
	 * @var \Aimeos\MShop\Context\Item\Iface
27
	 */
28
	private static $context;
29
30
	/**
31
	 * @var \Aimeos\MShop\Locale\Item\Iface
32
	 */
33
	private $locale;
34
35
	/**
36
	 * @var array
37
	 */
38
	private $settings;
39
40
	/**
41
	 * @var \TYPO3\Flow\Cache\Frontend\StringFrontend
42
	 */
43
	private $cache;
44
45
	/**
46
	 * @var \Aimeos\Shop\Base\Aimeos
47
	 * @Flow\Inject
48
	 */
49
	protected $aimeos;
50
51
	/**
52
	 * @var \Aimeos\Shop\Base\Config
53
	 * @Flow\Inject
54
	 */
55
	protected $config;
56
57
	/**
58
	 * @var \Aimeos\Shop\Base\I18n
59
	 * @Flow\Inject
60
	 */
61
	protected $i18n;
62
63
	/**
64
	 * @var \Aimeos\Shop\Base\Locale
65
	 * @Flow\Inject
66
	 */
67
	protected $locale;
68
69
	/**
70
	 * @var \TYPO3\SwiftMailer\MailerInterface
71
	 * @Flow\Inject
72
	 */
73
	protected $mailer;
74
75
	/**
76
	 * @var \TYPO3\Flow\Session\SessionInterface
77
	 * @Flow\Inject(lazy = FALSE)
78
	 */
79
	protected $session;
80
81
82
	/**
83
	 * Returns the current context.
84
	 *
85
	 * @param \TYPO3\Flow\Mvc\RequestInterface $request Request object
86
	 * @return \Aimeos\MShop\Context\Item\Iface
87
	 */
88
	public function get( \TYPO3\Flow\Mvc\RequestInterface $request = null, $type = 'frontend' )
89
	{
90
		$config = $this->config->get( $type );
91
92
		if( self::$context === null )
93
		{
94
			$context = new \Aimeos\MShop\Context\Item\Standard();
95
			$context->setConfig( $config );
96
97
			$this->addDataBaseManager( $context );
98
			$this->addFilesystemManager( $context );
99
			$this->addMessageQueueManager( $context );
100
			$this->addLogger( $context );
101
			$this->addCache( $context );
102
			$this->addMailer( $context);
103
104
			self::$context = $context;
105
		}
106
107
		$context = self::$context;
108
		$context->setConfig( $config );
109
110
		if( $request !== null )
111
		{
112
			$localeItem = $this->locale->get( $context, $request );
113
			$context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
114
			$context->setLocale( $localeItem );
115
		}
116
117
		$this->addSession( $context );
118
		$this->addUser( $context );
119
120
		return $context;
121
	}
122
123
124
	/**
125
	 * Adds the cache object to the context
126
	 *
127
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
128
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
129
	 */
130
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
131
	{
132
		$config = $context->getConfig();
133
134
		switch( $config->get( 'flow/cache/name', 'Flow' ) )
135
		{
136
			case 'None':
137
				$config->set( 'client/html/basket/cache/enable', false );
138
				return $context->setCache( \Aimeos\MW\Cache\Factory::createManager( 'None', array(), null ) );
139
140
			case 'Flow':
141
				return $context->setCache( new \Aimeos\MAdmin\Cache\Proxy\Flow( $context, $this->cache ) );
142
143
			default:
144
				return $context->setCache( new \Aimeos\MAdmin\Cache\Proxy\Standard( $context ) );
145
		}
146
	}
147
148
149
	/**
150
	 * Adds the database manager object to the context
151
	 *
152
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
153
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
154
	 */
155
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context )
156
	{
157
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
158
		$context->setDatabaseManager( $dbm );
159
160
		return $context;
161
	}
162
163
164
	/**
165
	 * Adds the filesystem manager object to the context
166
	 *
167
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
168
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
169
	 */
170
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
171
	{
172
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() );
173
		$context->setFilesystemManager( $fs );
174
175
		return $context;
176
	}
177
178
179
	/**
180
	 * Adds the logger object to the context
181
	 *
182
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
183
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
184
	 */
185
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
186
	{
187
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
188
		$context->setLogger( $logger );
189
190
		return $context;
191
	}
192
193
194
195
	/**
196
	 * Adds the mailer object to the context
197
	 *
198
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
199
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
200
	 */
201
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
202
	{
203
		$mail = new \Aimeos\MW\Mail\Swift( $this->mailer );
0 ignored issues
show
Documentation introduced by
$this->mailer is of type object<TYPO3\SwiftMailer\MailerInterface>, but the function expects a object<Closure>|object<A...s\MW\Mail\Swift_Mailer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
		$context->setMail( $mail );
205
206
		return $context;
207
	}
208
209
210
	/**
211
	 * Adds the message queue manager object to the context
212
	 *
213
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
214
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
215
	 */
216
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
217
	{
218
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
219
		$context->setMessageQueueManager( $mq );
220
221
		return $context;
222
	}
223
224
225
	/**
226
	 * Adds the session object to the context
227
	 *
228
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
229
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
230
	 */
231
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
232
	{
233
		$session = new \Aimeos\MW\Session\Flow( $this->session );
234
		$context->setSession( $session );
235
236
		return $context;
237
	}
238
239
240
	/**
241
	 * Adds the user ID and name if available
242
	 *
243
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
244
	 */
245
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
246
	{
247
		$context->setEditor( '' );
248
	}
249
250
251
	/**
252
	 * Inject the settings
253
	 *
254
	 * @param array $settings
255
	 * @return void
256
	 */
257
	public function injectSettings( array $settings )
258
	{
259
		$this->settings = $settings;
260
	}
261
262
263
	/**
264
	 * Sets the Aimeos shop cache
265
	 *
266
	 * @param \TYPO3\Flow\Cache\Frontend\StringFrontend $cache Cache for shop data
267
	 * @return void
268
	 */
269
	public function setCache( \TYPO3\Flow\Cache\Frontend\StringFrontend $cache )
270
	{
271
		$this->cache = $cache;
272
	}
273
}
274