Completed
Push — master ( a9d673...d0cde4 )
by Arjay
06:58
created

PermissionMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 0
dl 29
loc 29
ccs 0
cts 16
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 18 18 4

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 Yajra\Acl\Middleware;
4
5
use Closure;
6
7 View Code Duplication
class PermissionMiddleware
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...
8
{
9
    /**
10
     * Handle an incoming request.
11
     *
12
     * @param  \Illuminate\Http\Request $request
13
     * @param  \Closure $next
14
     * @param  string $permission
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next, $permission)
18
    {
19
        if (! $request->user() || ! $request->user()->can($permission)) {
20
            if ($request->ajax()) {
21
                return response()->json([
22
                    'error' => [
23
                        'status_code' => 401,
24
                        'code'        => 'INSUFFICIENT_PERMISSIONS',
25
                        'description' => 'You are not authorized to access this resource.',
26
                    ],
27
                ], 401);
28
            }
29
30
            return abort(401, 'You are not authorized to access this resource.');
31
        }
32
33
        return $next($request);
34
    }
35
}
36