MatchController::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Http\Requests\MatchUpdate;
6
use App\Match;
7
use App\Transformers\MatchTransformer;
8
use Illuminate\Support\Facades\Input;
9
10
/**
11
 * Class MatchController
12
 * @package App\Http\Controllers\API
13
 */
14
class MatchController extends Controller
15
{
16
    /**
17
     * @SWG\Get(
18
     *     tags={"Match"},
19
     *     path="/api/v1/matches",
20
     *     description="Returns all matches with specified tournamentId from the database",
21
     *     operationId="catalogue",
22
     *     produces={"application/json"},
23
     *     @SWG\Parameter(
24
     *         description="Tournament-team id",
25
     *         in="query",
26
     *         name="teamId",
27
     *         required=true,
28
     *         type="integer"
29
     *     ),
30
     *     @SWG\Parameter(
31
     *         description="Status of match: started, not_started, finished",
32
     *         in="query",
33
     *         name="status",
34
     *         required=true,
35
     *         type="string"
36
     *     ),
37
     *     @SWG\Parameter(
38
     *         description="Tournament id",
39
     *         in="query",
40
     *         name="tournamentId",
41
     *         required=true,
42
     *         type="integer"
43
     *     ),
44
     *     @SWG\Response(
45
     *     response="200",
46
     *     description="Successfully get list of matches"
47
     *     )
48
     * )
49
     */
50
    public function catalogue()
51
    {
52
        $teamId = Input::get('teamId');
53
        $status = Input::get('status');
54
55
        $collection = Match::with(['homeTournamentTeam.team', 'awayTournamentTeam.team'])
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
56
            ->where('tournamentId', Input::get('tournamentId'))
57
            ->orderBy('round')->orderBy('id');
58
59
        if ($status) {
60
            $collection->where('status', $status);
61
        }
62
63
        if ($teamId) {
64
            $collection->where(function ($query) use ($teamId) {
65
                $query->where('homeTournamentTeamId', $teamId)
66
                    ->orWhere('awayTournamentTeamId', $teamId);
67
            });
68
        }
69
70
        return $this->response->collection($collection->get(), new MatchTransformer(), 'matches');
71
    }
72
73
    /**
74
     * @SWG\Put(
75
     *     tags={"Match"},
76
     *     path="/api/v1/matches/{matchId}",
77
     *     description="Update specified match",
78
     *     operationId="update",
79
     *     produces={"application/json"},
80
     *     @SWG\Parameter(
81
     *         description="Match id",
82
     *         in="path",
83
     *         name="matchId",
84
     *         required=true,
85
     *         type="integer"
86
     *     ),
87
     *     @SWG\Parameter(
88
     *         description="New home score",
89
     *         in="query",
90
     *         name="match[homeScore]",
91
     *         required=true,
92
     *         type="integer"
93
     *     ),
94
     *     @SWG\Parameter(
95
     *         description="New away score",
96
     *         in="query",
97
     *         name="match[awayScore]",
98
     *         required=true,
99
     *         type="integer"
100
     *     ),
101
     *     @SWG\Parameter(
102
     *         description="New status",
103
     *         in="query",
104
     *         name="match[status]",
105
     *         required=true,
106
     *         type="string"
107
     *     ),
108
     *     @SWG\Response(
109
     *     response="200",
110
     *     description="Successfully update specified match"
111
     *     )
112
     * )
113
     * @param $matchId
114
     * @param MatchUpdate $request
115
     * @return array
116
     */
117
    public function update($matchId, MatchUpdate $request)
118
    {
119
        /**
120
         * @var $match Match
121
         */
122
        $match = Match::findOrFail($matchId);
123
        $match->update(
124
            array_get($request->only(['match.homeScore', 'match.awayScore', 'match.status']), 'match')
125
        );
126
127
        return $this->response->collection(Match::where(['id' => $matchId])->get(), new MatchTransformer(), 'matches');
128
    }
129
}
130