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

AbuseController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 35
dl 0
loc 49
rs 10
c 1
b 0
f 1
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findCause() 0 7 2
A report() 0 36 2
1
<?php
2
3
namespace App\Http\Controllers\Ajax;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\ResponseModel;
7
use App\Models\Eloquent\Abuse;
8
use App\Models\Eloquent\Group;
9
use Illuminate\Http\Request;
10
use Illuminate\Validation\Rule;
11
use Auth;
12
use Str;
13
use Arr;
14
use Throwable;
15
16
class AbuseController extends Controller
17
{
18
    protected static $cause=[];
19
20
    public static function findCause($causeDesc){
21
        if(empty($cause)){
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cause does not exist. Did you maybe mean $causeID?
Loading history...
22
            self::$cause=array_flip(Abuse::$cause);
0 ignored issues
show
Bug introduced by
App\Models\Eloquent\Abuse::cause of type integer is incompatible with the type array expected by parameter $array of array_flip(). ( Ignorable by Annotation )

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

22
            self::$cause=array_flip(/** @scrutinizer ignore-type */ Abuse::$cause);
Loading history...
23
        }
24
        $causeID=Arr::get(self::$cause, $causeDesc, 0);
25
        $causeDesc=Arr::get(Abuse::$cause, $causeID, 'General');
0 ignored issues
show
Bug introduced by
App\Models\Eloquent\Abuse::cause of type integer is incompatible with the type ArrayAccess|array expected by parameter $array of Illuminate\Support\Arr::get(). ( Ignorable by Annotation )

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

25
        $causeDesc=Arr::get(/** @scrutinizer ignore-type */ Abuse::$cause, $causeID, 'General');
Loading history...
Bug introduced by
It seems like $causeID can also be of type array; however, parameter $key of Illuminate\Support\Arr::get() does only seem to accept integer|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

25
        $causeDesc=Arr::get(Abuse::$cause, /** @scrutinizer ignore-type */ $causeID, 'General');
Loading history...
26
        return [$causeID, $causeDesc];
27
    }
28
29
    public function report(Request $request)
30
    {
31
        $request->validate([
32
            "supplement" => "required|string",
33
            "category" => ['required', Rule::in(Abuse::$supportCategory)],
34
            "subject_id" => "required|integer"
35
        ]);
36
        $category2link=[
37
            'group'=>function($id){
38
                return route('group.detail',['gcode'=>Group::findOrFail($id)->gcode]);
39
            },
40
            'user'=>function($id){
41
                return route('user.view',['uid' => $id]);
42
            }
43
        ];
44
        $supplement = $request->input('supplement');
45
        $category = $request->input('category');
46
        $subject_id = $request->input('subject_id');
47
        try {
48
            $link = $category2link[$category]($subject_id);
49
        } catch(Throwable $e){
50
            return ResponseModel::err(9001);
51
        }
52
        $uid = Auth::user()->id;
53
        [$causeID, $causeDesc] = self::findCause('General');
54
        $abuseRecord = Abuse::create([
55
            'title' => Str::title($category)." #$subject_id Abused - $causeDesc",
56
            'category' => array_search($category, Abuse::$supportCategory),
57
            'cause' => $causeID,
58
            'supplement' => $supplement,
59
            'link' => $link,
60
            'user_id' => $uid,
61
        ]);
62
        $abuseRecord->save();
63
        return ResponseModel::success(200, null, [
64
            'id' => $abuseRecord->id
65
        ]);
66
    }
67
}
68