GenericBulkAction::getI18nLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LeKoala\Tabulator\BulkActions;
4
5
use SilverStripe\Control\HTTPRequest;
6
use LeKoala\Tabulator\AbstractBulkAction;
7
8
/**
9
 *  Generic bulk action that can run a callable
10
 */
11
class GenericBulkAction extends AbstractBulkAction
12
{
13
    protected string $name = '';
14
    protected string $label = '';
15
    protected $callable = null;
16
    protected bool $xhr = true;
17
18
    public function __construct($name, $label, $callable = null)
19
    {
20
        parent::__construct();
21
22
        $this->name = $name;
23
        $this->label = $label;
24
        $this->callable = $callable;
25
    }
26
27
    public function getI18nLabel(): string
28
    {
29
        return $this->getLabel();
30
    }
31
32
    public function process(HTTPRequest $request): string
33
    {
34
        $records = $this->getRecords() ?? [];
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getRecords() targeting LeKoala\Tabulator\AbstractBulkAction::getRecords() 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...
35
        $i = 0;
36
        $success = 0;
37
        $errors = 0;
38
        if (!$this->callable) {
39
            return "Callbable function not defined";
40
        }
41
42
        foreach ($records as $record) {
43
            $callable = $this->callable;
44
            $result = $callable($record, $this->tabulatorGrid);
45
            if ($result) {
46
                $success++;
47
            } elseif ($result === false) {
48
                $errors++;
49
            }
50
            $i++;
51
        }
52
53
        if ($errors > 0) {
54
            $result = _t(__CLASS__ . ".RECORDSPROCESSEDWITHERR", "{err} errors and {count} records processed", ["err" => $errors, "count" => $i]);
55
        } else {
56
            $result = _t(__CLASS__ . ".RECORDSPROCESSED", "{count} records processed", ["count" => $i]);
57
        }
58
59
        return $result;
60
    }
61
62
    /**
63
     * Get the value of callable
64
     */
65
    public function getCallable()
66
    {
67
        return $this->callable;
68
    }
69
70
    /**
71
     * Set the value of callable
72
     *
73
     * @param callable $callable
74
     */
75
    public function setCallable($callable): self
76
    {
77
        $this->callable = $callable;
78
        return $this;
79
    }
80
}
81