Completed
Push — master ( 20f1f2...9061ff )
by wen
13:56
created

HasAccess::makeAbilityCallback()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 1
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
     * @var \Illuminate\Support\Collection
12
     */
13
    protected $abilities;
14
15
    /**
16
     * Access observer class
17
     *
18
     * @var string
19
     */
20
    protected $observer = \Sco\Admin\Component\Observer::class;
21
22
    /**
23
     * User exposed observable abilities.
24
     *
25
     * @var array
26
     */
27
    protected $observables = [];
28
29
    public function bootHasAccess()
30
    {
31
        $this->abilities = new Collection();
32
33
        $this->observe($this->observer);
34
    }
35
36
    public function isView()
37
    {
38
        return method_exists($this, 'callView') && $this->can('view');
39
    }
40
41
    public function isCreate()
42
    {
43
        return method_exists($this, 'callCreate') && $this->can('create');
44
    }
45
46
    public function isEdit()
47
    {
48
        return method_exists($this, 'callEdit') && $this->can('edit');
49
    }
50
51
    public function isDelete()
52
    {
53
        return $this->can('delete');
54
    }
55
56
    public function isDestroy()
57
    {
58
        return $this->isRestorableModel() && $this->can('destroy');
59
    }
60
61
    public function isRestore()
62
    {
63
        return $this->isRestorableModel() && $this->can('restore');
64
    }
65
66
    protected function isRestorableModel()
67
    {
68
        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...
69
    }
70
71
    /**
72
     * Register an observer with the Component.
73
     *
74
     * @param $class
75
     */
76
    public function observe($class)
77
    {
78
        $className = is_string($class) ? $class : get_class($class);
79
80
        foreach ($this->getObservableAbilities() as $ability) {
81
            if (method_exists($class, $ability)) {
82
                $this->registerAbility(
83
                    $ability,
84
                    $className . '@' . $ability
85
                );
86
            }
87
        }
88
    }
89
90
    /**
91
     * Get the observable ability names.
92
     *
93
     * @return array
94
     */
95
    public function getObservableAbilities()
96
    {
97
        return array_merge(
98
            [
99
                'view', 'create', 'edit', 'delete',
100
                'destroy', 'restore',
101
            ],
102
            $this->observables
103
        );
104
    }
105
106
    public function registerAbility($ability, $callback)
107
    {
108
        $this->abilities->put($ability, $this->makeAbilityCallback($callback));
109
    }
110
111
    protected function makeAbilityCallback($callback)
112
    {
113
        return function ($component) use ($callback) {
114
            if (is_callable($callback)) {
115
                return $callback($component);
116
            }
117
            if (is_string($callback)) {
118
                list($class, $method) = Str::parseCallback($callback);
119
                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...
120
            }
121
        };
122
    }
123
124
    /**
125
     * @param string $ability
126
     *
127
     * @return mixed
128
     */
129
    final public function can($ability)
130
    {
131
        if (!$this->abilities->has($ability)) {
132
            return false;
133
        }
134
        $value = $this->abilities->get($ability);
135
136
        return $value($this) ? true : false;
137
    }
138
139
    public function getAccesses()
140
    {
141
        return $this->abilities->mapWithKeys(function ($item, $key) {
142
            return [$key => $this->can($key)];
143
        });
144
    }
145
}
146