AbstractAction::renderCheck()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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