ActionColumn::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace ChurakovMike\EasyGrid\Columns;
4
5
use ChurakovMike\EasyGrid\Columns\Actions\BaseButton;
6
use ChurakovMike\EasyGrid\Columns\Actions\Destroy;
7
use ChurakovMike\EasyGrid\Columns\Actions\Show;
8
use ChurakovMike\EasyGrid\Columns\Actions\Update;
9
use ChurakovMike\EasyGrid\Filters\StubFilter;
10
use ChurakovMike\EasyGrid\Traits\Configurable;
11
use Closure;
12
13
/**
14
 * Class ActionColumn.
15
 * @package ChurakovMike\EasyGrid\Columns
16
 *
17
 * @property StubFilter $filter
18
 * @property BaseButton[] $buttons
19
 * @property string $value
20
 */
21
class ActionColumn extends BaseColumn
22
{
23
    use Configurable;
24
25
    const
26
        ACTION_SHOW = 'show',
27
        ACTION_UPDATE = 'update',
28
        ACTION_DESTROY = 'destroy',
29
        ACTIONS = [
30
            self::ACTION_SHOW,
31
            self::ACTION_UPDATE,
32
            self::ACTION_DESTROY,
33
        ];
34
35
    const
36
        BASE_ACTIONS = [
37
            self::ACTION_SHOW => Show::class,
38
            self::ACTION_UPDATE => Update::class,
39
            self::ACTION_DESTROY => Destroy::class,
40
        ];
41
42
    /**
43
     * @var StubFilter $filter
44
     */
45
    public $filter;
46
47
    /**
48
     * @var array $buttons
49
     */
50
    public $buttons;
51
52
    /**
53
     * @var string $value
54
     */
55
    public $value;
56
57
    /**
58
     * ActionColumn constructor.
59
     * @param array $config
60
     */
61
    public function __construct(array $config)
62
    {
63
        $this->loadConfig($config);
64
        $this->buildButtons($config);
65
        if (is_null($this->width)) {
0 ignored issues
show
introduced by
The condition is_null($this->width) is always false.
Loading history...
66
            $this->width = '10%';
67
        }
68
69
        $this->filter = new StubFilter(['name' => '']);
70
    }
71
72
    /**
73
     * Build action column buttons.
74
     *
75
     * @param $config
76
     */
77
    public function buildButtons($config)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

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

77
    public function buildButtons(/** @scrutinizer ignore-unused */ $config)

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

Loading history...
78
    {
79
        foreach ($this->buttons as $key => &$button) {
80
            if ($button instanceof Closure) {
81
                $class = self::BASE_ACTIONS[$key];
82
                $button = new $class([
83
                    'url' => $button,
84
                ]);
85
                continue;
86
            }
87
88
            if (is_string($button)) {
89
                if (in_array($button, self::ACTIONS)) {
90
                    $class = self::BASE_ACTIONS[$button];
91
                    $button = new $class;
92
                    continue;
93
                }
94
95
                if (in_array($key, self::ACTIONS)) {
96
                    $class = self::BASE_ACTIONS[$key];
97
                    $button = new $class([
98
                        'url' => $button,
99
                    ]);
100
                    continue;
101
                }
102
            }
103
        }
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getLabel(): string
110
    {
111
        return '';
112
    }
113
114
    /**
115
     * @param $row
116
     * @return string
117
     */
118
    public function render($row)
119
    {
120
        $value = '';
121
        foreach ($this->buttons as $button) {
122
            $value .= $button->render($row);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $button->render($row) targeting ChurakovMike\EasyGrid\Co...ns\BaseButton::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
123
        }
124
125
        return '<div class="row">' . $value . '</div>';
126
    }
127
}
128