Test Setup Failed
Push — master ( 0a8e7c...ce45b0 )
by Sam
04:25
created

ResultSubmissionsController::update()   B

Complexity

Conditions 8
Paths 66

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 37
nc 66
nop 2
dl 0
loc 56
rs 8.0835
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Association;
6
use App\Result;
7
use App\TeamResult;
8
use App\ResultSubmission;
9
use Illuminate\Http\Request;
10
11
class ResultSubmissionsController extends Controller
12
{
13
    /**
14
     * Display a listing of the resource.
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function index(Association $association)
19
    {
20
        return view('result_submissions.approve', ['association' => $association]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('result_subm...tion' => $association)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
21
    }
22
23
    /**
24
     * Show the form for creating a new resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function create()
29
    {
30
        //
31
    }
32
33
    /**
34
     * Store a newly created resource in storage.
35
     *
36
     * @param  \Illuminate\Http\Request  $request
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function store(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

39
    public function store(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        //
42
    }
43
44
    /**
45
     * Display the specified resource.
46
     *
47
     * @param  int  $id
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function show($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

50
    public function show(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        //
53
    }
54
55
    /**
56
     * Show the form for editing the specified resource.
57
     *
58
     * @param  int  $id
59
     * @return \Illuminate\Http\Response
60
     */
61
    public function edit($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

61
    public function edit(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        //
64
    }
65
66
    /**
67
     * Update the specified resource in storage.
68
     *
69
     * @param  \Illuminate\Http\Request  $request
70
     * @param  int  $id
71
     * @return \Illuminate\Http\Response
72
     */
73
    public function update(Request $request, $id)
74
    {
75
        $submission = ResultSubmission::find($id);
76
77
        if ($request->delete == 'delete') {
78
            $submission->delete();
79
        }
80
        else {
81
            $result = Result::where('match_id', $submission->match_id)->first();
82
83
            if (empty($result)) {
84
                $result = new Result();
85
                $result->match_id = $submission->match_id;
86
            }
87
88
            $result->home_team_id = $submission->match->home_team_id;
89
            $result->away_team_id = $submission->match->away_team_id;
90
            $result->home_team_score = $submission->home_team_score;
91
            $result->away_team_score = $submission->away_team_score;
92
93
            $result->save();
94
95
            // FIXME: need to look up / overwrite existing team results as applicable:
96
            // home team result:
97
            $team_result = new TeamResult();
98
            $team_result->schedule_id = $submission->match->schedule_id;
99
            $team_result->match_id = $submission->match->id;
100
            $team_result->team_id = $result->home_team_id;
101
            $team_result->points = $result->home_team_score;
102
            $team_result->win = $result->home_team_id == $submission->win_team_id ? 1 : 0;
103
            $team_result->loss = $result->home_team_id != $submission->win_team_id ? 1 : 0;
104
            $team_result->tie = 0;
105
            $team_result->save();
106
107
            // away team result;
108
            $team_result = new TeamResult();
109
            $team_result->schedule_id = $submission->match->schedule_id;
110
            $team_result->match_id = $submission->match->id;
111
            $team_result->team_id = $result->away_team_id;
112
            $team_result->points = $result->away_team_score;
113
            $team_result->win = $result->away_team_id == $submission->win_team_id ? 1 : 0;
114
            $team_result->loss = $result->away_team_id != $submission->win_team_id ? 1 : 0;
115
            $team_result->tie = 0;
116
            $team_result->save();
117
118
            $submission->approved = TRUE;
119
            $submission->save();
120
        }
121
122
        $url = $request->url;
123
124
        if (!empty($url)) {
125
            return redirect($url)->with('success', 'Data saved successfully!');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect($url)->w...a saved successfully!') also could return the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
126
        }
127
128
        return redirect()->route('user', ['id' => \Auth::user()->id]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route...' => Auth::user()->id)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
129
    }
130
131
    /**
132
     * Remove the specified resource from storage.
133
     *
134
     * @param  int  $id
135
     * @return \Illuminate\Http\Response
136
     */
137
    public function destroy($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

137
    public function destroy(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
    {
139
        //
140
    }
141
}
142