TournamentController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 12
dl 0
loc 261
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A catalogue() 0 6 1
A find() 0 6 1
A tablescores() 0 19 2
A standings() 0 13 1
A store() 0 10 1
A update() 0 14 1
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Match;
6
use App\Tournament;
7
use App\Serializers\Tournament\StandingsSerializer;
8
use App\Serializers\Tournament\TablescoresSerializer;
9
use App\Transformers\StandingsTransformer;
10
use App\Transformers\TournamentTransformer;
11
use App\Transformers\TablescoresTransformer;
12
use App\Http\Requests\Tournament\Create as CreateTournament;
13
use Illuminate\Support\Facades\Auth;
14
use Illuminate\Support\Facades\Input;
15
use Sorskod\Larasponse\Larasponse;
16
17
/**
18
 * Class TournamentController
19
 * @package App\Http\Controllers\API
20
 */
21
class TournamentController extends Controller
22
{
23
    /**
24
     * TournamentController constructor.
25
     * @param Larasponse $response
26
     */
27
    public function __construct(Larasponse $response)
28
    {
29
        $this->response = $response;
30
        parent::__construct($response);
31
    }
32
33
    /**
34
     * @SWG\Get(
35
     *     tags={"Tournament"},
36
     *     path="/api/v1/tournaments",
37
     *     description="Returns all tournaments from database",
38
     *     operationId="catalogue",
39
     *     produces={"application/json"},
40
     *     @SWG\Response(
41
     *     response="200",
42
     *     description="Successfully get list of tournaments"
43
     *     )
44
     * )
45
     */
46
    public function catalogue()
47
    {
48
        $collection = Tournament::with('tournamentTeams.team')->get();
0 ignored issues
show
Bug introduced by
The method get 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...
49
50
        return $this->response->collection($collection, new TournamentTransformer(), 'tournaments');
51
    }
52
53
    /**
54
     * @SWG\Get(
55
     *     tags={"Tournament"},
56
     *     path="/api/v1/tournaments/{tournamentId}",
57
     *     description="Returns specified tournament from database",
58
     *     operationId="find",
59
     *     produces={"application/json"},
60
     *     @SWG\Parameter(
61
     *         description="Tournament id",
62
     *         in="path",
63
     *         name="tournamentId",
64
     *         required=true,
65
     *         type="integer"
66
     *     ),
67
     *     @SWG\Response(
68
     *     response="200",
69
     *     description="Successfully get specified tournament"
70
     *     )
71
     * )
72
     * @param $tournamentId
73
     * @return array
74
     */
75
    public function find($tournamentId)
76
    {
77
        $collection = Tournament::with('tournamentTeams.team')->where(['id' => $tournamentId])->get();
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...
78
79
        return $this->response->collection($collection, new TournamentTransformer(), 'tournaments');
80
    }
81
82
    /**
83
     * @SWG\Get(
84
     *     tags={"Tournament"},
85
     *     path="/api/v1/tablescores",
86
     *     description="Returns tablescores",
87
     *     operationId="tablescores",
88
     *     produces={"application/json"},
89
     *     @SWG\Parameter(
90
     *         description="Tournament id",
91
     *         in="query",
92
     *         name="tournamentId",
93
     *         required=true,
94
     *         type="integer"
95
     *     ),
96
     *     @SWG\Response(
97
     *     response="200",
98
     *     description="Successfully get tablescores"
99
     *     )
100
     * )
101
     */
102
    public function tablescores()
103
    {
104
        $serializer = new TablescoresSerializer();
105
106
        $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...
107
            ->where(['tournamentId' => Input::get('tournamentId')]);
108
109
        $matches = $collection->get();
110
111
        if (!empty($matches->all())) {
112
            return $this->response->collection(
113
                $serializer->collection($matches),
114
                new TablescoresTransformer(),
115
                'tablescore'
116
            );
117
        }
118
119
        return $this->response->collection([]);
120
    }
121
122
    /**
123
     * @SWG\Get(
124
     *     tags={"Tournament"},
125
     *     path="/api/v1/standings",
126
     *     description="Returns standings",
127
     *     operationId="standings",
128
     *     produces={"application/json"},
129
     *     @SWG\Parameter(
130
     *         description="Tournament id",
131
     *         in="query",
132
     *         name="tournamentId",
133
     *         required=true,
134
     *         type="integer"
135
     *     ),
136
     *     @SWG\Response(
137
     *     response="200",
138
     *     description="Successfully get standings"
139
     *     )
140
     * )
141
     */
142
    public function standings()
143
    {
144
        $serializer = new StandingsSerializer();
145
146
        $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...
147
            ->where(['tournamentId' => Input::get('tournamentId')]);
148
149
        return $this->response->collection(
150
            $serializer->collection($collection->get()),
151
            new StandingsTransformer(),
152
            'standings'
153
        );
154
    }
155
156
    /**
157
     * @SWG\Post(
158
     *     tags={"Tournament"},
159
     *     path="/api/v1/tournaments",
160
     *     description="Create new tournament",
161
     *     operationId="store",
162
     *     produces={"application/json"},
163
     *     @SWG\Parameter(
164
     *         description="Tournament name",
165
     *         in="query",
166
     *         name="tournament[name]",
167
     *         required=true,
168
     *         type="string"
169
     *     ),
170
     *     @SWG\Parameter(
171
     *         description="Tournament description",
172
     *         in="query",
173
     *         name="tournament[description]",
174
     *         required=true,
175
     *         type="string"
176
     *     ),
177
     *      @SWG\Parameter(
178
     *         description="Type of tournament: league, knock_out, multistage",
179
     *         in="query",
180
     *         name="tournament[type]",
181
     *         required=true,
182
     *         type="string"
183
     *     ),
184
     *     @SWG\Parameter(
185
     *         description="Members type: single, double",
186
     *         in="query",
187
     *         name="tournament[membersType]",
188
     *         required=true,
189
     *         type="string"
190
     *     ),
191
     *     @SWG\Response(
192
     *     response="200",
193
     *     description="Successfully add tournament"
194
     *     )
195
     * )
196
     * @param CreateTournament $request
197
     * @return array
198
     */
199
    public function store(CreateTournament $request)
200
    {
201
        $input = $request->input('tournament');
202
        $input['status'] = Tournament::STATUS_DRAFT;
203
204
        $tournament = Auth::user()->tournaments()->create($input);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method tournaments() does only exist in the following implementations of said interface: App\Member.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
205
        $tournament = Tournament::where(['id' => $tournament->id])->get();
206
207
        return $this->response->collection($tournament, new TournamentTransformer(), 'tournaments');
208
    }
209
210
    /**
211
     * @SWG\Put(
212
     *     tags={"Tournament"},
213
     *     path="/api/v1/tournaments/{tournamentId}",
214
     *     description="Update specified tournament",
215
     *     operationId="update",
216
     *     produces={"application/json"},
217
     *     @SWG\Parameter(
218
     *         description="Tournament id",
219
     *         in="path",
220
     *         name="tournamentId",
221
     *         required=true,
222
     *         type="integer"
223
     *     ),
224
     *     @SWG\Parameter(
225
     *         description="Tournament name",
226
     *         in="query",
227
     *         name="tournament[name]",
228
     *         required=true,
229
     *         type="string"
230
     *     ),
231
     *     @SWG\Parameter(
232
     *         description="Tournament description",
233
     *         in="query",
234
     *         name="tournament[description]",
235
     *         required=true,
236
     *         type="string"
237
     *     ),
238
     *     @SWG\Parameter(
239
     *         description="Tournament status: draft, started, completed",
240
     *         in="query",
241
     *         name="tournament[status]",
242
     *         required=true,
243
     *         type="string"
244
     *     ),
245
     *      @SWG\Parameter(
246
     *         description="Tournament type: league, knock_out, multistage",
247
     *         in="query",
248
     *         name="tournament[type]",
249
     *         required=true,
250
     *         type="string"
251
     *     ),
252
     *     @SWG\Parameter(
253
     *         description="Members type: single, double",
254
     *         in="query",
255
     *         name="tournament[membersType]",
256
     *         required=true,
257
     *         type="string"
258
     *     ),
259
     *     @SWG\Response(
260
     *     response="200",
261
     *     description="Successfully update tournament"
262
     *     )
263
     * )
264
     * @param $tournamentId
265
     * @return array
266
     */
267
    public function update($tournamentId)
268
    {
269
        $tournament = Tournament::findOrFail($tournamentId);
270
        $tournament->update([
271
            'name' => Input::get('tournament.name'),
272
            'type' => Input::get('tournament.type'),
273
            'status' => Input::get('tournament.status'),
274
            'membersType' => Input::get('tournament.membersType'),
275
            'description' => Input::get('tournament.description')
276
        ]);
277
        $tournament = Tournament::where(['id' => $tournamentId])->get();
278
279
        return $this->response->collection($tournament, new TournamentTransformer(), 'tournaments');
280
    }
281
}
282