Completed
Push — master ( 67c4a6...e61cbc )
by Aimeos
01:50
created

src/Aimeos/Shop/Base/Context.php (3 issues)

Labels

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 );
0 ignored issues
show
It seems like $config defined by $this->config->get($type) on line 76 can also be of type object<Aimeos\Shop\Base\Config>; however, Aimeos\MShop\Context\Item\Standard::setConfig() does only seem to accept object<Aimeos\MW\Config\Iface>, 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...
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->addProcess( $context );
90
			$this->addSession( $context );
91
			$this->addUser( $context);
92
			$this->addGroups( $context);
93
94
			$this->context = $context;
95
		}
96
97
		$this->context->setConfig( $config );
0 ignored issues
show
It seems like $config defined by $this->config->get($type) on line 76 can also be of type object<Aimeos\Shop\Base\Config>; however, Aimeos\MShop\Context\Item\Iface::setConfig() does only seem to accept object<Aimeos\MW\Config\Iface>, 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...
98
99
		if( $locale === true )
100
		{
101
			$localeItem = $this->locale->get( $this->context );
102
			$this->context->setLocale( $localeItem );
103
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
104
		}
105
106
		return $this->context;
107
	}
108
109
110
	/**
111
	 * Adds the cache object to the context
112
	 *
113
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
114
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
115
	 */
116
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context )
117
	{
118
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
119
120
		return $context->setCache( $cache );
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
134
		return $context->setDatabaseManager( $dbm );
135
	}
136
137
138
	/**
139
	 * Adds the filesystem manager object to the context
140
	 *
141
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
142
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
143
	 */
144
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context )
145
	{
146
		$config = $context->getConfig();
147
		$path = storage_path( 'aimeos' );
148
149
		$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, $path );
150
151
		return $context->setFilesystemManager( $fs );
152
	}
153
154
155
	/**
156
	 * Adds the logger object to the context
157
	 *
158
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
159
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
160
	 */
161
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context )
162
	{
163
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
164
165
		return $context->setLogger( $logger );
166
	}
167
168
169
170
	/**
171
	 * Adds the mailer object to the context
172
	 *
173
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
174
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
175
	 */
176
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context )
177
	{
178
		$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
179
180
		return $context->setMail( $mail );
181
	}
182
183
184
	/**
185
	 * Adds the message queue manager object to the context
186
	 *
187
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
188
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
189
	 */
190
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context )
191
	{
192
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
193
194
		return $context->setMessageQueueManager( $mq );
195
	}
196
197
198
	/**
199
	 * Adds the process object to the context
200
	 *
201
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
202
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
203
	 */
204
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context )
205
	{
206
		$config = $context->getConfig();
207
		$max = $config->get( 'pcntl_max', 4 );
208
		$prio = $config->get( 'pcntl_priority', 19 );
209
210
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
211
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
212
213
		return $context->setProcess( $process );
214
	}
215
216
217
	/**
218
	 * Adds the session object to the context
219
	 *
220
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
221
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
222
	 */
223
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context )
224
	{
225
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
226
227
		return $context->setSession( $session );
228
	}
229
230
231
	/**
232
	 * Adds the user ID and name if available
233
	 *
234
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
235
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
236
	 */
237
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context )
238
	{
239
		if( ( $userid = Auth::id() ) !== null ) {
240
			$context->setUserId( $userid );
241
		}
242
243
		if( ( $user = Auth::user() ) !== null ) {
244
			$context->setEditor( $user->name );
0 ignored issues
show
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...
245
		} else {
246
			$context->setEditor( \Request::ip() );
247
		}
248
249
		return $context;
250
	}
251
252
253
	/**
254
	 * Adds the group IDs if available
255
	 *
256
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
257
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
258
	 */
259
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context )
260
	{
261
		if( ( $userid = Auth::id() ) !== null )
262
		{
263
			$context->setGroupIds( function() use ( $context, $userid )
264
			{
265
				$manager = \Aimeos\MShop::create( $context, 'customer' );
266
				return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups();
267
			} );
268
		}
269
270
		return $context;
271
	}
272
}
273