RconService::ban()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Gameap\Services;
4
5
use Gameap\Models\Server;
6
use Knik\GRcon\EasyGRcon;
7
use Knik\GRcon\Exceptions\GRconException;
8
9
class RconService
10
{
11
    /**
12
     * @var EasyGRcon
13
     */
14
    private $rcon;
15
16
    /**
17
     * RconService constructor.
18
     * @param EasyGRcon $rcon
19
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
20
     */
21 6
    public function __construct(EasyGRcon $rcon)
22
    {
23 6
        $this->rcon = $rcon;
24 6
    }
25
26
    /**
27
     * @param Server $server
28
     * @return array
29
     */
30 3
    public function supportedFeatures(Server $server)
31
    {
32
        try {
33 3
            $this->setRconOptions($server);
34
35
            return [
36 2
                'rcon'          => true,
37 2
                'playersManage' => $this->rcon->isPlayersManageSupported(),
38
            ];
39 1
        } catch (GRconException $exception) {
40
            return [
41 1
                'rcon'          => false,
42
                'playersManage' => false,
43
            ];
44
        }
45
    }
46
47
    /**
48
     * @param Server $server
49
     * @param string $command
50
     */
51
    public function sendCommand(Server $server, string $command)
52
    {
53
        $this->setRconOptions($server);
54
        return $this->rcon->execute($command);
55
    }
56
57
    /**
58
     * @param Server $server
59
     * @return array
60
     * @throws \Knik\GRcon\Exceptions\PlayersManageNotSupportedExceptions
61
     */
62
    public function getPlayers(Server $server)
63
    {
64
        $this->setRconOptions($server);
65
66
        return $this->rcon->getPlayers();
67
    }
68
69
    /**
70
     * @param Server $server
71
     * @param mixed $playerId
72
     * @param string $reason
73
     * @return mixed
74
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
75
     */
76
    public function kick(Server $server, $playerId, string $reason = '')
77
    {
78
        $this->setRconOptions($server);
79
80
        return $this->rcon->kick($playerId, $reason);
81
    }
82
83
    /**
84
     * @param Server $server
85
     * @param mixed $playerId
86
     * @param string $reason
87
     * @param int $time
88
     * @return mixed
89
     * @throws \Knik\GRcon\Exceptions\PlayersManageNotSupportedExceptions
90
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
91
     */
92
    public function ban(Server $server, $playerId, string $reason = '', int $time = 0)
93
    {
94
        $this->setRconOptions($server);
95
96
        return $this->rcon->ban($playerId, $reason, $time);
97
    }
98
99
    /**
100
     * @param Server $server
101
     * @throws \Knik\GRcon\Exceptions\ProtocolNotSupportedException
102
     */
103 3
    private function setRconOptions(Server $server): void
104
    {
105 3
        $this->rcon->setProtocol(strtolower($server->game->engine), [
106 3
            'host'     => $server->server_ip,
107 3
            'port'     => $server->rcon_port,
108 3
            'password' => $server->rcon,
109
        ]);
110 2
    }
111
}
112