TeamController::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\API;
4
5
use App\Http\Requests\Tournament\RemoveTeam;
6
use App\League;
7
use App\Team;
8
use App\TournamentTeam;
9
use App\Transformers\TeamSearchTransformer;
10
use App\Transformers\TeamTransformer;
11
use App\Transformers\TournamentTeamTransformer;
12
use Illuminate\Support\Facades\Input;
13
use App\Http\Requests\CreateTeam;
14
15
/**
16
 * Class TeamController
17
 * @package App\Http\Controllers\API
18
 */
19
class TeamController extends Controller
20
{
21
    /**
22
     * @SWG\Get(
23
     *     tags={"Team"},
24
     *     path="/api/v1/teams/all",
25
     *     description="Returns all teams from the database",
26
     *     operationId="catalogue",
27
     *     produces={"application/json"},
28
     *     @SWG\Response(
29
     *     response="200",
30
     *     description="Successfully get list of teams"
31
     *     )
32
     * )
33
     */
34
    public function catalogue()
35
    {
36
        return $this->response->collection(Team::all(), new TeamTransformer(), 'teams');
37
    }
38
39
    /**
40
     * @SWG\Get(
41
     *     tags={"Team"},
42
     *     path="/api/v1/teams/{teamId}",
43
     *     description="Returns specified team from tournament",
44
     *     operationId="find",
45
     *     produces={"application/json"},
46
     *     @SWG\Parameter(
47
     *         description="Team id",
48
     *         in="path",
49
     *         name="teamId",
50
     *         required=true,
51
     *         type="integer"
52
     *     ),
53
     *     @SWG\Response(
54
     *     response="200",
55
     *     description="Successfully get specified team"
56
     *     )
57
     * )
58
     * @param $teamId
59
     * @return array
60
     */
61
    public function find($teamId)
62
    {
63
        $collection = TournamentTeam::where(['teamId' => $teamId]);
64
65
        return $this->response->collection($collection->get(), new TournamentTeamTransformer(), 'teams');
66
    }
67
68
    /**
69
     * @SWG\Get(
70
     *     tags={"Team"},
71
     *     path="/api/v1/teams/search",
72
     *     description="Returns teams we search from database",
73
     *     operationId="search",
74
     *     produces={"application/json"},
75
     *     @SWG\Parameter(
76
     *         description="First letters of team name",
77
     *         in="query",
78
     *         name="term",
79
     *         required=true,
80
     *         type="string"
81
     *     ),
82
     *     @SWG\Response(
83
     *     response="200",
84
     *     description="Successfully get teams we search"
85
     *     )
86
     * )
87
     */
88
    public function search()
89
    {
90
        $collection = Team::with('tournamentTeams.tournament')->where('name', 'ilike', strtolower(Input::get('term')) . '%')->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...
91
92
        return $this->response->collection(
93
            $collection,
94
            new TeamSearchTransformer(),
95
            'teams'
96
        );
97
    }
98
99
    /**
100
     * @SWG\Delete(
101
     *     tags={"Team"},
102
     *     path="/api/v1/teams/{teamId}",
103
     *     description="Delete specified team from tournament",
104
     *     operationId="remove",
105
     *     produces={"application/json"},
106
     *     @SWG\Parameter(
107
     *         description="Team id",
108
     *         in="path",
109
     *         name="teamId",
110
     *         required=true,
111
     *         type="integer"
112
     *     ),
113
     *     @SWG\Response(
114
     *     response="200",
115
     *     description="Successfully remove specified team"
116
     *     )
117
     * )
118
     * @param $teamId
119
     * @return
120
     */
121
    public function remove($teamId)
122
    {
123
        return TournamentTeam::where(['teamId' => $teamId])->delete();
124
    }
125
126
    /**
127
     * @SWG\Post(
128
     *     tags={"Team"},
129
     *     path="/api/v1/leagueTeams",
130
     *     description="Add new team to database",
131
     *     operationId="store",
132
     *     produces={"application/json"},
133
     *     @SWG\Parameter(
134
     *         description="League id",
135
     *         in="formData",
136
     *         name="team[leagueId]",
137
     *         required=true,
138
     *         type="integer"
139
     *     ),
140
     *     @SWG\Parameter(
141
     *         description="Team name",
142
     *         in="formData",
143
     *         name="team[name]",
144
     *         required=true,
145
     *         type="string"
146
     *     ),
147
     *     @SWG\Parameter(
148
     *         description="Team logo",
149
     *         in="formData",
150
     *         name="team[logoPath]",
151
     *         required=false,
152
     *         type="file"
153
     *     ),
154
     *     @SWG\Response(
155
     *     response="200",
156
     *     description="Successfully add new team"
157
     *     )
158
     * )
159
     */
160
    public function store(CreateTeam $request)
161
    {
162
        $team = new Team();
163
        $team = $team->addTeam($request);
164
165
        return $this->response->collection(Team::where(['id' => $team->id])->get(), new TeamTransformer(), 'leagueTeams');
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Team>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
    }
167
168
    /**
169
     * @SWG\Delete(
170
     *     tags={"Team"},
171
     *     path="/api/v1/leagueTeams/{teamId}",
172
     *     description="Delete specified team from database",
173
     *     operationId="delete",
174
     *     produces={"application/json"},
175
     *     @SWG\Parameter(
176
     *         description="Team id",
177
     *         in="path",
178
     *         name="teamId",
179
     *         required=true,
180
     *         type="integer"
181
     *     ),
182
     *     @SWG\Response(
183
     *     response="200",
184
     *     description="Successfully remove specified team"
185
     *     )
186
     * )
187
     */
188
    public function delete($id)
189
    {
190
        TournamentTeam::where(['teamId' => $id])->delete();
191
        return Team::where(['id' => $id])->delete();
192
    }
193
}
194