Test Failed
Push — master ( 794710...4acb1e )
by Georgi
03:38
created

ActionButton   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addIcon() 0 9 2
A addLabel() 0 5 2
A getIcon() 0 3 2
A callback() 0 5 1
A addCallback() 0 13 4
A renderView() 0 9 1
1
<?php
2
3
namespace Epesi\Core\System\View;
4
5
use atk4\ui\View;
6
use atk4\ui\Icon;
7
use atk4\ui\jsCallback;
8
9
class ActionButton extends View
10
{
11
    public $defaultTemplate = 'action_button.html';
12
13
    public $ui = 'basic inverted action button';
14
15
    /**
16
     * Icon that will appear on the button (top).
17
     *
18
     * @var string|array|Icon
19
     */
20
    public $icon;
21
    
22
    public $label;
23
24
    public $element = 'a';
25
    
26
    public $weight = 10;
27
    
28
    public $callback;
29
30
    public function renderView()
31
    {
32
        $this->addIcon();
33
        
34
        $this->addLabel();
35
        
36
        $this->addCallback();
37
38
        parent::renderView();
39
    }
40
    
41
    protected function addIcon()
42
    {
43
    	if (! $icon = $this->getIcon()) return;
44
    	
45
    	$this->add($icon, 'Icon')->id = null;
46
    	
47
    	$this->addClass('icon');
48
    	
49
    	return $this;
50
    }
51
    
52
    protected function addLabel()
53
    {
54
    	$this->content = $this->label?: $this->content;
55
    	
56
    	return $this;
57
    }
58
59
    public function getIcon()
60
    {
61
    	return is_object($this->icon)? $this->icon: new Icon($this->icon);
62
    }
63
    
64
    public function callback($callable)
65
    {
66
    	$this->callback = $callable;
67
    	
68
    	return $this;
69
    }
70
    
71
    public function addCallback()
72
    {
73
    	if (!$callable = $this->callback) return;
74
    	
75
    	if (is_callable($callable)) {
76
    		$callable = $this->add('jsCallback')->set($callable);
77
    	}
78
    	
79
    	if ($callable instanceof jsCallback) {
80
    		$this->on('click', $callable);
0 ignored issues
show
Bug introduced by
$callable of type atk4\ui\jsCallback is incompatible with the type string expected by parameter $selector of atk4\ui\View::on(). ( Ignorable by Annotation )

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

80
    		$this->on('click', /** @scrutinizer ignore-type */ $callable);
Loading history...
81
    	}
82
    	
83
    	return $this;
84
    }
85
86
}
87