Passed
Pull Request — v3 (#705)
by Zeky
34:40
created

Discord::processResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 43
rs 9.424
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 * ! THIS PART IS HARDCODED DISCORD STATUS
21
 * ! NOT FINAL VERSION
22
 * ! NO TEST AVAILABLE...
23
 * ! THERE IS NO OPTIONS FOR CUSTOMIZE / NORMALIZE PARAMS
24
 * 
25
 * ! DIDNT TEST: https://github.com/Austinb/GameQ/wiki/Configuration-v3#filters
26
 * ! NOT SURE IF ANY OF THIS WILL WORK.
27
 */
28
29
namespace GameQ\Protocols;
30
31
use GameQ\Exception\Protocol as Exception;
32
use GameQ\Result;
33
use GameQ\Protocol;
34
35
/**
36
 * ECO Global Survival Protocol Class
37
 *
38
 * @author Austin Bischoff <[email protected]>
39
 */
40
class Discord extends Protocol
41
{
42
    /**
43
     * The protocol being used
44
     *
45
     * @var string
46
     */
47
    protected $protocol = 'discord';
48
49
    /**
50
     * String name of this protocol class
51
     *
52
     * @var string
53
     */
54
    protected $name = 'discord';
55
56
    /**
57
     * Longer string name of this protocol class
58
     *
59
     * @var string
60
     */
61
    protected $name_long = "Discord";
62
63
    /**
64
     * query_port = client_port + 1
65
     *
66
     * @type int
67
     */
68
    protected $port_diff = 0;
69
70
    /**
71
     * Process the response
72
     *
73
     * @return array
74
     * @throws Exception
75
     */
76
    public function processResponse()
77
    {
78
        $discord = curl_init('');
79
        curl_setopt($discord, CURLOPT_RETURNTRANSFER, 1);
80
        curl_setopt($discord, CURLOPT_SSL_VERIFYPEER, 0);
81
        curl_setopt($discord, CURLOPT_CONNECTTIMEOUT, 5.0);
82
        curl_setopt($discord, CURLOPT_TIMEOUT, 3);
83
        curl_setopt($discord, CURLOPT_HTTPHEADER, ['Accept: application/json']);
84
        curl_setopt(
85
            $discord,
86
            CURLOPT_URL,
87
            "https://discord.com/api/v10/invites/{$this->options['invite']}?with_counts=true"
88
        );
89
90
        $buffer = curl_exec($discord);
91
        $buffer = json_decode($buffer, true);
0 ignored issues
show
Bug introduced by
It seems like $buffer can also be of type true; however, parameter $json of json_decode() 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

91
        $buffer = json_decode(/** @scrutinizer ignore-type */ $buffer, true);
Loading history...
92
93
        $result = new Result();
94
95
        // Server is always dedicated
96
        $result->add('gq_joinlink', "https://discord.gg/{$this->options['invite']}");
97
        $result->add('gq_address', "https://discord.gg/{$this->options['invite']}");
98
99
        // gq_
100
        $result->add('num_players', $buffer['approximate_presence_count']);
101
        $result->add('max_players', $buffer['approximate_member_count']);
102
        $result->add('dedicated', 1);
103
        $result->add('servername', $buffer['guild']['name']);
104
        $result->add('map', 'discord');
105
        $result->add('game', 'discord');
106
        $result->add('matchtype', 'discord');
107
        $result->add('passsord', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array|string expected by parameter $value of GameQ\Result::add(). ( Ignorable by Annotation )

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

107
        $result->add('passsord', /** @scrutinizer ignore-type */ false);
Loading history...
108
109
        // Others
110
        $result->add('dd_guildid', $buffer['guild']['id']);
111
        $result->add('dd_description', $buffer['guild']['description']);
112
        $result->add('dd_nsfw', $buffer['guild']['nsfw']);
113
        $result->add('dd_features', implode(', ', $buffer['guild']['features']));
114
        $result->add('dd_nsfw', $buffer['guild']['nsfw']);
115
        if (isset($buffer['inviter'])) {
116
            $result->add('dd_inviter', $buffer['inviter']['username'] . "#" . $buffer['inviter']['discriminator']);
117
        }
118
        return $result->fetch();
119
    }
120
}
121