Passed
Push — main ( b96c2d...30a05e )
by Richard
04:40
created

PlayerController::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Furic\GameEssentials\Http\Controllers;
4
5
use Furic\GameEssentials\Models\Player;
6
use Furic\GameEssentials\Models\PlayerGame;
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
        return response(Player::findOrFail($id), 200);
22
    }
23
24
    /**
25
     * Show the form for creating a new player resource.
26
     *
27
     * @return \Illuminate\Http\Response
28
     */
29
    public function create(Request $request)
30
    {
31
        // Check if an old player
32
        if ($request->has('facebook_id') && !empty($request->facebook_id)) {
33
            $player = Player::findByFacebookId($request->facebook_id);
34
        } else if ($request->has('playgames_id') && !empty($request->playgames_id)) {
35
            $player = Player::findByPlayGamesId($request->playgames_id);
36
        } else if ($request->has('gamecenter_id') && !empty($request->gamecenter_id)) {
37
            $player = Player::findByGameCenterId($request->gamecenter_id);
38
        } else if ($request->has('udid') && !empty($request->udid)) {
39
            $player = Player::findByUdid($request->udid);
40
        }
41
        
42
        // Create or update
43
        $data = $request->all();
44
        $data['ip'] = filter_input(INPUT_SERVER, "REMOTE_ADDR");
45
        if ($player) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $player does not seem to be defined for all execution paths leading up to this point.
Loading history...
46
            $player->update($data);
47
        } else {
48
            $player = Player::create($data);
49
        }
50
        
51
        // Create playerGame if not exists
52
        if ($request->has('game_id')) {
53
            $playerGame = PlayerGame::findByPlayerGame($player->id, $request->game_id);
54
            if ($playerGame) {
55
                $playerGame->update($request->all());
56
            } else {
57
                $data = $request->all();
58
                $data['player_id'] = $player->id;
59
                PlayerGame::create($data);
60
            }
61
        }
62
        
63
        return response($player, 200);
64
    }
65
    
66
    /**
67
     * Display the ID of the player resource by a given name.
68
     *
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function showId($name)
72
    {
73
        $player = Player::findByName($name);
74
        return response(["id" => $player->id], 200);
75
    }
76
77
    /**
78
     * Display the online status of the player resource.
79
     *
80
     * @return \Illuminate\Http\Response
81
     */
82
    public function showOnlineStatus(Request $request, $id)
83
    {
84
        $player = PlayerGame::findByPlayerGame($id, $request->playgames_id);
85
        return response(["id" => $player->online_status], 200);
0 ignored issues
show
Bug introduced by
The property online_status does not seem to exist on Furic\GameEssentials\Models\PlayerGame. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
86
    }
87
    
88
    /**
89
     * Update the specified player resource in storage.
90
     *
91
     * @param  Request  $request
92
     * @param  int  $id
93
     * @return \Illuminate\Http\Response
94
     */
95
    public function update(Request $request, $id)
96
    {
97
        $player = Player::findOrFail($id);
98
        $player->update($request->all());
99
        return response($player, 200);
100
    }
101
}
102