DojoPassesController::edit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Models\Eloquent\Dojo\DojoPass;
6
use App\Models\Eloquent\Dojo\Dojo;
7
use App\Models\Eloquent\User;
8
use App\Http\Controllers\Controller;
9
use Illuminate\Support\MessageBag;
10
use Encore\Admin\Controllers\HasResourceActions;
11
use Encore\Admin\Form;
12
use Encore\Admin\Grid;
13
use Encore\Admin\Layout\Content;
14
use Encore\Admin\Show;
15
16
class DojoPassesController extends Controller
17
{
18
    use HasResourceActions;
19
20
    /**
21
     * Index interface.
22
     *
23
     * @param Content $content
24
     * @return Content
25
     */
26
    public function index(Content $content)
27
    {
28
        return $content
29
            ->header(__('admin.dojopasses.index.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.index.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() 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

29
            ->header(/** @scrutinizer ignore-type */ __('admin.dojopasses.index.header'))
Loading history...
30
            ->description(__('admin.dojopasses.index.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.index.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() 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

30
            ->description(/** @scrutinizer ignore-type */ __('admin.dojopasses.index.description'))
Loading history...
31
            ->body($this->grid()->render());
32
    }
33
34
    /**
35
     * Show interface.
36
     *
37
     * @param mixed $id
38
     * @param Content $content
39
     * @return Content
40
     */
41
    public function show($id, Content $content)
42
    {
43
        return $content
44
            ->header(__('admin.dojopasses.show.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.show.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() 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

44
            ->header(/** @scrutinizer ignore-type */ __('admin.dojopasses.show.header'))
Loading history...
45
            ->description(__('admin.dojopasses.show.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.show.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() 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

45
            ->description(/** @scrutinizer ignore-type */ __('admin.dojopasses.show.description'))
Loading history...
46
            ->body($this->detail($id));
47
    }
48
49
    /**
50
     * Edit interface.
51
     *
52
     * @param mixed $id
53
     * @param Content $content
54
     * @return Content
55
     */
56
    public function edit($id, Content $content)
57
    {
58
        return $content
59
            ->header(__('admin.dojopasses.edit.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.edit.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() 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

59
            ->header(/** @scrutinizer ignore-type */ __('admin.dojopasses.edit.header'))
Loading history...
60
            ->description(__('admin.dojopasses.edit.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.edit.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() 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

60
            ->description(/** @scrutinizer ignore-type */ __('admin.dojopasses.edit.description'))
Loading history...
61
            ->body($this->form()->edit($id));
62
    }
63
64
    /**
65
     * Create interface.
66
     *
67
     * @param Content $content
68
     * @return Content
69
     */
70
    public function create(Content $content)
71
    {
72
        return $content
73
            ->header(__('admin.dojopasses.create.header'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.create.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() 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

73
            ->header(/** @scrutinizer ignore-type */ __('admin.dojopasses.create.header'))
Loading history...
74
            ->description(__('admin.dojopasses.create.description'))
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.create.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() 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

74
            ->description(/** @scrutinizer ignore-type */ __('admin.dojopasses.create.description'))
Loading history...
75
            ->body($this->form());
76
    }
77
78
    /**
79
     * Make a grid builder.
80
     *
81
     * @return Grid
82
     */
83
    protected function grid()
84
    {
85
        $grid=new Grid(new DojoPass);
86
        $grid->column('id', "ID")->sortable();
87
        $grid->column("dojo_name", __('admin.dojopasses.dojo'))->display(function() {
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.dojo') can also be of type array and array; however, parameter $label of Encore\Admin\Grid::column() 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

87
        $grid->column("dojo_name", /** @scrutinizer ignore-type */ __('admin.dojopasses.dojo'))->display(function() {
Loading history...
88
            return $this->dojo->name;
0 ignored issues
show
Bug Best Practice introduced by
The property dojo does not exist on App\Admin\Controllers\DojoPassesController. Did you maybe forget to declare it?
Loading history...
89
        });
90
        $grid->column("user_readable", __('admin.dojopasses.user'))->display(function() {
91
            return $this->user->readable_name;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist on App\Admin\Controllers\DojoPassesController. Did you maybe forget to declare it?
Loading history...
92
        });
93
        $grid->column('updated_at', __('admin.dojopasses.updated_at'));
94
95
        $grid->filter(function(Grid\Filter $filter) {
96
            $filter->disableIdFilter();
97
            $filter->column(6, function($filter) {
98
                $filter->equal('dojo_id', __('admin.dojopasses.dojo'))->select(Dojo::all()->pluck('name', 'id'));
99
            });
100
            $filter->column(6, function($filter) {
101
                $filter->equal('user_id', __('admin.dojopasses.user'))->select(function($id) {
102
                    $user=User::find($id);
103
                    if ($user) {
104
                        return [$user->id => $user->readable_name];
105
                    }
106
                })->config('minimumInputLength', 4)->ajax(route('admin.api.users'));
107
            });
108
        });
109
        return $grid;
110
    }
111
112
    /**
113
     * Make a show builder.
114
     *
115
     * @param mixed $id
116
     * @return Show
117
     */
118
    protected function detail($id)
119
    {
120
        $show=new Show(DojoPass::findOrFail($id));
121
        $show->field('id', "ID");
122
        $show->field("dojo_name", __('admin.dojopasses.dojo'))->as(function() {
0 ignored issues
show
Bug introduced by
It seems like __('admin.dojopasses.dojo') can also be of type array and array; however, parameter $label of Encore\Admin\Show::field() 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

122
        $show->field("dojo_name", /** @scrutinizer ignore-type */ __('admin.dojopasses.dojo'))->as(function() {
Loading history...
123
            return $this->dojo->name;
0 ignored issues
show
Bug Best Practice introduced by
The property dojo does not exist on App\Admin\Controllers\DojoPassesController. Did you maybe forget to declare it?
Loading history...
124
        });
125
        $show->field("user_readable", __('admin.dojopasses.user'))->as(function() {
126
            return $this->user->readable_name;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist on App\Admin\Controllers\DojoPassesController. Did you maybe forget to declare it?
Loading history...
127
        });
128
        $show->field('updated_at', __('admin.dojopasses.updated_at'));
129
        return $show;
130
    }
131
132
    /**
133
     * Make a form builder.
134
     *
135
     * @return Form
136
     */
137
    protected function form()
138
    {
139
        $form=new Form(new DojoPass);
140
        $form->tab('Basic', function(Form $form) {
141
            $form->display('id', 'ID');
142
            $form->select('dojo_id', __('admin.dojopasses.dojo'))->options(Dojo::all()->pluck('name', 'id'))->rules('required');
143
            $form->select('user_id', __('admin.dojopasses.user'))->options(function($id) {
144
                $user=User::find($id);
145
                if ($user) {
146
                    return [$user->id => $user->readable_name];
147
                }
148
            })->config('minimumInputLength', 4)->ajax(route('admin.api.users'))->rules('required');
149
150
            $form->saving(function(Form $form) {
151
                $err=function($msg, $title='Error occur.') {
152
                    $error=new MessageBag([
153
                        'title'   => $title,
154
                        'message' => $msg,
155
                    ]);
156
                    return back()->with(compact('error'));
157
                };
158
                $user_id=$form->user_id;
159
                $dojo_id=$form->dojo_id;
160
                $pass=DojoPass::where([
161
                    "dojo_id" => $dojo_id,
162
                    "user_id" => $user_id,
163
                ])->first();
164
165
                $pass_id=$form->model()->id ?? null;
166
                if (!blank($pass) && $pass->id!=$pass_id) {
167
                    return $err('User has passed this dojo', 'Error occured.');
168
                }
169
            });
170
        });
171
        return $form;
172
    }
173
}
174