Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

AbuseController::edit()   A

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

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

63
        $show->field('id', /** @scrutinizer ignore-type */ __('Id'));
Loading history...
64
        $show->field('title', __('Title'));
65
        $show->field('cause', __('Cause'));
66
        $show->field('supplement', __('Supplement'));
67
        $show->field('link', __('Link'));
68
        $show->field('audit', __('Audit'));
69
        $show->field('user_id', __('User id'));
70
        $show->field('created_at', __('Created at'));
71
        $show->field('updated_at', __('Updated at'));
72
        $show->field('deleted_at', __('Deleted at'));
73
74
        return $show;
75
    }
76
77
    /**
78
     * Make a form builder.
79
     *
80
     * @return Form
81
     */
82
    protected function form()
83
    {
84
        $form = new Form(new Abuse);
85
86
        $form->text('title', __('Title'));
87
        $form->number('cause', __('Cause'));
88
        $form->textarea('supplement', __('Supplement'));
89
        $form->textarea('link', __('Link'));
90
        $form->switch('audit', __('Audit'));
91
        $form->number('user_id', __('User id'));
92
93
        $form->ignore(['created_at']);
94
95
        $form->saving(function (Form $form) {
96
            $abuse = $form->model();
97
            //get gategory and subject id
98
            $regex = '/^([A-Za-z]+) #(\d+)/';
99
            $matches = [];
100
            preg_match($regex,$abuse->title,$matches);
0 ignored issues
show
Bug introduced by
It seems like $abuse->title can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $subject of preg_match() 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

100
            preg_match($regex,/** @scrutinizer ignore-type */ $abuse->title,$matches);
Loading history...
101
            $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...
102
            $subject_id = (int)$matches[2];
103
            switch($abuse->category) {
104
                case 0:
105
                    $gid = $subject_id;
106
                    $group = Group::find($gid);
107
                    if(empty($group)) {
108
                        return ;
109
                    }
110
                    if($form->audit) {
111
                        $ban_time = request()->created_at;
112
                        sendMessage([
113
                            'sender'    => 1,
114
                            'receiver'  => $abuse->user_id,
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'    => 1,
120
                            'receiver'  => $group->leader->id,
121
                            'title'     => "Your group {$group->name} has been banned.",
122
                            '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"
123
                        ]);
124
                        $abuse->delete();
125
                        GroupBanned::create([
126
                            'abuse_id'   => $abuse->id,
127
                            'group_id'   => $group->gid,
128
                            'reason'     => $abuse->supplement,
129
                            'removed_at' => $ban_time
130
                        ]);
131
                        return ;
132
                    }else{
133
                        sendMessage([
134
                            'sender'    => 1,
135
                            'receiver'  => $abuse->user_id,
136
                            'title'     => "Your abuse report about group {$group->name} was rejected",
137
                            '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"
138
                        ]);
139
                        $abuse->delete();
140
                        return ;
141
                    }
142
                    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...
143
                case 1:
144
                    $ban_time = request()->created_at;
145
                    UserBanned::create([
146
                        'abuse_id'   => $abuse->id,
147
                        'user_id'    => $subject_id,
148
                        'reason'     => $abuse->supplement,
149
                        'removed_at' => $ban_time
150
                    ]);
151
                    $abuse->delete();
152
                    return;
153
                default:
154
                    return;
155
            }
156
157
158
        });
159
160
        return $form;
161
    }
162
163
    public function edit($id, Content $content)
164
    {
165
        return $content
166
            ->header('Check Abuses')
167
            ->description('Refer to abuse reports submitted by users')
168
            ->body($this->check_form()->edit($id));
169
    }
170
171
    protected function check_form()
172
    {
173
        $form = new Form(new Abuse);
174
        $form->display('id',__('Abuse id'));
175
        $form->display('title', __('Title'));
176
        $form->display('cause', __('Cause'));
177
        $form->display('supplement', __('Supplement'));
178
        $form->display('link', __('Group Link'));
179
        $form->display('user_id', __('Submitter'));
180
        $form->radio('audit','result')->options(['0' => 'Reject', '1'=> 'Pass']);
181
        $form->datetime('created_at','ban until');
182
183
        return $form;
184
    }
185
}
186