Completed
Push — master ( b43387...8f2a89 )
by Yaro
08:18
created

AbstractAction::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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 105
    public function __construct()
17
    {
18
        $this->checkClosure = function () {
19 33
            return true;
20
        };
21 105
    }
22
23 105
    public static function make()
24
    {
25 105
        return new static();
26
    }
27
28 73
    public function setCrud(CRUD $crud)
29
    {
30 73
        $this->crud = $crud;
31 73
    }
32
33 24
    public function crud(): CRUD
34
    {
35 24
        return $this->crud;
36
    }
37
38 44
    public function identifier()
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...
39
    {
40 44
        return $this->ident ?: static::class;
41
    }
42
43 13
    public function check(\Closure $closure = null)
44
    {
45 13
        $this->checkClosure = $closure;
46
47 13
        return $this;
48
    }
49
50 40
    public function isAllowed($model = null)
51
    {
52 40
        $closure = $this->checkClosure;
53
54 40
        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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $closure can also be of type object<Closure>. However, the property $renderClosure is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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;
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