Completed
Push — master ( da2dea...5ebe79 )
by Aimeos
22:25
created

Context::getLocale()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 24
rs 8.9713
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015
6
 * @package laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\Support\Facades\Route;
15
16
17
/**
18
 * Service providing the context objects
19
 *
20
 * @package laravel
21
 * @subpackage Base
22
 */
23
class Context
24
{
25
	/**
26
	 * @var \Aimeos\MShop\Context\Item\Iface
27
	 */
28
	private static $context;
29
30
	/**
31
	 * @var \Illuminate\Contracts\Config\Repository
32
	 */
33
	private $config;
34
35
	/**
36
	 * @var \Aimeos\MShop\Locale\Item\Iface
37
	 */
38
	private $locale;
39
40
	/**
41
	 * @var \Illuminate\Session\Store
42
	 */
43
	private $session;
44
45
46
	/**
47
	 * Initializes the object
48
	 *
49
	 * @param \Illuminate\Contracts\Config\Repository $config Configuration object
50
	 * @param \Illuminate\Session\Store $session Laravel session object
51
	 */
52
	public function __construct( \Illuminate\Contracts\Config\Repository $config, \Illuminate\Session\Store $session )
53
	{
54
		$this->config = $config;
55
		$this->session = $session;
56
	}
57
58
59
	/**
60
	 * Returns the current context
61
	 *
62
	 * @param boolean $locale True to add locale object to context, false if not
63
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
64
	 */
65
	public function get( $locale = true )
66
	{
67
		if( self::$context === null )
68
		{
69
			$context = new \Aimeos\MShop\Context\Item\Standard();
70
71
			$config = $this->getConfig();
72
			$context->setConfig( $config );
73
74
			$dbm = new \Aimeos\MW\DB\Manager\PDO( $config );
75
			$context->setDatabaseManager( $dbm );
76
77
			$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) );
78
			$context->setFilesystemManager( $fs );
79
80
			$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
81
			$context->setMail( $mail );
82
83
			$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
84
			$context->setLogger( $logger );
85
86
			$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
87
			$context->setCache( $cache );
88
89
			self::$context = $context;
90
		}
91
92
		$context = self::$context;
93
94
		if( $locale === true )
95
		{
96
			$localeItem = $this->getLocale( $context );
97
			$langid = $localeItem->getLanguageId();
98
99
			$context->setLocale( $localeItem );
100
			$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $langid ) ) );
101
		}
102
103
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
104
		$context->setSession( $session );
105
106
		$this->addUser( $context );
107
108
		return $context;
109
	}
110
111
112
	/**
113
	 * Adds the user ID and name if available
114
	 *
115
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
116
	 */
117
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
118
	{
119
		if( ( $userid = Auth::id() ) !== null )
120
		{
121
			$context->setUserId( $userid );
122
			$context->setGroupIds( function() use ( $context, $userid )
0 ignored issues
show
Documentation introduced by
function () use($context...roup'))->getGroups(); } is of type object<Closure>, but the function expects a object<Aimeos\MShop\Context\Item\closure>|array.

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...
123
			{
124
				$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
125
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
126
			} );
127
		}
128
129
		if( ( $user = Auth::user() ) !== null ) {
130
			$context->setEditor( $user->name );
131
		}
132
	}
133
134
135
	/**
136
	 * Creates a new configuration object.
137
	 *
138
	 * @return \Aimeos\MW\Config\Iface Configuration object
139
	 */
140
	protected function getConfig()
141
	{
142
		$conf = $this->config->get( 'shop' );
143
144
		$configPaths = app( '\Aimeos\Shop\Base\Aimeos' )->get()->getConfigPaths( 'mysql' );
145
		$config = new \Aimeos\MW\Config\PHPArray( $conf, $configPaths );
146
147 View Code Duplication
		if( function_exists( 'apc_store' ) === true && $this->config->get( 'shop.apc_enabled', false ) == true ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
			$config = new \Aimeos\MW\Config\Decorator\APC( $config, $this->config->get( 'shop.apc_prefix', 'laravel:' ) );
149
		}
150
151
		return $config;
152
	}
153
154
155
	/**
156
	 * Returns the locale item for the current request
157
	 *
158
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
159
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
160
	 */
161
	protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context )
162
	{
163
		if( $this->locale === null )
164
		{
165
			if( Route::current() !== null )
166
			{
167
				$site = Route::input( 'site', 'default' );
168
				$lang = Route::input( 'locale', '' );
169
				$currency = Route::input( 'currency', '' );
170
			}
171
			else
172
			{
173
				$site = 'default';
174
				$lang = $currency = '';
175
			}
176
177
			$disableSites = $this->config->has( 'shop.disableSites' );
178
179
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
180
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
181
		}
182
183
		return $this->locale;
184
	}
185
}
186