Completed
Push — master ( 17739d...6c5d1a )
by Yaro
07:08
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 27
    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 27
        foreach ($this->actions as $action) {
15 27
            if ($action->identifier() == $ident) {
16 27
                return $action;
17
            }
18
        }
19 10
        return null;
20
    }
21
22 2
    public function getRowActions(): array
23
    {
24 2
        $actions = [];
25
        /** @var AbstractAction $action */
26 2
        foreach ($this->actions as $action) {
27 2
            if ($action->identifier() != 'create') {
28 2
                $actions[] = $action;
29
            }
30
        }
31
32 2
        return $actions;
33
    }
34
35 1
    public function remove($ident)
36
    {
37 1
        $actions = [];
38
        /** @var AbstractAction $action */
39 1
        foreach ($this->actions as $action) {
40 1
            if ($action->identifier() != $ident) {
41 1
                $actions[] = $action;
42
            }
43
        }
44
45 1
        $this->actions = $actions;
46 1
    }
47
48 54
    public function add($actions)
49
    {
50 54
        $actions = is_array($actions) ? $actions : [$actions];
51 54
        foreach ($actions as $action) {
52 54
            $this->addAction($action);
53
        }
54 54
    }
55
56 54
    private function addAction(AbstractAction $action)
57
    {
58 54
        $this->actions[] = $action;
59 54
    }
60
61 54
    public function set(array $actions = [])
62
    {
63 54
        $this->actions = [];
64 54
        $this->add($actions);
65 54
    }
66
67 16
    public function isAllowed($ident, $model = null): bool
68
    {
69 16
        $action = $this->find($ident);
70 16
        if (!$action) {
71 1
            return false;
72
        }
73
74 16
        return $action->isAllowed($model);
75
    }
76
77 1
    public function shouldRender($ident, $model = null): bool
78
    {
79 1
        $action = $this->find($ident);
80 1
        if (!$action) {
81 1
            return false;
82
        }
83
84 1
        return $action->shouldRender($model);
85
    }
86
87 4 View Code Duplication
    public function moveAfter($baseActionIdent, $movableActionIdent)
88
    {
89 4
        if (!$this->find($baseActionIdent) || !$this->find($movableActionIdent)) {
90 3
            throw new \RuntimeException(
91 3
                sprintf('No action defined for identifier [%s] or [%s]', $baseActionIdent, $movableActionIdent)
92
            );
93
        }
94
95 1
        $actions = [];
96
        /** @var AbstractAction $action */
97 1
        foreach ($this->actions as $action) {
98 1
            if ($action->identifier() == $movableActionIdent) {
99 1
                continue;
100
            }
101
102 1
            $actions[] = $action;
103 1
            if ($action->identifier() == $baseActionIdent) {
104 1
                $actions[] = $this->find($movableActionIdent);
105
            }
106
        }
107
108 1
        $this->actions = $actions;
109 1
    }
110
111 4 View Code Duplication
    public function moveBefore($baseActionIdent, $movableActionIdent)
112
    {
113 4
        if (!$this->find($baseActionIdent) || !$this->find($movableActionIdent)) {
114 3
            throw new \RuntimeException(
115 3
                sprintf('No action defined for identifier [%s] or [%s]', $baseActionIdent, $movableActionIdent)
116
            );
117
        }
118
119 1
        $actions = [];
120
        /** @var AbstractAction $action */
121 1
        foreach ($this->actions as $action) {
122 1
            if ($action->identifier() == $movableActionIdent) {
123 1
                continue;
124
            }
125
126 1
            if ($action->identifier() == $baseActionIdent) {
127 1
                $actions[] = $this->find($movableActionIdent);
128
            }
129 1
            $actions[] = $action;
130
        }
131
132 1
        $this->actions = $actions;
133 1
    }
134
135 34
    public function setCrud(CRUD $crud)
136
    {
137
        /** @var AbstractAction $action */
138 34
        foreach ($this->actions as $action) {
139 34
            $action->setCrud($crud);
140
        }
141 34
    }
142
}
143