Completed
Push — master ( 0d8635...4c7cc5 )
by wen
13:50
created

HasAccess::observe()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 8
nop 1
1
<?php
2
3
namespace Sco\Admin\Component\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
8
trait HasAccess
9
{
10
    /**
11
     * The abilities of access.
12
     *
13
     * @var \Illuminate\Support\Collection
14
     */
15
    protected $abilities;
16
17
    /**
18
     * Access observer class.
19
     *
20
     * @var string
21
     */
22
    protected $observer;
23
24
    /**
25
     * User exposed observable abilities.
26
     *
27
     * @var array
28
     */
29
    protected $observables = [];
30
31
    /**
32
     * Initialize access.
33
     */
34
    public function bootHasAccess()
35
    {
36
        $this->abilities = new Collection();
37
38
        $this->observe($this->observer);
39
    }
40
41
    /**
42
     * Determine if the entity have access to display.
43
     *
44
     * @return bool
45
     */
46
    public function isDisplay()
47
    {
48
        return method_exists($this, 'callDisplay') && $this->can('display');
49
    }
50
51
    /**
52
     * Check if the entity have access to create.
53
     *
54
     * @return bool
55
     */
56
    public function isCreate()
57
    {
58
        return method_exists($this, 'callCreate') && $this->can('create');
59
    }
60
61
    /**
62
     * Check if the entity have access to edit.
63
     *
64
     * @return bool
65
     */
66
    public function isEdit()
67
    {
68
        return method_exists($this, 'callEdit') && $this->can('edit');
69
    }
70
71
    /**
72
     * Check if the entity have access to delete.
73
     *
74
     * @return mixed
75
     */
76
    public function isDelete()
77
    {
78
        return $this->can('delete');
79
    }
80
81
    /**
82
     * Check if the entity have access to destroy.
83
     *
84
     * @return bool
85
     */
86
    public function isDestroy()
87
    {
88
        return $this->isRestorableModel() && $this->can('destroy');
89
    }
90
91
    /**
92
     * Check if the entity have access to restore.
93
     *
94
     * @return bool
95
     */
96
    public function isRestore()
97
    {
98
        return $this->isRestorableModel() && $this->can('restore');
99
    }
100
101
    /**
102
     * Whether the model can be restored
103
     *
104
     * @return mixed
105
     */
106
    protected function isRestorableModel()
107
    {
108
        return $this->getRepository()->isRestorable();
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
109
    }
110
111
    /**
112
     * Register an observer with the Component.
113
     *
114
     * @param $class
115
     */
116
    public function observe($class)
117
    {
118
        $className = is_string($class) ? $class : get_class($class);
119
120
        if (!class_exists($className)) {
121
            return;
122
        }
123
124
        foreach ($this->getObservableAbilities() as $ability) {
125
            if (method_exists($class, $ability)) {
126
                $this->registerAbility($ability, $className . '@' . $ability);
127
            }
128
        }
129
    }
130
131
    /**
132
     * Get the observable ability names.
133
     *
134
     * @return array
135
     */
136
    public function getObservableAbilities()
137
    {
138
        return array_merge([
139
            'display',
140
            'create',
141
            'edit',
142
            'delete',
143
            'destroy',
144
            'restore',
145
        ], $this->observables);
146
    }
147
148
    /**
149
     * register ability to access.
150
     *
151
     * @param string $ability
152
     * @param string|\Closure $callback
153
     */
154
    public function registerAbility($ability, $callback)
155
    {
156
        $this->abilities->put($ability, $this->makeAbilityCallback($callback));
157
    }
158
159
    /**
160
     * @param string|\Closure $callback
161
     *
162
     * @return \Closure
163
     */
164
    protected function makeAbilityCallback($callback)
165
    {
166
        return function ($component) use ($callback) {
167
            if (is_callable($callback)) {
168
                return $callback($component);
169
            }
170
            if (is_string($callback)) {
171
                list($class, $method) = Str::parseCallback($callback);
172
173
                return call_user_func([$this->app->make($class), $method], $component);
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
174
            }
175
        };
176
    }
177
178
    /**
179
     * Determine if the entity has a given ability.
180
     *
181
     * @param string $ability
182
     *
183
     * @return bool
184
     */
185
    final public function can($ability)
186
    {
187
        if (! $this->abilities->has($ability)) {
188
            return false;
189
        }
190
        $value = $this->abilities->get($ability);
191
192
        return $value($this) ? true : false;
193
    }
194
195
    /**
196
     * Get all ability.
197
     *
198
     * @return Collection
199
     */
200
    public function getAccesses()
201
    {
202
        return $this->abilities->mapWithKeys(function ($item, $key) {
203
            return [$key => $this->can($key)];
204
        });
205
    }
206
}
207