Completed
Push — master ( f7149b...10b3eb )
by Aimeos
02:17
created

Context   B

Complexity

Total Complexity 15

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 16
dl 0
loc 205
rs 8.4614
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 50 3
A addUser() 0 6 1
A getCache() 0 17 3
B getLocale() 0 18 6
A injectSettings() 0 4 1
A setCache() 0 4 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 array
47
	 * @Flow\Inject(setting="persistence.backendOptions", package="TYPO3.Flow")
48
	 */
49
	protected $resource;
50
51
	/**
52
	 * @var \Aimeos\Shop\Base\Aimeos
53
	 * @Flow\Inject
54
	 */
55
	protected $aimeos;
56
57
	/**
58
	 * @var \Aimeos\Shop\Base\Config
59
	 * @Flow\Inject
60
	 */
61
	protected $config;
62
63
	/**
64
	 * @var \Aimeos\Shop\Base\I18n
65
	 * @Flow\Inject
66
	 */
67
	protected $i18n;
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
96
			$context->setConfig( $config );
97
98
			$dbm = new \Aimeos\MW\DB\Manager\DBAL( $config );
99
			$context->setDatabaseManager( $dbm );
100
101
			$fsm = new \Aimeos\MW\Filesystem\Manager\Standard( $config );
102
			$context->setFilesystemManager( $fsm );
103
104
			$mq = new \Aimeos\MW\MQueue\Manager\Standard( $config );
105
			$context->setMessageQueueManager( $mq );
106
107
			$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...
108
			$context->setMail( $mail );
109
110
			$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
111
			$context->setLogger( $logger );
112
113
			$cache = $this->getCache( $context );
114
			$context->setCache( $cache );
115
116
			self::$context = $context;
117
		}
118
119
		$context = self::$context;
120
		$context->setConfig( $config );
121
122
		if( $request !== null )
123
		{
124
			$localeItem = $this->getLocale( $context, $request );
125
			$context->setLocale( $localeItem );
126
127
			$i18n = $this->i18n->get( array( $localeItem->getLanguageId() ) );
128
			$context->setI18n( $i18n );
129
		}
130
131
		$session = new \Aimeos\MW\Session\Flow( $this->session );
132
		$context->setSession( $session );
133
134
		$this->addUser( $context );
135
136
		return $context;
137
	}
138
139
140
	/**
141
	 * Adds the user ID and name if available
142
	 *
143
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
144
	 */
145
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
146
	{
147
		$username = '';
148
149
		$context->setEditor( $username );
150
	}
151
152
153
	/**
154
	 * Returns the cache object for the context
155
	 *
156
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
157
	 * @return \Aimeos\MW\Cache\Iface Cache object
158
	 */
159
	protected function getCache( \Aimeos\MShop\Context\Item\Iface $context )
160
	{
161
		$config = $context->getConfig();
162
163
		switch( $config->get( 'flow/cache/name', 'Flow' ) )
164
		{
165
			case 'None':
166
				$config->set( 'client/html/basket/cache/enable', false );
167
				return \Aimeos\MW\Cache\Factory::createManager( 'None', array(), null );
168
169
			case 'Flow':
170
				return new \Aimeos\MAdmin\Cache\Proxy\Flow( $context, $this->cache );
171
172
			default:
173
				return new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
174
		}
175
	}
176
177
178
	/**
179
	 * Returns the locale item for the current request
180
	 *
181
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
182
	 * @param \TYPO3\Flow\Mvc\RequestInterface $request Request object
183
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
184
	 */
185
	protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context, \TYPO3\Flow\Mvc\RequestInterface $request )
186
	{
187
		if( $this->locale === null )
188
		{
189
			$params = $request->getArguments();
190
191
			$site = ( isset( $params['site'] ) ? $params['site'] : 'default' );
192
			$lang = ( isset( $params['locale'] ) ? $params['locale'] : '' );
193
			$currency = ( isset( $params['currency'] ) ? $params['currency'] : '' );
194
195
			$disableSites = (bool) ( isset( $this->settings['flow']['disableSites'] ) ? $this->settings['flow']['disableSites'] : true );
196
197
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
198
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
199
		}
200
201
		return $this->locale;
202
	}
203
204
205
	/**
206
	 * Inject the settings
207
	 *
208
	 * @param array $settings
209
	 * @return void
210
	 */
211
	public function injectSettings( array $settings )
212
	{
213
		$this->settings = $settings;
214
	}
215
216
217
	/**
218
	 * Sets the Aimeos shop cache
219
	 *
220
	 * @param \TYPO3\Flow\Cache\Frontend\StringFrontend $cache Cache for shop data
221
	 * @return void
222
	 */
223
	public function setCache( \TYPO3\Flow\Cache\Frontend\StringFrontend $cache )
224
	{
225
		$this->cache = $cache;
226
	}
227
}
228