MassDeleteTool::setCrud()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Yaro\Jarboe\Table\Toolbar;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Table\CRUD;
7
8
class MassDeleteTool extends AbstractTool
9
{
10
    /**
11
     * Set CRUD object.
12
     *
13
     * @param CRUD $crud
14
     */
15 1
    public function setCrud(CRUD $crud)
16
    {
17 1
        parent::setCrud($crud);
18
19 1
        if ($this->check()) {
20 1
            $this->crud()->enableBatchCheckboxes(true);
21
        }
22 1
    }
23
24
    /**
25
     * Position where should tool be placed.
26
     */
27
    public function position()
28
    {
29
        return self::POSITION_BODY_TOP;
30
    }
31
32
    /**
33
     * Unique tool identifier.
34
     */
35 1
    public function identifier(): string
36
    {
37 1
        return 'mass-delete';
38
    }
39
40
    /**
41
     * Tool's view.
42
     */
43
    public function render()
44
    {
45
        return view('jarboe::crud.toolbar.mass_delete', [
46
            'tool' => $this,
47
            'crud' => $this->crud(),
48
        ]);
49
    }
50
51
    /**
52
     * Handle tool execution.
53
     *
54
     * @param Request $request
55
     *
56
     * @return \Illuminate\Http\JsonResponse
57
     */
58
    public function handle(Request $request)
59
    {
60
        $errors = [];
61
        $hidden = [];
62
        $removed = [];
63
        foreach ($request->get('ids') as $id) {
64
            try {
65
                if (!$this->crud()->repo()->delete($id)) {
66
                    throw new \Exception(__('jarboe::toolbar.mass_delete.delete_event_failed'));
0 ignored issues
show
Bug introduced by
It seems like __('jarboe::toolbar.mass...e.delete_event_failed') can also be of type array; however, parameter $message of Exception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
                    throw new \Exception(/** @scrutinizer ignore-type */ __('jarboe::toolbar.mass_delete.delete_event_failed'));
Loading history...
67
                }
68
69
                try {
70
                    $this->crud()->repo()->find($id);
71
                    $hidden[] = $id;
72
                } catch (\Exception $e) {
73
                    $removed[] = $id;
74
                }
75
            } catch (\Exception $e) {
76
                $errors[$id] = $e->getMessage();
77
            }
78
        }
79
80
        return response()->json([
81
            'errors' => $errors,
82
            'hidden' => $hidden,
83
            'removed' => $removed,
84
        ]);
85
    }
86
87
    /**
88
     * Check allowance to show and process tool.
89
     */
90 1
    public function check(): bool
91
    {
92 1
        return true;
93
    }
94
}
95