Completed
Push — master ( 008b5d...87a11c )
by Aimeos
03:29
created

Context::getConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 3
Ratio 27.27 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 3
loc 11
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
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\Input;
15
use Illuminate\Support\Facades\Route;
16
17
18
/**
19
 * Service providing the context objects
20
 *
21
 * @package laravel
22
 * @subpackage Base
23
 */
24
class Context
25
{
26
	/**
27
	 * @var \Aimeos\MShop\Context\Item\Iface
28
	 */
29
	private static $context;
30
31
	/**
32
	 * @var \Illuminate\Contracts\Config\Repository
33
	 */
34
	private $config;
35
36
	/**
37
	 * @var \Aimeos\MShop\Locale\Item\Iface
38
	 */
39
	private $locale;
40
41
	/**
42
	 * @var \Illuminate\Session\Store
43
	 */
44
	private $session;
45
46
47
	/**
48
	 * Initializes the object
49
	 *
50
	 * @param \Illuminate\Contracts\Config\Repository $config Configuration object
51
	 * @param \Illuminate\Session\Store $session Laravel session object
52
	 */
53
	public function __construct( \Illuminate\Contracts\Config\Repository $config, \Illuminate\Session\Store $session )
54
	{
55
		$this->config = $config;
56
		$this->session = $session;
57
	}
58
59
60
	/**
61
	 * Returns the current context
62
	 *
63
	 * @param boolean $locale True to add locale object to context, false if not
64
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
65
	 */
66
	public function get( $locale = true )
67
	{
68
		if( self::$context === null )
69
		{
70
			$context = new \Aimeos\MShop\Context\Item\Standard();
71
72
			$config = $this->getConfig();
73
			$context->setConfig( $config );
74
75
			$dbm = new \Aimeos\MW\DB\Manager\DBAL( $config );
76
			$context->setDatabaseManager( $dbm );
77
78
			$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, storage_path( 'aimeos' ) );
79
			$context->setFilesystemManager( $fs );
80
81
			$mq = new \Aimeos\MW\MQueue\Manager\Standard( $config );
82
			$context->setMessageQueueManager( $mq );
83
84
			$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
85
			$context->setMail( $mail );
86
87
			$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
88
			$context->setLogger( $logger );
89
90
			$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
91
			$context->setCache( $cache );
92
93
			self::$context = $context;
94
		}
95
96
		$context = self::$context;
97
98
		if( $locale === true )
99
		{
100
			$localeItem = $this->getLocale( $context );
101
			$langid = $localeItem->getLanguageId();
102
103
			$context->setLocale( $localeItem );
104
			$context->setI18n( app('\Aimeos\Shop\Base\I18n')->get( array( $langid ) ) );
105
		}
106
107
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
108
		$context->setSession( $session );
109
110
		$this->addUser( $context );
111
112
		return $context;
113
	}
114
115
116
	/**
117
	 * Adds the user ID and name if available
118
	 *
119
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
120
	 */
121
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
122
	{
123
		if( ( $userid = Auth::id() ) !== null )
124
		{
125
			$context->setUserId( $userid );
126
			$context->setGroupIds( function() use ( $context, $userid )
127
			{
128
				$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
129
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
130
			} );
131
		}
132
133
		if( ( $user = Auth::user() ) !== null ) {
134
			$context->setEditor( $user->name );
135
		}
136
	}
137
138
139
	/**
140
	 * Creates a new configuration object.
141
	 *
142
	 * @return \Aimeos\MW\Config\Iface Configuration object
143
	 */
144
	protected function getConfig()
145
	{
146
		$configPaths = app( '\Aimeos\Shop\Base\Aimeos' )->get()->getConfigPaths();
147
		$config = new \Aimeos\MW\Config\PHPArray( array(), $configPaths );
148
149 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...
150
			$config = new \Aimeos\MW\Config\Decorator\APC( $config, $this->config->get( 'shop.apc_prefix', 'laravel:' ) );
151
		}
152
153
		return new \Aimeos\MW\Config\Decorator\Memory( $config, $this->config->get( 'shop' ) );
154
	}
155
156
157
	/**
158
	 * Returns the locale item for the current request
159
	 *
160
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
161
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
162
	 */
163
	protected function getLocale( \Aimeos\MShop\Context\Item\Iface $context )
164
	{
165
		if( $this->locale === null )
166
		{
167
			$site = Route::input( 'site', Input::get( 'site', 'default' ) );
168
			$currency = Route::input( 'currency', Input::get( 'currency', '' ) );
169
			$lang = Route::input( 'locale', Input::get( 'locale', '' ) );
170
171
			$disableSites = $this->config->get( 'shop.disableSites', true );
172
173
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
174
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
175
		}
176
177
		return $this->locale;
178
	}
179
}
180