Passed
Push — main ( e80760...ee0f4a )
by Richard
05:13
created

PlayerController::show()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Furic\GameEssentials\Http\Controllers;
4
5
use Furic\GameEssentials\Models\Player;
6
use Furic\GameEssentials\Models\GamePlayer;
7
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Http\Request;
9
10
class PlayerController extends Controller
11
{
12
13
    /**
14
     * Display the specified player resource.
15
     *
16
     * @param  int  $id
17
     * @return \Illuminate\Http\Response
18
     */
19
    public function show($id)
20
    {
21
        try {
22
            return response(Player::findOrFail($id), 200);
23
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
24
            return response([
25
                'error' => 'No player found.'
26
            ], 400);
27
        }
28
    }
29
30
    /**
31
     * Display the specified player resource with a given name.
32
     *
33
     * @param  string  $name
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function showWithName($name)
37
    {
38
        $player = Player::findByName($name);
39
        if ($player != NULL)
40
            return response($player, 200);
41
        return response([
42
            'error' => 'No player found.'
43
        ], 400);
44
    }
45
46
    /**
47
     * Store a newly created player resource in storage.
48
     *
49
     * @param  Request  $request
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function create(Request $request)
53
    {
54
        // Check if an old player
55
        $player = NULL;
56
        if ($request->has('facebook_id') && !empty($request->facebook_id)) {
57
            $player = Player::findByFacebookId($request->facebook_id);
58
        } else if ($request->has('playgames_id') && !empty($request->playgames_id)) {
59
            $player = Player::findByPlayGamesId($request->playgames_id);
60
        } else if ($request->has('gamecenter_id') && !empty($request->gamecenter_id)) {
61
            $player = Player::findByGameCenterId($request->gamecenter_id);
62
        } else if ($request->has('udid') && !empty($request->udid)) {
63
            $player = Player::findByUdid($request->udid);
64
        }
65
        
66
        // Create or update
67
        $data = $request->all();
68
        $data['ip'] = filter_input(INPUT_SERVER, "REMOTE_ADDR");
69
        if ($player) {
70
            $player->update($data);
71
        } else {
72
            $player = Player::create($data);
73
        }
74
        
75
        // Update/create GamePlayer entry if not exists
76
        if ($request->has('game_id')) {
77
            $playerGame = GamePlayer::findByGamePlayer($request->game_id, $player->id);
78
            if ($playerGame) {
79
                $playerGame->update($request->all());
80
            } else {
81
                $data = $request->all();
82
                $data['player_id'] = $player->id;
83
                GamePlayer::create($data);
84
            }
85
        }
86
        
87
        return response($player, 200);
88
    }
89
    
90
    /**
91
     * Update the specified player resource in storage.
92
     *
93
     * @param  Request  $request
94
     * @param  int  $id
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function update(Request $request, $id)
98
    {
99
        try {
100
            $player = Player::findOrFail($id);
101
            $player->update($request->all());
102
            // Update/create GamePlayer entry
103
            if ($request->has('game_id')) {
104
                $playerGame = GamePlayer::findByPlayerGame($request->game_id, $id);
105
                if ($playerGame) {
106
                    $playerGame->update($request->all());
107
                } else {
108
                    $data = $request->all();
109
                    $data['player_id'] = $player->id;
110
                    PlayerGame::create($data);
0 ignored issues
show
Bug introduced by
The type Furic\GameEssentials\Http\Controllers\PlayerGame was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
                }
112
            }
113
            return response($player, 200);
114
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
115
            return response([
116
                'error' => 'No player found.'
117
            ], 400);
118
        }
119
    }
120
121
    /**
122
     * Display a listing of the game resource by a given player ID.
123
     *
124
     * @param  Request  $request
125
     * @param  int  $id
126
     * @return \Illuminate\Http\Response
127
     */
128
    public function showGames(Request $request, $id)
129
    {
130
        try {
131
            if ($request->limit <= 0) {
132
                return response(Player::findOrFail($id)->games, 200);
133
            } else {
134
                return response(Player::findOrFail($id)->games->take($request->limit), 200);
135
            } 
136
         } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
137
            return response([
138
                'error' => 'No player found.'
139
            ], 400);
140
        }
141
    }
142
143
}