Passed
Push — master ( f5069b...2ddc1c )
by Thomas
12:50 queued 09:56
created

GenericBulkAction::process()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 19
c 3
b 1
f 0
dl 0
loc 28
rs 9.0111
cc 6
nc 9
nop 1
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
25
        if ($callable) {
26
            $this->callable = $callable;
27
        }
28
    }
29
30
    public function getI18nLabel(): string
31
    {
32
        return $this->getLabel();
33
    }
34
35
    public function process(HTTPRequest $request): string
36
    {
37
        $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...
38
        $i = 0;
39
        $success = 0;
40
        $errors = 0;
41
        if (!$this->callable) {
42
            return "Callbable function not defined";
43
        }
44
45
        foreach ($records as $record) {
46
            $callable = $this->callable;
47
            $result = $callable($record, $this->tabulatorGrid);
48
            if ($result) {
49
                $success++;
50
            } elseif ($result === false) {
51
                $errors++;
52
            }
53
            $i++;
54
        }
55
56
        if ($errors > 0) {
57
            $result = _t(__CLASS__ . ".RECORDSPROCESSEDWITHERR", "{err} errors and {count} records processed", ["err" => $errors, "count" => $i]);
58
        } else {
59
            $result = _t(__CLASS__ . ".RECORDSPROCESSED", "{count} records processed", ["count" => $i]);
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Get the value of callable
67
     */
68
    public function getCallable()
69
    {
70
        return $this->callable;
71
    }
72
73
    /**
74
     * Set the value of callable
75
     *
76
     * @param callable $callable
77
     */
78
    public function setCallable($callable): self
79
    {
80
        $this->callable = $callable;
81
        return $this;
82
    }
83
}
84