Completed
Push — master ( 194cbe...2ee496 )
by Yaro
05:39 queued 19s
created

ActionsContainer::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\Actions;
4
5
use Yaro\Jarboe\Table\CRUD;
6
7
class ActionsContainer
8
{
9
    private $actions = [];
10
11 5
    public function find($ident)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
12
    {
13
        /** @var AbstractAction $action */
14 5
        foreach ($this->actions as $action) {
15 5
            if ($action->identifier() == $ident) {
16 5
                return $action;
17
            }
18
        }
19
        return null;
20
    }
21
22 View Code Duplication
    public function getRowActions(): array
23
    {
24
        $actions = [];
25
        /** @var AbstractAction $action */
26
        foreach ($this->actions as $action) {
27
            if ($action->identifier() != 'create') {
28
                $actions[] = $action;
29
            }
30
        }
31
32
        return $actions;
33
    }
34
35 View Code Duplication
    public function remove($ident)
36
    {
37
        $actions = [];
38
        /** @var AbstractAction $action */
39
        foreach ($this->actions as $action) {
40
            if ($action->identifier() != $ident) {
41
                $actions[] = $action;
42
            }
43
        }
44
45
        $this->actions = $actions;
46
    }
47
48 17
    public function add($actions)
49
    {
50 17
        $actions = is_array($actions) ? $actions : [$actions];
51 17
        foreach ($actions as $action) {
52 17
            $this->addAction($action);
53
        }
54 17
    }
55
56 17
    private function addAction(AbstractAction $action)
57
    {
58 17
        $this->actions[] = $action;
59 17
    }
60
61 17
    public function set(array $actions = [])
62
    {
63 17
        $this->actions = [];
64 17
        $this->add($actions);
65 17
    }
66
67 5
    public function isAllowed($ident, $model = null): bool
68
    {
69 5
        $action = $this->find($ident);
70 5
        if (!$action) {
71
            return false;
72
        }
73
74 5
        return $action->isAllowed($model);
75
    }
76
77
    public function shouldRender($ident, $model = null): bool
78
    {
79
        $action = $this->find($ident);
80
        if (!$action) {
81
            return false;
82
        }
83
84
        return $action->shouldRender($model);
85
    }
86
87 View Code Duplication
    public function moveAfter($baseActionIdent, $movableActionIdent)
88
    {
89
        $actions = [];
90
        /** @var AbstractAction $action */
91
        foreach ($this->actions as $action) {
92
            if ($action->identifier() == $movableActionIdent) {
93
                continue;
94
            }
95
96
            $actions[] = $action;
97
            if ($action->identifier() == $baseActionIdent) {
98
                $actions[] = $this->find($movableActionIdent);
99
            }
100
        }
101
102
        $this->actions = $actions;
103
    }
104
105 View Code Duplication
    public function moveBefore($baseActionIdent, $movableActionIdent)
106
    {
107
        $actions = [];
108
        /** @var AbstractAction $action */
109
        foreach ($this->actions as $action) {
110
            if ($action->identifier() == $movableActionIdent) {
111
                continue;
112
            }
113
114
            if ($action->identifier() == $baseActionIdent) {
115
                $actions[] = $this->find($movableActionIdent);
116
            }
117
            $actions[] = $action;
118
        }
119
120
        $this->actions = $actions;
121
    }
122
123 13
    public function setCrud(CRUD $crud)
124
    {
125
        /** @var AbstractAction $action */
126 13
        foreach ($this->actions as $action) {
127 13
            $action->setCrud($crud);
128
        }
129 13
    }
130
}
131