MakePermission   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addToDB() 0 34 5
A makePolicy() 0 21 5
A makePermission() 0 4 1
1
<?php
2
3
namespace Pratiksh\Adminetic\Services;
4
5
use Illuminate\Support\Str;
6
use Pratiksh\Adminetic\Models\Admin\Permission;
7
use Pratiksh\Adminetic\Models\Admin\Role;
8
use Pratiksh\Adminetic\Services\Helper\CommandHelper;
9
10
class MakePermission extends CommandHelper
11
{
12
    public static function makePermission($name, $role, $for_all, $only_flags = false)
13
    {
14
        self::addToDB($name, $role, $for_all);
15
        self::makePolicy($name, $only_flags);
16
    }
17
18
    // Add Permission Flags to DB
19
    protected static function addToDB($name, $role, $for_all)
20
    {
21
        // Permission for model existence check
22
        $permissions = Permission::all(['id', 'model']);
23
        foreach ($permissions as $permission) {
24
            if ($permission->name == $name) {
25
                break;
26
27
                return false;
0 ignored issues
show
Unused Code introduced by
return false is not 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
        }
30
31
        if ($for_all) {
32
            $roles = Role::all();
33
            foreach ($roles as $r) {
34
                Permission::create([
35
                    'browse' => 1,
36
                    'read' => 1,
37
                    'edit' => 1,
38
                    'add' => 1,
39
                    'delete' => 1,
40
                    'role_id' => $r->id,
41
                    'model' => $name,
42
                ]);
43
            }
44
        } else {
45
            Permission::create([
46
                'browse' => 1,
47
                'read' => 1,
48
                'edit' => 1,
49
                'add' => 1,
50
                'delete' => 1,
51
                'role_id' => $role,
52
                'model' => $name,
53
            ]);
54
        }
55
    }
56
57
    // Make a Policy
58
    protected static function makePolicy($name, $only_flags)
59
    {
60
        if (! $only_flags) {
61
            if (trim($name) != 'User' || trim($name) != 'user') {
62
                if (! file_exists($path = app_path('Policies'))) {
63
                    mkdir($path, 0777, true);
64
                }
65
                $policyTemplate = str_replace(
66
                    [
67
                        '{{modelName}}',
68
                        '{{modelNamePluralLowerCase}}',
69
                        '{{modelNameSingularLowerCase}}',
70
                    ],
71
                    [
72
                        $name,
73
                        strtolower(Str::plural($name)),
74
                        strtolower($name),
75
                    ],
76
                    self::getStub('Policy')
77
                );
78
                file_put_contents(app_path("/Policies/{$name}Policy.php"), $policyTemplate);
79
            }
80
        }
81
    }
82
}
83