AdminMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 6
nc 1
nop 5
1
<?php namespace Modules\Core\Http\Middleware;
2
3
use Illuminate\Foundation\Application;
4
use Illuminate\Http\Request;
5
use Illuminate\Routing\Redirector;
6
use Illuminate\Session\Store;
7
use Modules\Core\Contracts\Authentication;
8
9
class AdminMiddleware
10
{
11
    /**
12
     * @var Authentication
13
     */
14
    private $auth;
15
    /**
16
     * @var SessionManager
17
     */
18
    private $session;
19
    /**
20
     * @var Request
21
     */
22
    private $request;
23
    /**
24
     * @var Redirector
25
     */
26
    private $redirect;
27
    /**
28
     * @var Application
29
     */
30
    private $application;
31
32
    public function __construct(Authentication $auth, Store $session, Request $request, Redirector $redirect, Application $application)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
33
    {
34
        $this->auth = $auth;
35
        $this->session = $session;
0 ignored issues
show
Documentation Bug introduced by
It seems like $session of type object<Illuminate\Session\Store> is incompatible with the declared type object<Modules\Core\Http...dleware\SessionManager> of property $session.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->request = $request;
37
        $this->redirect = $redirect;
38
        $this->application = $application;
39
    }
40
41
    /**
42
     * Handle an incoming request.
43
     *
44
     * @param  \Illuminate\Http\Request $request
45
     * @param  \Closure                 $next
46
     * @return mixed
47
     */
48
    public function handle($request, \Closure $next)
49
    {
50
        // Check if the user is logged in
51
        if (!$this->auth->check()) {
52
            // Store the current uri in the session
53
            $this->session->put('url.intended', $this->request->url());
54
55
            // Redirect to the login page
56
            return $this->redirect->route('login');
57
        }
58
59
        // Check if the user has access to the dashboard page
60
        if (! $this->auth->hasAccess('dashboard.index')) {
61
            // Show the insufficient permissions page
62
            return $this->application->abort(403);
63
        }
64
65
        return $next($request);
66
    }
67
}
68