Completed
Push — master ( b5c386...93e691 )
by wen
12:51
created

Access::isRestorableModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sco\Admin\Component;
4
5
use Sco\Admin\Contracts\AccessInterface;
6
use Sco\Admin\Contracts\ComponentInterface;
7
8
abstract class Access implements AccessInterface
9
{
10
    /**
11
     * @var \Sco\Admin\Contracts\ComponentInterface
12
     */
13
    protected $component;
14
15
    private $abilities = [
16
        'view'    => true,
17
        'create'  => true,
18
        'edit'    => true,
19
        'delete'  => true,
20
        'destroy' => true,
21
        'restore' => true,
22
    ];
23
24
    public function __construct(ComponentInterface $component)
25
    {
26
        $this->component = $component;
27
    }
28
29
    public function isView()
30
    {
31
        return method_exists($this->getComponent(), 'callView')
32
            && $this->can('view');
33
    }
34
35
    public function isCreate()
36
    {
37
        return method_exists($this->getComponent(), 'callCreate')
38
            && $this->can('create');
39
    }
40
41
    public function isEdit()
42
    {
43
        return method_exists($this->getComponent(), 'callEdit')
44
            && $this->can('edit');
45
    }
46
47
    public function isDelete()
48
    {
49
        return $this->can('delete');
50
    }
51
52
    public function isDestroy()
53
    {
54
        return $this->isRestorableModel() && $this->can('destroy');
55
    }
56
57
    public function isRestore()
58
    {
59
        return $this->isRestorableModel() && $this->can('restore');
60
    }
61
62
    protected function isRestorableModel()
63
    {
64
        return $this->getComponent()->getRepository()->isRestorable();
65
    }
66
67
    /**
68
     * @param string $ability
69
     *
70
     * @return mixed
71
     */
72
    final public function can($ability)
73
    {
74
        if (!isset($this->abilities[$ability])) {
75
            return false;
76
        }
77
78
        if (is_callable($this->abilities[$ability])) {
79
            return call_user_func_array(
80
                $this->abilities[$ability],
81
                [$this->getComponent()]
82
            );
83
        }
84
        return $this->abilities[$ability] ? true : false;
85
    }
86
87
    protected function getComponent()
88
    {
89
        return $this->component;
90
    }
91
}
92