Completed
Push — master ( 462bed...960354 )
by Aimeos
11:15
created

Context   B

Complexity

Total Complexity 13

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 12
Bugs 2 Features 3
Metric Value
wmc 13
c 12
b 2
f 3
lcom 1
cbo 16
dl 0
loc 163
rs 8.4615

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addUser() 0 16 3
A getConfig() 0 13 3
B getLocale() 0 24 3
B get() 0 45 3
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
/**
14
 * Service providing the context objects
15
 *
16
 * @package laravel
17
 * @subpackage Base
18
 */
19
class Context
20
{
21
	/**
22
	 * @var \Aimeos\MShop\Context\Item\Iface
23
	 */
24
	private static $context;
25
26
	/**
27
	 * @var \Illuminate\Contracts\Config\Repository
28
	 */
29
	private $config;
30
31
	/**
32
	 * @var \Aimeos\MShop\Locale\Item\Iface
33
	 */
34
	private $locale;
35
36
	/**
37
	 * @var \Illuminate\Session\Store
38
	 */
39
	private $session;
40
41
42
	/**
43
	 * Initializes the object
44
	 *
45
	 * @param \Illuminate\Contracts\Config\Repository $config Configuration object
46
	 * @param \Illuminate\Session\Store $session Laravel session object
47
	 */
48
	public function __construct( \Illuminate\Contracts\Config\Repository $config, \Illuminate\Session\Store $session )
49
	{
50
		$this->config = $config;
51
		$this->session = $session;
52
	}
53
54
55
	/**
56
	 * Returns the current context
57
	 *
58
	 * @param boolean $locale True to add locale object to context, false if not
59
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
60
	 */
61
	public function get( $locale = true )
62
	{
63
		if( self::$context === null )
64
		{
65
			$context = new \Aimeos\MShop\Context\Item\Standard();
66
67
			$config = $this->getConfig();
68
			$context->setConfig( $config );
69
70
			$dbm = new \Aimeos\MW\DB\Manager\PDO( $config );
71
			$context->setDatabaseManager( $dbm );
72
73
			$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) );
74
			$context->setFilesystemManager( $fs );
75
76
			$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
77
			$context->setMail( $mail );
78
79
			$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
80
			$context->setLogger( $logger );
81
82
			$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
83
			$context->setCache( $cache );
84
85
			self::$context = $context;
86
		}
87
88
		$context = self::$context;
89
90
		if( $locale === true )
91
		{
92
			$localeItem = $this->getLocale( $context );
93
			$langid = $localeItem->getLanguageId();
94
95
			$context->setLocale( $localeItem );
96
			$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $langid ) ) );
97
		}
98
99
		$session = new \Aimeos\MW\Session\Laravel4( $this->session );
100
		$context->setSession( $session );
101
102
		$this->addUser( $context );
103
104
		return $context;
105
	}
106
107
108
	/**
109
	 * Adds the user ID and name if available
110
	 *
111
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
112
	 */
113
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
114
	{
115
		if( ( $userid = \Auth::id() ) !== null )
116
		{
117
			$context->setUserId( $userid );
118
			$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...
119
			{
120
				$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
121
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
122
			} );
123
		}
124
125
		if( ( $user = \Auth::user() ) !== null ) {
126
			$context->setEditor( $user->name );
127
		}
128
	}
129
130
131
	/**
132
	 * Creates a new configuration object.
133
	 *
134
	 * @return \Aimeos\MW\Config\Iface Configuration object
135
	 */
136
	protected function getConfig()
137
	{
138
		$conf = $this->config->get( 'shop' );
139
140
		$configPaths = app( '\Aimeos\Shop\Base\Aimeos' )->get()->getConfigPaths( 'mysql' );
141
		$config = new \Aimeos\MW\Config\PHPArray( $conf, $configPaths );
142
143
		if( function_exists( 'apc_store' ) === true && $this->config->get( 'shop.apc_enabled', false ) == true ) {
144
			$config = new \Aimeos\MW\Config\Decorator\APC( $config, $this->config->get( 'shop.apc_prefix', 'laravel:' ) );
145
		}
146
147
		return $config;
148
	}
149
150
151
	/**
152
	 * Returns the locale item for the current request
153
	 *
154
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
155
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
156
	 */
157
	protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context )
158
	{
159
		if( $this->locale === null )
160
		{
161
			if( \Route::current() !== null )
162
			{
163
				$site = \Route::input( 'site', 'default' );
164
				$lang = \Route::input( 'locale', '' );
165
				$currency = \Route::input( 'currency', '' );
166
			}
167
			else
168
			{
169
				$site = 'default';
170
				$lang = $currency = '';
171
			}
172
173
			$disableSites = $this->config->has( 'shop.disableSites' );
174
175
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
176
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
0 ignored issues
show
Bug introduced by
It seems like $site defined by \Route::input('site', 'default') on line 163 can also be of type object; however, Aimeos\MShop\Locale\Manager\Iface::bootstrap() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $lang defined by \Route::input('locale', '') on line 164 can also be of type object; however, Aimeos\MShop\Locale\Manager\Iface::bootstrap() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $currency defined by \Route::input('currency', '') on line 165 can also be of type object; however, Aimeos\MShop\Locale\Manager\Iface::bootstrap() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
177
		}
178
179
		return $this->locale;
180
	}
181
}
182