Permission::has()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\Roles\Http\Middleware;
4
5
use Closure;
6
use Helldar\Roles\Exceptions\Http\PermissionAccessIsDeniedHttpException;
7
8
class Permission extends BaseMiddleware
9
{
10
    /**
11
     * Checks for the entry of one of the specified permissions.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @param  string  ...$permissions
16
     *
17
     * @throws \Helldar\Roles\Exceptions\Http\PermissionAccessIsDeniedHttpException
18
     *
19
     * @return mixed
20
     */
21 30
    public function handle($request, Closure $next, ...$permissions)
22
    {
23 30
        $this->check();
24
25 18
        if ($this->hasRoot($request) || $this->has($request, $permissions)) {
26 18
            return $next($request);
27
        }
28
29 6
        throw new PermissionAccessIsDeniedHttpException();
30
    }
31
32
    /**
33
     * @param  \Illuminate\Http\Request  $request
34
     * @param  array  $permissions
35
     *
36
     * @return bool
37
     */
38 6
    protected function has($request, array $permissions): bool
39
    {
40 6
        foreach ($permissions as $permission) {
41 6
            if ($request->user()->hasPermission($permission)) {
42 6
                return true;
43
            }
44
        }
45
46 6
        return false;
47
    }
48
}
49