Completed
Push — master ( 3df36a...73a491 )
by Aimeos
03:04
created

Context::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 48
rs 9.125
cc 3
eloc 28
nc 4
nop 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
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
			$mq = new \Aimeos\MW\MQueue\Manager\Standard( $config );
81
			$context->setMessageQueueManager( $mq );
0 ignored issues
show
Bug introduced by
The method setMessageQueueManager() does not seem to exist on object<Aimeos\MShop\Context\Item\Standard>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
83
			$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
84
			$context->setMail( $mail );
85
86
			$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
87
			$context->setLogger( $logger );
88
89
			$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
90
			$context->setCache( $cache );
91
92
			self::$context = $context;
93
		}
94
95
		$context = self::$context;
96
97
		if( $locale === true )
98
		{
99
			$localeItem = $this->getLocale( $context );
100
			$langid = $localeItem->getLanguageId();
101
102
			$context->setLocale( $localeItem );
103
			$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $langid ) ) );
104
		}
105
106
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
107
		$context->setSession( $session );
108
109
		$this->addUser( $context );
110
111
		return $context;
112
	}
113
114
115
	/**
116
	 * Adds the user ID and name if available
117
	 *
118
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
119
	 */
120
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
121
	{
122
		if( ( $userid = Auth::id() ) !== null )
123
		{
124
			$context->setUserId( $userid );
125
			$context->setGroupIds( function() use ( $context, $userid )
126
			{
127
				$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
128
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
129
			} );
130
		}
131
132
		if( ( $user = Auth::user() ) !== null ) {
133
			$context->setEditor( $user->name );
134
		}
135
	}
136
137
138
	/**
139
	 * Creates a new configuration object.
140
	 *
141
	 * @return \Aimeos\MW\Config\Iface Configuration object
142
	 */
143
	protected function getConfig()
144
	{
145
		$configPaths = app( '\Aimeos\Shop\Base\Aimeos' )->get()->getConfigPaths();
146
		$config = new \Aimeos\MW\Config\PHPArray( $this->config->get( 'shop' ), $configPaths );
147
148 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...
149
			$config = new \Aimeos\MW\Config\Decorator\APC( $config, $this->config->get( 'shop.apc_prefix', 'laravel:' ) );
150
		}
151
152
		return $config;
153
	}
154
155
156
	/**
157
	 * Returns the locale item for the current request
158
	 *
159
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
160
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
161
	 */
162
	protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context )
163
	{
164
		if( $this->locale === null )
165
		{
166
			if( Route::current() !== null )
167
			{
168
				$site = Route::input( 'site', 'default' );
169
				$lang = Route::input( 'locale', '' );
170
				$currency = Route::input( 'currency', '' );
171
			}
172
			else
173
			{
174
				$site = 'default';
175
				$lang = $currency = '';
176
			}
177
178
			$disableSites = $this->config->has( 'shop.disableSites' );
179
180
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
181
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
182
		}
183
184
		return $this->locale;
185
	}
186
}
187