Event::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 4
crap 3.0123
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\Actions;
13
14
use Grido\Grid;
15
use Grido\Exception;
16
17
/**
18
 * Event action.
19
 *
20
 * @package     Grido
21
 * @subpackage  Components\Actions
22
 * @author      Josef Kříž <[email protected]>
23
 * @author      Petr Bugyík
24
 *
25
 * @property callable $onClick function($id, Grido\Components\Actions\Event $event)
26
 */
27
class Event extends Action
28 1
{
29
    /** @var callable function($id, Grido\Components\Actions\Event $event) */
30
    private $onClick;
31
32
    /**
33
     * @param \Grido\Grid $grid
34
     * @param string $name
35
     * @param string $label
36
     * @param callable $onClick
37
     * @throws Exception
38
     */
39
    public function __construct($grid, $name, $label, $onClick = NULL)
40
    {
41 1
        parent::__construct($grid, $name, $label);
42
43 1
        if ($onClick === NULL) {
44 1
            $grid->onRender[] = function(Grid $grid) {
0 ignored issues
show
Unused Code introduced by
The parameter $grid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45 1
                if ($this->onClick === NULL) {
46 1
                    throw new Exception("Callback onClick in action '{$this->name}' must be set.");
47
                }
48
            };
49 1
        } else {
50 1
            $this->setOnClick($onClick);
51
        }
52 1
    }
53
54
    /**
55
     * Sets on-click handler.
56
     * @param callable $onClick function($id, Grido\Components\Actions\Event $event)
57
     * @return \Grido\Components\Actions\Event
58
     */
59
    public function setOnClick(callable $onClick)
60
    {
61 1
        $this->onClick = $onClick;
62 1
        return $this;
63
    }
64
65
    /**
66
     * Returns on-click handler.
67
     * @return callable
68
     */
69
    public function getOnClick()
70
    {
71 1
        return $this->onClick;
72
    }
73
74
    /**********************************************************************************************/
75
76
    /**
77
     * @param mixed $row
78
     * @return \Nette\Utils\Html
79
     * @internal
80
     */
81
    public function getElement($row)
82
    {
83 1
        $element = parent::getElement($row);
84
85 1
        $primaryValue = $this->grid->getProperty($row, $this->getPrimaryKey());
86 1
        $element->href($this->link('click!', $primaryValue));
87
88 1
        return $element;
89
    }
90
91
    /**********************************************************************************************/
92
93
    /**
94
     * @param int $id
95
     * @internal
96
     */
97
    public function handleClick($id)
98
    {
99 1
        call_user_func_array($this->onClick, [$id, $this]);
100 1
    }
101
}
102