Completed
Push — master ( cf351a...30d0ed )
by Aimeos
12:17
created

Context::addMailer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
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
15
16
/**
17
 * Service providing the context objects
18
 *
19
 * @package laravel
20
 * @subpackage Base
21
 */
22
class Context
23
{
24
	/**
25
	 * @var \Aimeos\MShop\Context\Item\Iface
26
	 */
27
	private $context;
28
29
	/**
30
	 * @var \Aimeos\Shop\Base\Config
31
	 */
32
	private $config;
33
34
	/**
35
	 * @var \Aimeos\Shop\Base\I18n
36
	 */
37
	private $i18n;
38
39
	/**
40
	 * @var \Aimeos\Shop\Base\Locale
41
	 */
42
	private $locale;
43
44
	/**
45
	 * @var \Illuminate\Session\Store
46
	 */
47
	private $session;
48
49
50
	/**
51
	 * Initializes the object
52
	 *
53
	 * @param \Illuminate\Session\Store $session Laravel session object
54
	 * @param \Aimeos\Shop\Base\Config $config Configuration object
55
	 * @param \Aimeos\Shop\Base\Locale $locale Locale object
56
	 * @param \Aimeos\Shop\Base\I18n $i18n Internationalisation object
57
	 */
58
	public function __construct( \Illuminate\Session\Store $session, \Aimeos\Shop\Base\Config $config, \Aimeos\Shop\Base\Locale $locale, \Aimeos\Shop\Base\I18n $i18n )
59
	{
60
		$this->session = $session;
61
		$this->config = $config;
62
		$this->locale = $locale;
63
		$this->i18n = $i18n;
64
	}
65
66
67
	/**
68
	 * Returns the current context
69
	 *
70
	 * @param boolean $locale True to add locale object to context, false if not (deprecated, use \Aimeos\Shop\Base\Locale)
71
	 * @param string $type Configuration type, i.e. "frontend" or "backend" (deprecated, use \Aimeos\Shop\Base\Config)
72
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
73
	 */
74
	public function get( $locale = true, $type = 'frontend' )
75
	{
76
		$config = $this->config->get( $type );
77
78
		if( $this->context === null )
79
		{
80
			$context = new \Aimeos\MShop\Context\Item\Standard();
81
			$context->setConfig( $config );
82
83
			$this->addDataBaseManager( $context );
84
			$this->addFilesystemManager( $context );
85
			$this->addMessageQueueManager( $context );
86
			$this->addLogger( $context );
87
			$this->addCache( $context );
88
			$this->addMailer( $context);
89
			$this->addSession( $context );
90
			$this->addUser( $context);
91
			$this->addGroups( $context);
92
93
			$this->context = $context;
94
		}
95
96
		$this->context->setConfig( $config );
97
98
		if( $locale === true )
99
		{
100
			$localeItem = $this->locale->get( $this->context );
101
			$this->context->setLocale( $localeItem );
102
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
103
		}
104
105
		return $this->context;
106
	}
107
108
109
	/**
110
	 * Adds the cache object to the context
111
	 *
112
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
113
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
114
	 */
115
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
116
	{
117
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
118
		$context->setCache( $cache );
119
120
		return $context;
121
	}
122
123
124
	/**
125
	 * Adds the database manager object to the context
126
	 *
127
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
128
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
129
	 */
130
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context )
131
	{
132
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
133
		$context->setDatabaseManager( $dbm );
134
135
		return $context;
136
	}
137
138
139
	/**
140
	 * Adds the filesystem manager object to the context
141
	 *
142
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
143
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
144
	 */
145
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
146
	{
147
		$config = $context->getConfig();
148
		$path = storage_path( 'aimeos' );
149
150
		$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, $path );
151
		$context->setFilesystemManager( $fs );
152
153
		return $context;
154
	}
155
156
157
	/**
158
	 * Adds the logger object to the context
159
	 *
160
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
161
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
162
	 */
163
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
164
	{
165
		$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context );
166
		$context->setLogger( $logger );
167
168
		return $context;
169
	}
170
171
172
173
	/**
174
	 * Adds the mailer object to the context
175
	 *
176
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
177
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
178
	 */
179
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
180
	{
181
		$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
182
		$context->setMail( $mail );
183
184
		return $context;
185
	}
186
187
188
	/**
189
	 * Adds the message queue manager object to the context
190
	 *
191
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
192
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
193
	 */
194
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
195
	{
196
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
197
		$context->setMessageQueueManager( $mq );
198
199
		return $context;
200
	}
201
202
203
	/**
204
	 * Adds the session object to the context
205
	 *
206
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
207
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
208
	 */
209
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
210
	{
211
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
212
		$context->setSession( $session );
213
214
		return $context;
215
	}
216
217
218
	/**
219
	 * Adds the user ID and name if available
220
	 *
221
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
222
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
223
	 */
224
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
225
	{
226
		if( ( $userid = Auth::id() ) !== null ) {
227
			$context->setUserId( $userid );
228
		}
229
230
		if( ( $user = Auth::user() ) !== null ) {
231
			$context->setEditor( $user->name );
0 ignored issues
show
Bug introduced by
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
232
		}
233
234
		return $context;
235
	}
236
237
238
	/**
239
	 * Adds the group IDs if available
240
	 *
241
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
242
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
243
	 */
244
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context )
245
	{
246
		if( ( $userid = Auth::id() ) !== null )
247
		{
248
			$context->setGroupIds( function() use ( $context, $userid )
249
			{
250
				$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' );
251
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
252
			} );
253
		}
254
255
		return $context;
256
	}
257
}
258