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

ModelAuthorize::handle()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 5
nop 3
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