Passed
Push — v3 ( efc739...bb78f7 )
by Austin
02:17
created

CFXPlayers   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 70
ccs 3
cts 8
cp 0.375
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A processResponse() 0 18 4
1
<?php
2
3
/**
4
 * This file is part of GameQ.
5
 *
6
 * GameQ is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GameQ is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace GameQ\Protocols;
21
22
use GameQ\Exception\Protocol as Exception;
23
use GameQ\Protocols\Http;
24
25
/**
26
 * GTA Five M Protocol Class
27
 *
28
 * Server base can be found at https://fivem.net/
29
 *
30
 * Based on code found at https://github.com/LiquidObsidian/fivereborn-query
31
 *
32
 * @author Austin Bischoff <[email protected]>
33
 *
34
 * Adding FiveM Player List by
35
 * @author Jesse Lukas <[email protected]>
36
 */
37
38
class CFXPlayers extends Http
39
{
40
    /**
41
     * Holds the real ip so we can overwrite it back
42
     *
43
     * @var string
44
     */
45
    protected $realIp = null;
46
47
    /**
48
     * Holds the real port so we can overwrite it back
49
     *
50
     * @var int
51
     */
52
    protected $realPortQuery = null;
53
54
    /**
55
     * Packets to send
56
     *
57
     * @var array
58
     */
59
    protected $packets = [
60
        self::PACKET_STATUS => "GET /players.json HTTP/1.0\r\nAccept: */*\r\n\r\n", // Player List
61
    ];
62
63
    /**
64
     * The protocol being used
65
     *
66
     * @var string
67
     */
68
    protected $protocol = 'cfxplayers';
69
70
    /**
71
     * String name of this protocol class
72
     *
73
     * @var string
74
     */
75
    protected $name = 'cfxplayers';
76
77
    /**
78
     * Longer string name of this protocol class
79
     *
80
     * @var string
81
     */
82
    protected $name_long = "cfxplayers";
83
84
    /**
85
     * Process the response
86
     *
87
     * @return array
88
     * @throws Exception
89
     */
90 30
    public function processResponse()
91
    {
92
        // Make sure we have any players
93 30
        if (empty($this->packets_response)) {
94 30
            return [];
95
        }
96
97
        // Implode and rip out the JSON
98
        preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
99
100
        // Return should be JSON, let's validate
101
        if (!isset($matches[0]) || ($json = json_decode($matches[0], true)) === null) {
102
            throw new Exception(__METHOD__ . " JSON response from Stationeers protocol is invalid.");
103
        }
104
105
        // Return json as it should already be well formed
106
        return [
107
            'players' => $json,
108
        ];
109
    }
110
}
111