Passed
Push — develop ( 2d9884...f0fa81 )
by Nikita
05:57
created

ServersRconController::fastRcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Gameap\Http\Controllers\API;
4
5
use Gameap\Http\Controllers\AuthController;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gameap\Http\Controllers\API\AuthController. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Gameap\Http\Requests\API\Rcon\BanRequest;
7
use Gameap\Http\Requests\API\Rcon\CommandRequest;
8
use Gameap\Http\Requests\API\Rcon\KickRequest;
9
use Gameap\Models\Server;
10
use Gameap\Services\RconService;
11
12
class ServersRconController extends AuthController
13
{
14
    /**
15
     * Get supported features list
16
     *
17
     * @param RconService $rconService
18
     * @param Server $server
19
     * @return array
20
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
21
     */
22
    public function supportedFeatures(RconService $rconService, Server $server)
23
    {
24
        return $rconService->supportedFeatures($server);
25
    }
26
27
    public function fastRcon(Server $server)
28
    {
29
        $this->authorize('server-rcon-console', $server);
30
31
        return $server->gameMod->fast_rcon;
32 9
    }
33
34 9
    /**
35
     * @param RconService $rconService
36
     * @param CommandRequest $request
37 6
     * @return array
38
     */
39 6
    public function sendCommand(CommandRequest $request, RconService $rconService, Server $server)
40
    {
41
        $this->authorize('server-rcon-console', $server);
42
43
        return [
44
            'output' => $server->processActive()
45
                ? $rconService->sendCommand($server, $request->post('command'))
0 ignored issues
show
Bug introduced by
It seems like $request->post('command') can also be of type array and null; however, parameter $command of Gameap\Services\RconService::sendCommand() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
                ? $rconService->sendCommand($server, /** @scrutinizer ignore-type */ $request->post('command'))
Loading history...
46
                : 'Server is offline',
47
        ];
48
    }
49
50
    /**
51 9
     * Get Player list
52
     *
53 9
     * @param RconService $rconService
54
     * @param Server $server
55 6
     * @return array
56 6
     * @throws \Knik\GRcon\Exceptions\PlayersManageNotSupportedExceptions
57
     */
58
    public function getPlayers(RconService $rconService, Server $server)
59
    {
60
        $this->authorize('server-rcon-players', $server);
61
62
        if (!$server->processActive()) {
63
            return [];
64
        }
65
66
        return $rconService->getPlayers($server);
67
    }
68
69
    /**
70
     * Kick Player
71 9
     *
72
     * @param KickRequest $request
73 9
     * @param RconService $rconService
74
     * @param Server $server
75 6
     * @return mixed
76 6
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
77
     */
78
    public function kick(KickRequest $request, RconService $rconService, Server $server)
79
    {
80
        $this->authorize('server-rcon-players', $server);
81
82
        if (!$server->processActive()) {
83
            return 'Server is offline';
84
        }
85
86
        return $rconService->kick($server, $request->post('player'), $request->post('reason'));
0 ignored issues
show
Bug introduced by
It seems like $request->post('reason') can also be of type array and null; however, parameter $reason of Gameap\Services\RconService::kick() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        return $rconService->kick($server, $request->post('player'), /** @scrutinizer ignore-type */ $request->post('reason'));
Loading history...
87
    }
88
89
    /**
90
     * Ban Player
91
     *
92 9
     * @param BanRequest $request
93
     * @param RconService $rconService
94 9
     * @param Server $server
95
     * @return mixed
96 6
     * @throws \Knik\GRcon\Exceptions\PlayersManageNotSupportedExceptions
97 6
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
98
     */
99
    public function ban(BanRequest $request, RconService $rconService, Server $server)
100
    {
101
        $this->authorize('server-rcon-players', $server);
102
103
        if (!$server->processActive()) {
104
            return 'Server is offline';
105
        }
106
107
        return $rconService->ban(
108 9
            $server,
109
            $request->post('player'),
110 9
            $request->post('reason'),
0 ignored issues
show
Bug introduced by
It seems like $request->post('reason') can also be of type array and null; however, parameter $reason of Gameap\Services\RconService::ban() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            /** @scrutinizer ignore-type */ $request->post('reason'),
Loading history...
111
            $request->post('time')
0 ignored issues
show
Bug introduced by
$request->post('time') of type array|null|string is incompatible with the type integer expected by parameter $time of Gameap\Services\RconService::ban(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
            /** @scrutinizer ignore-type */ $request->post('time')
Loading history...
112 6
        );
113
    }
114
115
    public function message(RconService $rconService, Server $server)
116 6
    {
117
        $this->authorize('server-rcon-players', $server);
118
119
        if ($server->processActive()) {
120
            return 'Server is offline';
121
        }
122
123
        return 'Not implemented';
124
    }
125
}
126