Button   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 1
dl 0
loc 163
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setIcon() 0 5 1
A setOption() 0 11 2
A setElementPrototype() 0 5 1
A getElement() 0 9 1
A getElementPrototype() 0 14 3
A getOption() 0 6 2
A getOptions() 0 4 1
A getDestination() 0 8 2
A getArguments() 0 4 1
A render() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the Grido (https://github.com/o5/grido)
5
 *
6
 * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
7
 *
8
 * For the full copyright and license information, please view
9
 * the file LICENSE.md that was distributed with this source code.
10
 */
11
12
namespace Grido\Components;
13
14
use Grido\Grid;
15
use Nette\Utils\Html;
16
17
/**
18
 * Toolbar button.
19
 *
20
 * @package     Grido
21
 * @subpackage  Components
22
 * @author      Petr Bugyík
23
 *
24
 * @property-read Html $element
25
 * @property-write Html $elementPrototype
26
 * @property string $options
27
 * @property-read string $destination
28
 * @property-read array $arguments
29
 */
30
class Button extends Component
31 1
{
32
    const ID = 'buttons';
33
34
    /** @var string first param for method $presenter->link() */
35
    protected $destination;
36
37
    /** @var array second param for method $presenter->link() */
38
    protected $arguments = [];
39
40
    /** @var Html <a> html tag */
41
    protected $elementPrototype;
42
43
    /** @var array */
44
    protected $options = [];
45
46
    /**
47
     * @param Grid $grid
48
     * @param string $name
49
     * @param string $label
50
     * @param string $destination - first param for method $presenter->link()
51
     * @param array $arguments - second param for method $presenter->link()
52
     */
53
    public function __construct(Grid $grid, $name, $label = NULL, $destination = NULL, array $arguments = [])
54
    {
55 1
        $this->label = $label;
56 1
        $this->destination = $destination;
57 1
        $this->arguments = $arguments;
58
59 1
        $this->addComponentToGrid($grid, $name);
60 1
    }
61
62
    /**
63
     * Sets name of icon.
64
     * @param string $name
65
     * @return Action
66
     */
67
    public function setIcon($name)
68
    {
69 1
        $this->setOption('icon', $name);
70 1
        return $this;
71
    }
72
73
    /**
74
     * Sets user-specific option.
75
     * @param string $key
76
     * @param mixed $value
77
     * @return Action
78
     */
79
    public function setOption($key, $value)
80
    {
81 1
        if ($value === NULL) {
82 1
            unset($this->options[$key]);
83
84 1
        } else {
85 1
            $this->options[$key] = $value;
86
        }
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * Sets html element.
93
     * @param Html $elementPrototype
94
     * @return Button
95
     */
96
    public function setElementPrototype(Html $elementPrototype)
97
    {
98 1
        $this->elementPrototype = $elementPrototype;
99 1
        return $this;
100
    }
101
102
    /**********************************************************************************************/
103
104
    /**
105
     * @return Html
106
     * @internal
107
     */
108
    public function getElement()
109
    {
110 1
        $element = clone $this->getElementPrototype();
111
112 1
        $href = $this->presenter->link($this->getDestination(), $this->getArguments());
113 1
        $element->href($href);
114
115 1
        return $element;
116
    }
117
118
    /**
119
     * Returns element prototype (<a> html tag).
120
     * @return Html
121
     * @throws Exception
122
     */
123
    public function getElementPrototype()
124
    {
125 1
        if ($this->elementPrototype === NULL) {
126 1
            $this->elementPrototype = Html::el('a')
127 1
                ->setClass(['grid-button-' . $this->getName()])
128 1
                ->setText($this->label);
129 1
        }
130
131 1
        if (isset($this->elementPrototype->class)) {
132 1
            $this->elementPrototype->class = (array) $this->elementPrototype->class;
133 1
        }
134
135 1
        return $this->elementPrototype;
136
    }
137
138
    /**
139
     * Returns user-specific option.
140
     * @param string $key
141
     * @param mixed $default
142
     * @return mixed
143
     */
144
    public function getOption($key, $default = NULL)
145
    {
146 1
        return isset($this->options[$key])
147 1
            ? $this->options[$key]
148 1
            : $default;
149
    }
150
151
    /**
152
     * Returns user-specific options.
153
     * @return array
154
     */
155
    public function getOptions()
156
    {
157 1
        return $this->options;
158
    }
159
160
    /**
161
     * @return string
162
     * @internal
163
     */
164
    public function getDestination()
165
    {
166 1
        if ($this->destination === NULL) {
167 1
            $this->destination = $this->getName();
168 1
        }
169
170 1
        return $this->destination;
171
    }
172
173
    /**
174
     * @return array
175
     * @internal
176
     */
177
    public function getArguments()
178
    {
179 1
        return $this->arguments;
180
    }
181
182
    /**********************************************************************************************/
183
184
    /**
185
     * @throws Exception
186
     * @return void
187
     */
188
    public function render()
189
    {
190 1
        echo $this->getElement();
191 1
    }
192
}
193