MassRestoreTool::check()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
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 MassRestoreTool extends AbstractTool
9
{
10
    /**
11
     * Set CRUD object.
12
     *
13
     * @param CRUD $crud
14
     */
15
    public function setCrud(CRUD $crud)
16
    {
17
        parent::setCrud($crud);
18
19
        if ($this->check()) {
20
            $this->crud()->enableBatchCheckboxes(true);
21
        }
22
    }
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
    public function identifier(): string
36
    {
37
        return 'mass-restore';
38
    }
39
40
    /**
41
     * Tool's view.
42
     */
43
    public function render()
44
    {
45
        return view('jarboe::crud.toolbar.mass_restore', [
46
            'tool' => $this,
47
        ]);
48
    }
49
50
    /**
51
     * Handle tool execution.
52
     *
53
     * @param Request $request
54
     *
55
     * @return \Illuminate\Http\JsonResponse
56
     */
57
    public function handle(Request $request)
58
    {
59
        $errors = [];
60
        $restored = [];
61
        foreach ($request->get('ids') as $id) {
62
            try {
63
                if (!$this->crud()->repo()->restore($id)) {
64
                    throw new \Exception(__('jarboe::toolbar.mass_restore.restore_event_failed'));
0 ignored issues
show
Bug introduced by
It seems like __('jarboe::toolbar.mass....restore_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

64
                    throw new \Exception(/** @scrutinizer ignore-type */ __('jarboe::toolbar.mass_restore.restore_event_failed'));
Loading history...
65
                }
66
                $restored[] = $id;
67
            } catch (\Exception $e) {
68
                $errors[$id] = $e->getMessage();
69
            }
70
        }
71
72
        return response()->json([
73
            'errors' => $errors,
74
            'restored' => $restored,
75
        ]);
76
    }
77
78
    /**
79
     * Check allowance to show and process tool.
80
     */
81
    public function check(): bool
82
    {
83
        return $this->crud()->isSoftDeleteEnabled();
84
    }
85
}
86