Test Failed
Push — master ( 470d4b...33c92c )
by Terzi
04:31
created

src/ActionsManager.php (5 issues)

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

90
        if (method_exists($this->module, $abilityMethod = 'can'.title_case(/** @scrutinizer ignore-call */ camel_case($ability)))) {
Loading history...
The function title_case 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

90
        if (method_exists($this->module, $abilityMethod = 'can'./** @scrutinizer ignore-call */ title_case(camel_case($ability)))) {
Loading history...
91
            $user = auth('admin')->user();
92
            $cacheID = $user->getAuthIdentifier().':'.$ability.($model ? '_'.$model->getKey() : '');
93
94
            return Cache::remember($cacheID, 1, function () use ($user, $ability, $model) {
95
                $abilityMethod = 'can'.title_case(camel_case($ability));
0 ignored issues
show
The function camel_case 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

95
                $abilityMethod = 'can'.title_case(/** @scrutinizer ignore-call */ camel_case($ability));
Loading history...
The function title_case 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

95
                $abilityMethod = 'can'./** @scrutinizer ignore-call */ title_case(camel_case($ability));
Loading history...
96
97
                return $this->module->$abilityMethod($user, $ability, $model);
98
            });
99
        }
100
101
        // Ask Actions Service for action permissions.
102
        return $this->service->authorize($ability, $model, $this->module);
103
    }
104
105
    /**
106
     * Checks if resource has no Actions at all / Readonly mode.
107
     */
108
    public function readonly()
109
    {
110
        if (null === $this->readonly) {
111
            $this->readonly = false;
112
113
            // check for <Module>::readonly() method.
114
            if (method_exists($this->module, 'readonly')) {
115
                $this->readonly = $this->module->readonly();
116
            }
117
        }
118
119
        return $this->readonly;
120
    }
121
122
    /**
123
     * Call handler method.
124
     *
125
     * @param string $method
126
     * @param array $arguments
127
     *
128
     * @return mixed
129
     */
130
    public function exec(string $method, array $arguments = [])
131
    {
132
        // execute custom action
133
        if (starts_with($method, 'action::')) {
0 ignored issues
show
The function starts_with 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

133
        if (/** @scrutinizer ignore-call */ starts_with($method, 'action::')) {
Loading history...
134
            $handler = $this->scaffoldActions()->find(
135
                str_replace('action::', '', $method)
136
            );
137
138
            return \call_user_func_array([$handler, 'handle'], $arguments);
139
        }
140
141
        // Execute batch action
142
        if (starts_with($method, 'batch::')) {
143
            $handler = $this->scaffoldBatch()->find(
144
                str_replace('batch::', '', $method)
145
            );
146
147
            return \call_user_func_array([$handler, 'handle'], $arguments);
148
        }
149
150
        // Execute CRUD action
151
        return \call_user_func_array([$this->service, $method], (array) $arguments);
152
    }
153
154
    /**
155
     * Parse handler class for per-item and global actions.
156
     *
157
     * @return Collection
158
     */
159
    protected function scaffoldActions()
160
    {
161
        return new Collection($this->service->actions());
162
    }
163
164
    /**
165
     * Parse handler class for per-item and global actions.
166
     *
167
     * @return Collection
168
     */
169
    protected function scaffoldBatch()
170
    {
171
        return new Collection($this->service->batchActions());
172
    }
173
}
174