AbuseController::edit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Models\Eloquent\Abuse;
6
use App\Models\Eloquent\Group;
7
use App\Models\Eloquent\GroupBanned;
8
use App\Models\Eloquent\UserBanned;
9
use Encore\Admin\Controllers\AdminController;
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 AbuseController extends AdminController
17
{
18
    use HasResourceActions;
19
20
    /**
21
     * Title for current resource.
22
     *
23
     * @var string
24
     */
25
    protected $title='Abuses';
26
27
    /**
28
     * Make a grid builder.
29
     *
30
     * @return Grid
31
     */
32
    protected function grid()
33
    {
34
        $grid=new Grid(new Abuse);
35
36
        $grid->column('id', __('Id'));
0 ignored issues
show
Bug introduced by
It seems like __('Id') 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

36
        $grid->column('id', /** @scrutinizer ignore-type */ __('Id'));
Loading history...
37
        $grid->column('title', __('Title'));
38
        $grid->column('cause', __('Cause'))->display(function() {
39
            return Abuse::$cause[$this->cause];
0 ignored issues
show
Bug Best Practice introduced by
The property cause does not exist on App\Admin\Controllers\AbuseController. Did you maybe forget to declare it?
Loading history...
40
        });
41
        $grid->column('supplement', __('Supplement'));
42
        $grid->column('link', __('Link'));
43
        $grid->column('audit', __('Status'))->using(['0' => 'Pending', '1' => 'Passed']);
44
        $grid->column('user', __('Submitter'))->display(function() {
45
            return $this->user->readable_name;
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist on App\Admin\Controllers\AbuseController. Did you maybe forget to declare it?
Loading history...
46
        });
47
        $grid->column('created_at', __('Created at'));
48
        $grid->column('updated_at', __('Updated at'));
49
        return $grid;
50
    }
51
52
    /**
53
     * Make a show builder.
54
     *
55
     * @param mixed $id
56
     * @return Show
57
     */
58
    protected function detail($id)
59
    {
60
        $show=new Show(Abuse::findOrFail($id));
61
62
        $show->field('id', __('Id'));
0 ignored issues
show
Bug introduced by
It seems like __('Id') 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

62
        $show->field('id', /** @scrutinizer ignore-type */ __('Id'));
Loading history...
63
        $show->field('title', __('Title'));
64
        $show->field('cause', __('Cause'));
65
        $show->field('supplement', __('Supplement'));
66
        $show->field('link', __('Link'));
67
        $show->field('audit', __('Audit'));
68
        $show->field('user_id', __('User id'));
69
        $show->field('created_at', __('Created at'));
70
        $show->field('updated_at', __('Updated at'));
71
        $show->field('deleted_at', __('Deleted at'));
72
73
        return $show;
74
    }
75
76
    /**
77
     * Make a form builder.
78
     *
79
     * @return Form
80
     */
81
    protected function form()
82
    {
83
        $form=new Form(new Abuse);
84
85
        $form->text('title', __('Title'));
86
        $form->number('cause', __('Cause'));
87
        $form->textarea('supplement', __('Supplement'));
88
        $form->textarea('link', __('Link'));
89
        $form->switch('audit', __('Audit'));
90
        $form->number('user_id', __('User id'));
91
92
        $form->ignore(['created_at']);
93
94
        $form->saving(function(Form $form) {
95
            $abuse=$form->model();
96
            //get gategory and subject id
97
            $regex='/^([A-Za-z]+) #(\d+)/';
98
            $matches=[];
99
            preg_match($regex, $abuse->title, $matches);
100
            $category=array_search(strtolower($matches[1]), Abuse::$supportCategory);
0 ignored issues
show
Unused Code introduced by
The assignment to $category is dead and can be removed.
Loading history...
101
            $subject_id=(int) $matches[2];
102
            switch ($abuse->category) {
103
                case 0:
104
                    $gid=$subject_id;
105
                    $group=Group::find($gid);
106
                    if (empty($group)) {
107
                        return;
108
                    }
109
                    if ($form->audit) {
110
                        $ban_time=request()->created_at;
111
                        sendMessage([
112
                            'sender'    => config('app.official_sender'),
113
                            'receiver'  => $abuse->user_id,
114
                            'level'     => 5,
115
                            'title'     => "Your abuse report about group {$group->name} was passed",
116
                            'content'   => "Hi, Dear **{$abuse->user->name}**,\n\nWe have checked your Abuse report about group **[{$group->name}]({$group->link})**.\n\n We think you're right.\n\n So as the consequence leading to a temporary/permanent sanction against the group.\n\n Thank you for your contribution to our community environment.\n\n Sincerely, NOJ"
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
117
                        ]);
118
                        sendMessage([
119
                            'sender'    => config('app.official_sender'),
120
                            'receiver'  => $group->leader->id,
121
                            'level'     => 3,
122
                            'title'     => "Your group {$group->name} has been banned.",
123
                            'content'   => "Hi, Dear **{$group->leader->name}**,\n\n For the following reasons: \n\n {$abuse->supplement}\n\n your group **[{$group->name}]({$group->link})** is currently banned and will continue until {$ban_time}.\n\n Before this, only you can enter the group. \n\n Please rectify before this, or you may be subjected to more serious treatment.\n\n Thank you for your contribution to our community environment.\n\n Sincerely, NOJ"
124
                        ]);
125
                        $abuse->delete();
126
                        GroupBanned::create([
127
                            'abuse_id'   => $abuse->id,
128
                            'group_id'   => $group->gid,
129
                            'reason'     => $abuse->supplement,
130
                            'removed_at' => $ban_time
131
                        ]);
132
                        return;
133
                    } else {
134
                        sendMessage([
135
                            'sender'    => config('app.official_sender'),
136
                            'receiver'  => $abuse->user_id,
137
                            'level'     => 2,
138
                            'title'     => "Your abuse report about group {$group->name} was rejected",
139
                            'content'   => "Hi, Dear **{$abuse->user->name}**,\n\n We have checked your Abuse report about group **[{$group->name}]({$group->link})**.\n\n However, we regret to say that the information you submitted is not sufficient for us to take action.\n\n Of course, we will continue to follow up the investigation.\n\n Thank you for your contribution to our community environment.\n\n Sincerely, NOJ"
140
                        ]);
141
                        $abuse->delete();
142
                        return;
143
                    }
144
                    return;
0 ignored issues
show
Unused Code introduced by
return is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
145
                case 1:
146
                    $ban_time=request()->created_at;
147
                    UserBanned::create([
148
                        'abuse_id'   => $abuse->id,
149
                        'user_id'    => $subject_id,
150
                        'reason'     => $abuse->supplement,
151
                        'removed_at' => $ban_time
152
                    ]);
153
                    $abuse->delete();
154
                    return;
155
                default:
156
                    return;
157
            }
158
159
160
        });
161
162
        return $form;
163
    }
164
165
    public function edit($id, Content $content)
166
    {
167
        return $content
168
            ->header('Check Abuses')
169
            ->description('Refer to abuse reports submitted by users')
170
            ->body($this->check_form()->edit($id));
171
    }
172
173
    protected function check_form()
174
    {
175
        $form=new Form(new Abuse);
176
        $form->display('id', __('Abuse id'));
177
        $form->display('title', __('Title'));
178
        $form->display('cause', __('Cause'));
179
        $form->display('supplement', __('Supplement'));
180
        $form->display('link', __('Group Link'));
181
        $form->display('user_id', __('Submitter'));
182
        $form->radio('audit', 'result')->options(['0' => 'Reject', '1'=> 'Pass']);
183
        $form->datetime('created_at', 'ban until');
184
185
        return $form;
186
    }
187
}
188