ActionsManager::actions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Terranet\Administrator;
4
5
use Illuminate\Database\Eloquent\Model;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Model was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\Cache;
7
use Illuminate\Support\Str;
8
use Terranet\Administrator\Actions\Collection;
9
use Terranet\Administrator\Contracts\ActionsManager as ActionsManagerContract;
10
use Terranet\Administrator\Contracts\Module;
11
use Terranet\Administrator\Contracts\Services\CrudActions;
12
13
class ActionsManager implements ActionsManagerContract
14
{
15
    /**
16
     * @var CrudActions
17
     */
18
    protected $service;
19
20
    /**
21
     * @var Module
22
     */
23
    protected $module;
24
25
    /**
26
     * List of item-related actions.
27
     *
28
     * @var array
29
     */
30
    protected $actions;
31
32
    /**
33
     * List of global actions.
34
     *
35
     * @var array
36
     */
37
    protected $globalActions;
38
39
    /**
40
     * Check if resource is readonly - has no actions.
41
     *
42
     * @var null|bool
43
     */
44
    protected $readonly;
45
46
    /**
47
     * ActionsManager constructor.
48
     *
49
     * @param  CrudActions  $service
50
     * @param  Module  $module
51
     */
52
    public function __construct(CrudActions $service, Module $module)
53
    {
54
        $this->service = $service;
55
56
        $this->module = $module;
57
    }
58
59
    /**
60
     * Fetch module's single (per item) actions.
61
     *
62
     * @return Collection
63
     */
64
    public function actions()
65
    {
66
        return $this->scaffoldActions();
67
    }
68
69
    /**
70
     * Fetch module's batch actions.
71
     *
72
     * @return Collection
73
     */
74
    public function batch()
75
    {
76
        return $this->scaffoldBatch();
77
    }
78
79
    /**
80
     * Determine if the user is authorized to make this request.
81
     *
82
     * @param string $ability
83
     * @param $model
84
     *
85
     * @return bool
86
     */
87
    public function authorize($ability, ?Model $model = null)
88
    {
89
        // for most cases it is enough to set
90
        // permissions in Resource object.
91
        if (method_exists($this->module, $abilityMethod = 'can'.Str::title(Str::camel($ability)))) {
92
            $user = auth('admin')->user();
0 ignored issues
show
Bug introduced by
The function auth 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

92
            $user = /** @scrutinizer ignore-call */ auth('admin')->user();
Loading history...
93
            $cacheID = $user->getAuthIdentifier().':'.$ability.($model ? '_'.$model->getKey() : '');
94
95
            return Cache::remember($cacheID, 1, function () use ($user, $ability, $model) {
96
                $abilityMethod = 'can'.Str::title(Str::camel($ability));
97
98
                return $this->module->$abilityMethod($user, $ability, $model);
99
            });
100
        }
101
102
        // Ask Actions Service for action permissions.
103
        return $this->service->authorize($ability, $model, $this->module);
0 ignored issues
show
Unused Code introduced by
The call to Terranet\Administrator\C...rudActions::authorize() has too many arguments starting with $this->module. ( Ignorable by Annotation )

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

103
        return $this->service->/** @scrutinizer ignore-call */ authorize($ability, $model, $this->module);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
104
    }
105
106
    /**
107
     * Checks if resource has no Actions at all / Readonly mode.
108
     */
109
    public function readonly()
110
    {
111
        if (null === $this->readonly) {
112
            $this->readonly = false;
113
114
            // check for <Module>::readonly() method.
115
            if (method_exists($this->module, 'readonly')) {
116
                $this->readonly = $this->module->readonly();
117
            }
118
        }
119
120
        return $this->readonly;
121
    }
122
123
    /**
124
     * Call handler method.
125
     *
126
     * @param string $method
127
     * @param array $arguments
128
     *
129
     * @return mixed
130
     */
131
    public function exec(string $method, array $arguments = [])
132
    {
133
        // execute custom action
134
        if (Str::startsWith($method, 'action::')) {
135
            $handler = $this->scaffoldActions()->find(
136
                str_replace('action::', '', $method)
137
            );
138
139
            return \call_user_func_array([$handler, 'handle'], $arguments);
140
        }
141
142
        // Execute batch action
143
        if (Str::startsWith($method, 'batch::')) {
144
            $handler = $this->scaffoldBatch()->find(
145
                str_replace('batch::', '', $method)
146
            );
147
148
            return \call_user_func_array([$handler, 'handle'], $arguments);
149
        }
150
151
        // Execute CRUD action
152
        return \call_user_func_array([$this->service, $method], (array) $arguments);
153
    }
154
155
    /**
156
     * Parse handler class for per-item and global actions.
157
     *
158
     * @return Collection
159
     */
160
    protected function scaffoldActions()
161
    {
162
        return new Collection($this->service->actions());
163
    }
164
165
    /**
166
     * Parse handler class for per-item and global actions.
167
     *
168
     * @return Collection
169
     */
170
    protected function scaffoldBatch()
171
    {
172
        return new Collection($this->service->batchActions());
173
    }
174
}
175