Passed
Push — master ( e8fe62...89e454 )
by Thomas
05:17 queued 02:34
created

GenericBulkAction::process()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 21
c 1
b 1
f 0
dl 0
loc 30
rs 8.6506
cc 7
nc 11
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
            try {
47
                $result = $this->callable($record, $this->tabulatorGrid);
0 ignored issues
show
Bug introduced by
The method callable() does not exist on LeKoala\Tabulator\BulkActions\GenericBulkAction. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

47
                /** @scrutinizer ignore-call */ 
48
                $result = $this->callable($record, $this->tabulatorGrid);
Loading history...
48
                if ($result) {
49
                    $success++;
50
                } elseif ($result === false) {
51
                    $errors++;
52
                }
53
            } catch (\Throwable $th) {
54
                $errors++;
55
            }
56
            $i++;
57
        }
58
        if ($errors) {
59
            $result = _t(__CLASS__ . ".RECORDSPROCESSEDWITHERR", "{err} errors and {count} records processed", ["err", $errors, "count" => $i]);
60
        } else {
61
            $result = _t(__CLASS__ . ".RECORDSPROCESSED", "{count} records processed", ["count" => $i]);
62
        }
63
64
        return $result;
65
    }
66
67
    /**
68
     * Get the value of callable
69
     */
70
    public function getCallable()
71
    {
72
        return $this->callable;
73
    }
74
75
    /**
76
     * Set the value of callable
77
     *
78
     * @param callable $callable
79
     */
80
    public function setCallable($callable): self
81
    {
82
        $this->callable = $callable;
83
        return $this;
84
    }
85
}
86