Completed
Push — dev ( 917cb5...992eab )
by Marc
10:46
created

ModelPermit::routeAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
1
<?php namespace Mascame\Artificer\Permit;
2
3
use App;
4
use Mascame\Artificer\Artificer;
5
use Mascame\Artificer\Model\Model;
6
use Mascame\Artificer\Options\AdminOption;
7
8
class ModelPermit extends Permit
9
{
10
11
    protected static $actions = array('create', 'update', 'delete', 'view');
12
13 View Code Duplication
    public static function access($model = null)
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...
14
    {
15
        return true;
16
17
        if (! $model) $model = Model::getCurrent()->name;
0 ignored issues
show
Unused Code introduced by
if (!$model) { $mode...::getCurrent()->name; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
Bug introduced by
The variable $model seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
18
19
        $modelPermissions = Artificer::getModel()->getOption('permissions', [], $model);
20
21
        return self::hasPermission($modelPermissions);
22
    }
23
24 View Code Duplication
    public static function to($action)
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...
25
    {
26
        return true;
27
        $model = Model::getCurrent()->name;
0 ignored issues
show
Unused Code introduced by
$model = \Mascame\Artifi...el::getCurrent()->name; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
28
29
        $modelPermissions = Artificer::getModel()->getOption('action_permissions.' . $action, [], $model);
30
31
        return self::hasPermission($modelPermissions);
32
    }
33
34
    public static function routeAction($route)
35
    {
36
        $route_permission = AdminOption::get('model.route_permission');
37
38
        if (in_array($route, array_keys($route_permission))) {
39
            if (! ModelPermit::to($route_permission[$route])) {
40
                App::abort('403', 'Insufficient privileges');
41
            }
42
        }
43
    }
44
45
}