Completed
Push — master ( 93e691...5acfa0 )
by wen
12:06
created

HasAccess::can()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace Sco\Admin\Component\Concerns;
4
5
trait HasAccess
6
{
7
    /**
8
     * @var \Illuminate\Support\Collection
9
     */
10
    private static $abilities;
11
12
    protected static $observer = \Sco\Admin\Component\Observer::class;
13
14
    protected $observables = [];
15
16
    public static function bootHasAccess()
17
    {
18
        static::$abilities = new Collection();
0 ignored issues
show
Bug introduced by
Since $abilities is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $abilities to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
19
20
        static::observe(static::$observer);
21
    }
22
23
    public function isView()
24
    {
25
        return method_exists($this, 'callView') && $this->can('view');
26
    }
27
28
    public function isCreate()
29
    {
30
        return method_exists($this, 'callCreate') && $this->can('create');
31
    }
32
33
    public function isEdit()
34
    {
35
        return method_exists($this, 'callEdit') && $this->can('edit');
36
    }
37
38
    public function isDelete()
39
    {
40
        return $this->can('delete');
41
    }
42
43
    public function isDestroy()
44
    {
45
        return $this->isRestorableModel() && $this->can('destroy');
46
    }
47
48
    public function isRestore()
49
    {
50
        return $this->isRestorableModel() && $this->can('restore');
51
    }
52
53
    protected function isRestorableModel()
54
    {
55
        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...
56
    }
57
58
    public static function observe($class)
59
    {
60
        $instance = new static;
61
62
        $className = is_string($class) ? $class : get_class($class);
63
64
        foreach ($instance->getObservableAbilities() as $ability) {
65
            if (method_exists($class, $ability)) {
66
                $instance->registerAccess(
67
                    $ability,
68
                    [$instance->app->make($className), $ability]
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...
69
                );
70
            }
71
        }
72
    }
73
74
    /**
75
     * Get the observable event names.
76
     *
77
     * @return array
78
     */
79
    public function getObservableAbilities()
80
    {
81
        return array_merge(
82
            [
83
                'view', 'create', 'edit', 'delete',
84
                'destroy', 'restore',
85
            ],
86
            $this->observables
87
        );
88
    }
89
90
    public function registerAccess($ability, $callback)
91
    {
92
        static::$abilities->put($ability, $callback);
0 ignored issues
show
Bug introduced by
Since $abilities is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $abilities to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
93
    }
94
95
    /**
96
     * @param string $ability
97
     *
98
     * @return mixed
99
     */
100
    final public function can($ability)
101
    {
102
        if (!static::$abilities->has($ability)) {
0 ignored issues
show
Bug introduced by
Since $abilities is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $abilities to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
103
            return false;
104
        }
105
        $value = static::$abilities->get($ability);
0 ignored issues
show
Bug introduced by
Since $abilities is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $abilities to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
106
        if (is_callable($value)) {
107
            return call_user_func_array(
108
                $value,
109
                [$this]
110
            );
111
        }
112
        return $value ? true : false;
113
    }
114
}
115