Completed
Push — master ( 78b3ab...6795be )
by wen
15:30
created

ModelAuthorize   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 41
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
B handle() 16 16 5

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
2
3
namespace Sco\Admin\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Auth\Access\AuthorizationException;
7
use Route;
8
use Illuminate\Contracts\Auth\Factory as Auth;
9
10 View Code Duplication
class ModelAuthorize
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    const DELIMITER = '|';
13
14
    protected $auth;
15
16
    /**
17
     * Create a new middleware instance.
18
     *
19
     * @param  \Illuminate\Contracts\Auth\Factory $auth
20
     */
21
    public function __construct(Auth $auth)
22
    {
23
        $this->auth = $auth;
24
    }
25
26
    /**
27
     * @param \Illuminate\Http\Request $request
28
     * @param Closure                  $next
29
     * @param string                   $permissions
30
     *
31
     * @return mixed
32
     * @throws \Illuminate\Auth\Access\AuthorizationException
33
     */
34
    public function handle($request, Closure $next, $permissions = '')
35
    {
36
        if (empty($permissions)) {
37
            throw new AuthorizationException();
38
        }
39
40
        if (!is_array($permissions)) {
41
            $permissions = explode(self::DELIMITER, $permissions);
42
        }
43
44
        if ($this->auth->guest() || !$request->route('model')->getPermissions()->can($permissions)) {
0 ignored issues
show
Bug introduced by
The method guest() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
            throw new AuthorizationException();
46
        }
47
48
        return $next($request);
49
    }
50
}
51