Passed
Push — feature/permission-manager ( 41e93d )
by
unknown
09:00
created

updatePermissionOptions()   C

Complexity

Conditions 12
Paths 60

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 24
c 2
b 0
f 0
nc 60
nop 3
dl 0
loc 44
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Illuminate\Support\Str;
4
use A17\Twill\Models\Permission;
5
use Illuminate\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Filesystem. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
if (!function_exists('getAllModules')) {
8
    function getAllModules()
9
    {
10
        $repositories = collect(app(FileSystem::class)->glob(app_path('Repositories') . '/*.php'))->map(function ($repository) {
11
            $re = "/(?<=Repositories\/).+(?=\.php)/";
12
            preg_match($re, $repository, $matches);
13
            return config('twill.namespace') . "\\Repositories\\" . $matches[0];
14
        });
15
16
        $moduleRepositories = $repositories->filter(function ($repository) {
17
            return is_subclass_of($repository, 'A17\Twill\Repositories\ModuleRepository');
18
        });
19
20
        $modules = $moduleRepositories->map(function ($repository) {
21
            $modelName = str_replace("Repository", '', str_replace("App\\Repositories\\", "", $repository));
22
            return str_plural(lcfirst($modelName));
0 ignored issues
show
Bug introduced by
The function str_plural was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
            return /** @scrutinizer ignore-call */ str_plural(lcfirst($modelName));
Loading history...
23
        });
24
25
        return $modules;
26
    }
27
}
28
29
if (!function_exists('getModelByModuleName')) {
30
    function getModelByModuleName($moduleName)
31
    {
32
        $model = config('twill.namespace') . '\\Models\\' . Str::studly(Str::singular($moduleName));
33
        if (!class_exists($model)) {
34
            throw new Exception($model . ' not existed');
35
        }
36
        return $model;
37
    }
38
}
39
40
if (!function_exists('getModuleNameByModel')) {
41
    function getModuleNameByModel($model)
42
    {
43
        return Str::plural(lcfirst(class_basename($model)));
44
    }
45
}
46
47
if (!function_exists('getRepositoryByModuleName')) {
48
    function getRepositoryByModuleName($moduleName)
49
    {
50
        return getModelRepository(class_basename(getModelByModuleName($moduleName)));
51
    }
52
}
53
54
if (!function_exists('getModelRepository')) {
55
    function getModelRepository($relation, $model = null)
56
    {
57
        if (!$model) {
58
            $model = ucfirst(Str::singular($relation));
59
        }
60
        $repository = config('twill.namespace') . "\\Repositories\\" . ucfirst($model) . "Repository";
61
62
        if (!class_exists($repository)) {
63
            throw new Exception($repository . ' not found');
64
        }
65
        return app($repository);
66
    }
67
}
68
69
if (!function_exists('isPermissionableModule')) {
70
    /**
71
     * Return the module name if the module is permissionable, otherwise return false
72
     *
73
     * @param string $moduleName
74
     * @return string|boolean
75
     */
76
    function isPermissionableModule($moduleName)
77
    {
78
        $submodule = Permission::permissionableModules()->filter(function($module) use ($moduleName) {
79
            return strpos($module, '.') && explode('.', $module)[1] === $moduleName;
80
        })->first();
81
82
        if (Permission::permissionableModules()->contains($moduleName)) {
83
            return $moduleName;
84
        } elseif ($submodule) {
85
            return $submodule;
86
        } else {
87
            return false;
88
        }
89
    }
90
}
91
92
if (!function_exists('updatePermissionOptions')) {
93
    function updatePermissionOptions($options, $user, $item)
94
    {
95
        $permissions = [];
96
97
        if ($user->role) {
98
            $permissions = $user->role->permissions()->module()->pluck('name','permissionable_type')->all();
99
            if (empty($permissions)) {
100
                if ($user->role->permissions()->global()->where('name', 'manage-modules')->first()){
101
                    $permissions[get_class($item)] = 'manage-item';
102
                }
103
            }
104
        }
105
106
        // looking for group permissions belonging to the user
107
        foreach($user->publishedGroups as $group) {
108
            if (($permission=$group->permissions()->ofItem($item)->first())!= null) {
109
                if (isset($permissions[get_class($item)])) {
110
                    $scopes = Permission::available(Permission::SCOPE_ITEM);
111
                    $previous = array_search($permissions[get_class($item)], $scopes);
112
                    $current = array_search($permission->name, $scopes);
113
114
                    // check permission level
115
                    if ($current > $previous) {
116
                        $permissions[get_class($item)] = $permission->name;
117
                    }
118
119
                } else {
120
                    $permissions[get_class($item)] = $permission->name;
121
                }
122
            }
123
        }
124
125
        if (isset($permissions[get_class($item)])) {
126
            $globalPermission = str_replace('-module', '-item', $permissions[get_class($item)]);
127
            foreach($options as &$option) {
128
                if ($option['value'] != $globalPermission || $globalPermission=='manage-item') {
129
                    $option['disabled'] = true;
130
                } else {
131
                    break;
132
                }
133
            }
134
        }
135
136
        return $options;
137
    }
138
}
139
140
if (!function_exists('updatePermissionGroupOptions')) {
141
    function updatePermissionGroupOptions($options, $item, $group)
0 ignored issues
show
Unused Code introduced by
The parameter $group is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

141
    function updatePermissionGroupOptions($options, $item, /** @scrutinizer ignore-unused */ $group)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

141
    function updatePermissionGroupOptions($options, /** @scrutinizer ignore-unused */ $item, $group)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
    {
143
        return $options;
144
    }
145
}
146
147
if (!function_exists('isUserGroupPermissionItemExists')) {
148
    function isUserGroupPermissionItemExists($user, $item, $permission)
149
    {
150
        foreach($user->publishedGroups as $group) {
151
            if (in_array($permission, $group->permissions()->ofItem($item)->get()->pluck('name')->all())){
152
                return true;
153
            }
154
        }
155
156
        return false;
157
    }
158
}
159
160
if (!function_exists('isUserGroupPermissionModuleExists')) {
161
    function isUserGroupPermissionModuleExists($user, $moduleName, $permission)
162
    {
163
        foreach($user->publishedGroups as $group) {
164
            if ($moduleName=='global') {
165
                return $group->permissions()->global()->where('name', 'manage-modules')->exists();
166
            } else {
167
                if( in_array($permission, $group->permissions()->OfModuleName($moduleName)->get()->pluck('name')->all())){
168
                    return true;
169
                }
170
            }
171
172
        }
173
174
        return false;
175
    }
176
}
177
178
179