AbstractAction   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 91
rs 10
ccs 40
cts 40
cp 1
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTooltip() 0 3 1
A crud() 0 3 1
A identifier() 0 3 2
A renderCheck() 0 5 1
A tooltip() 0 6 1
A setCrud() 0 3 1
A shouldRender() 0 8 3
A getTooltipPosition() 0 11 5
A make() 0 3 1
A __construct() 0 4 1
A check() 0 5 1
A isAllowed() 0 5 2
1
<?php
2
3
namespace Yaro\Jarboe\Table\Actions;
4
5
use Yaro\Jarboe\Table\CRUD;
6
7
abstract class AbstractAction implements ActionInterface
8
{
9
    protected $ident;
10
    protected $checkClosure;
11
    protected $renderClosure = false;
12
    private $crud;
13
    private $tooltip = null;
14
    private $tooltipPosition = ActionInterface::TOOLTIP_POSITION_TOP;
15
16 113
    public function __construct()
17
    {
18
        $this->checkClosure = function () {
19 36
            return true;
20
        };
21 113
    }
22
23 113
    public static function make()
24
    {
25 113
        return new static();
26
    }
27
28 81
    public function setCrud(CRUD $crud)
29
    {
30 81
        $this->crud = $crud;
31 81
    }
32
33 24
    public function crud(): CRUD
34
    {
35 24
        return $this->crud;
36
    }
37
38 49
    public function identifier()
39
    {
40 49
        return $this->ident ?: static::class;
41
    }
42
43 14
    public function check(\Closure $closure = null)
44
    {
45 14
        $this->checkClosure = $closure;
46
47 14
        return $this;
48
    }
49
50 44
    public function isAllowed($model = null)
51
    {
52 44
        $closure = $this->checkClosure;
53
54 44
        return is_callable($closure) ? call_user_func_array($closure, [$model]) : false;
55
    }
56
57 6
    public function renderCheck(\Closure $closure = null)
58
    {
59 6
        $this->renderClosure = $closure;
60
61 6
        return $this;
62
    }
63
64 13
    public function shouldRender($model = null)
65
    {
66 13
        $closure = $this->renderClosure;
67 13
        if ($closure === false) {
68 13
            return $this->isAllowed($model);
69
        }
70
71 6
        return is_callable($closure) ? call_user_func_array($closure, [$model]) : false;
0 ignored issues
show
Bug introduced by
$closure of type true is incompatible with the type callable expected by parameter $callback of call_user_func_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        return is_callable($closure) ? call_user_func_array(/** @scrutinizer ignore-type */ $closure, [$model]) : false;
Loading history...
72
    }
73
74 5
    public function tooltip(string $tooltip, string $position = ActionInterface::TOOLTIP_POSITION_TOP): ActionInterface
75
    {
76 5
        $this->tooltip = $tooltip;
77 5
        $this->tooltipPosition = $position;
78
79 5
        return $this;
80
    }
81
82 5
    public function getTooltip()
83
    {
84 5
        return $this->tooltip;
85
    }
86
87 5
    public function getTooltipPosition(): string
88
    {
89 5
        switch ($this->tooltipPosition) {
90 5
            case self::TOOLTIP_POSITION_TOP:
91 5
            case self::TOOLTIP_POSITION_RIGHT:
92 5
            case self::TOOLTIP_POSITION_BOTTOM:
93 5
            case self::TOOLTIP_POSITION_LEFT:
94 5
                return $this->tooltipPosition;
95
96
            default:
97 5
                return self::TOOLTIP_POSITION_TOP;
98
        }
99
    }
100
}
101