ToolbarTrait   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 30.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 63
ccs 8
cts 26
cp 0.3076
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getActiveBodyToolbarTools() 0 12 5
A getTools() 0 3 1
A getTool() 0 7 2
A getActiveHeaderToolbarTools() 0 11 4
A getActiveBodyToolbarToolsOnTop() 0 3 1
A getActiveBodyToolbarToolsOnBottom() 0 3 1
A addTool() 0 3 1
1
<?php
2
3
namespace Yaro\Jarboe\Table\CrudTraits;
4
5
use Yaro\Jarboe\Table\Toolbar\AbstractTool;
6
use Yaro\Jarboe\Table\Toolbar\Interfaces\ToolInterface;
7
8
trait ToolbarTrait
9
{
10
    private $toolbar = [];
11
12
    /**
13
     * @param $ident
14
     * @throws \RuntimeException
15
     * @return AbstractTool
16
     */
17 2
    public function getTool($ident)
18
    {
19 2
        if (array_key_exists($ident, $this->toolbar)) {
20 2
            return $this->toolbar[$ident];
21
        }
22
23
        throw new \RuntimeException('Not allowed toolbar');
24
    }
25
26 2
    public function addTool(ToolInterface $tool)
27
    {
28 2
        $this->toolbar[$tool->identifier()] = $tool;
29 2
    }
30
31 2
    public function getTools()
32
    {
33 2
        return $this->toolbar;
34
    }
35
36
    public function getActiveHeaderToolbarTools()
37
    {
38
        $list = [];
39
        /** @var ToolInterface $tool */
40
        foreach ($this->toolbar as $tool) {
41
            if ($tool->position() == ToolInterface::POSITION_HEADER && $tool->check()) {
42
                $list[] = $tool;
43
            }
44
        }
45
46
        return $list;
47
    }
48
49
    public function getActiveBodyToolbarToolsOnTop()
50
    {
51
        return $this->getActiveBodyToolbarTools(ToolInterface::POSITION_BODY_TOP);
52
    }
53
54
    public function getActiveBodyToolbarToolsOnBottom()
55
    {
56
        return $this->getActiveBodyToolbarTools(ToolInterface::POSITION_BODY_BOTTOM);
57
    }
58
59
    public function getActiveBodyToolbarTools($position)
60
    {
61
        $list = [];
62
        /** @var ToolInterface $tool */
63
        foreach ($this->toolbar as $tool) {
64
            $isPositioned = $tool->position() == $position || $tool->position() == ToolInterface::POSITION_BODY_BOTH;
65
            if ($isPositioned && $tool->check()) {
66
                $list[] = $tool;
67
            }
68
        }
69
70
        return $list;
71
    }
72
}
73