AuthorizeRoutePermission   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 13.19 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 0
dl 12
loc 91
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
C handle() 0 32 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Anomaly\UsersModule\Http\Middleware;
2
3
use Anomaly\Streams\Platform\Message\MessageBag;
4
use Anomaly\Streams\Platform\Support\Authorizer;
5
use Closure;
6
use Illuminate\Contracts\Auth\Guard;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Redirector;
9
use Illuminate\Routing\Route;
10
11
/**
12
 * Class AuthorizeRoutePermission
13
 *
14
 * @link          http://pyrocms.com/
15
 * @author        PyroCMS, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 */
18
class AuthorizeRoutePermission
19
{
20
21
    /**
22
     * The route object.
23
     *
24
     * @var Route
25
     */
26
    protected $route;
27
28
    /**
29
     * The redirect utility.
30
     *
31
     * @var Redirector
32
     */
33
    protected $redirect;
34
35
    /**
36
     * The message bag.
37
     *
38
     * @var MessageBag
39
     */
40
    protected $messages;
41
    /**
42
     * The authorizer utility.
43
     *
44
     * @var Authorizer
45
     */
46
    protected $authorizer;
47
48
    /**
49
     * Create a new AuthorizeModuleAccess instance.
50
     *
51
     * @param Route      $route
52
     * @param Redirector $redirect
53
     * @param MessageBag $messages
54
     * @param Authorizer $authorizer
55
     */
56 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
57
        Guard $auth,
0 ignored issues
show
Unused Code introduced by
The parameter $auth is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
        Route $route,
59
        Redirector $redirect,
60
        MessageBag $messages,
61
        Authorizer $authorizer
62
    ) {
63
        $this->route      = $route;
64
        $this->redirect   = $redirect;
65
        $this->messages   = $messages;
66
        $this->authorizer = $authorizer;
67
    }
68
69
    /**
70
     * Check the authorization of module access.
71
     *
72
     * @param  Request                           $request
73
     * @param  \Closure                          $next
74
     * @return \Illuminate\Http\RedirectResponse
75
     */
76
    public function handle(Request $request, Closure $next)
77
    {
78
        if (in_array($request->path(), ['admin/login', 'admin/logout'])) {
79
            return $next($request);
80
        }
81
82
        if ($request->segment(1) == 'admin' && !$this->authorizer->authorize(
83
                'anomaly.module.users::general.control_panel'
84
            )
85
        ) {
86
            abort(403);
87
        }
88
89
        $permission = array_get($this->route->getAction(), 'anomaly.module.users::permission');
90
        $redirect   = array_get($this->route->getAction(), 'anomaly.module.users::redirect');
91
        $message    = array_get($this->route->getAction(), 'anomaly.module.users::message');
92
93
        if ($permission && !$this->authorizer->authorizeAny((array)$permission, null, true)) {
94
95
            if ($message) {
96
                $this->messages->error($message);
97
            }
98
99
            if ($redirect) {
100
                return $this->redirect->to($redirect);
101
            }
102
103
            abort(403);
104
        }
105
106
        return $next($request);
107
    }
108
}
109