Main::getStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace guastallaigor\PhpBattlerite;
4
5
use GuzzleHttp\Exception\RequestException;
6
7
/**
8
 * PHP-Battlerite easy API.
9
 *
10
 * @category  Games
11
 *
12
 * @author    Igor Guastalla de Lima  <[email protected]>
13
 * @copyright 2018 PHP Battlerite
14
 * @license   MIT https://github.com/guastallaigor/php-battlerite/blob/master/LICENSE
15
 *
16
 * @link      https://github.com/guastallaigor/php-battlerite
17
 */
18
class Main
19
{
20
    /**
21
     * Guzzle Client variable to send all requests.
22
     *
23
     * @var object<GuzzleHttp\Client>
24
     */
25
    private $client;
26
27
    /**
28
     * API Key of your development battlerite account.
29
     *
30
     * @var string
31
     */
32
    private $apiKey;
33
34
    /**
35
     * @var \guastallaigor\PhpBattlerite\Config
36
     */
37
    private $config;
38
39
    /**
40
     * Main constructor.
41
     *
42
     * @param \guastallaigor\PhpBattlerite\Config $config
43
     */
44 4
    public function __construct(Config $config)
45
    {
46 4
        $this->config = $config;
47 4
        $this->client = new \GuzzleHttp\Client();
48 4
    }
49
50
    /**
51
     * Method to set your API Key provided by your Battlerite development account.
52
     *
53
     * @param string $apiKey
54
     *
55
     * @return void
56
     */
57 4
    public function setAPIKey($apiKey)
58
    {
59 4
        $this->apiKey = $apiKey;
60 4
    }
61
62
    /**
63
     * Function that is going to make all the requests you need.
64
     *
65
     * @param string $method
66
     * @param string $request
67
     * @param array  $filter
68
     *
69
     * @return array
70
     */
71 4
    public function sendRequest($method, $request, $filter = [], $global = true)
72
    {
73 4
        $apiUrl = 'https://api.dc01.gamelockerapp.com/';
74 4
        $shardsGlobal = 'shards/global/';
75 4
        $url = $apiUrl.($global ? $shardsGlobal.$request : $request);
76
        $header = [
77 4
            'Authorization' => 'Bearer '.$this->apiKey,
78 4
            'Accept'        => 'application/vnd.api+json',
79
        ];
80
81
        try {
82 4
            $response = $this->client->request(
83 4
                $method,
84 4
                $url,
85
                [
86 4
                    'query'   => $filter,
87 4
                    'headers' => $header,
88
                ]
89
            );
90
91 4
            return json_decode($response->getBody()->getContents());
92
        } catch (RequestException $error) {
93
            $response = $this->statusCodeHandling($error);
94
95
            return $response;
96
        }
97
    }
98
99
    /**
100
     * Get a single player request.
101
     *
102
     * @param string $id
103
     *
104
     * @return array
105
     */
106 1
    public function getPlayer($id)
107
    {
108 1
        return $this->sendRequest('GET', 'players/'.$id);
109
    }
110
111
    /**
112
     * Get a collection of players.
113
     *
114
     * @param string $ids
115
     * @param string $type
116
     *
117
     * @return array
118
     */
119 1
    public function getPlayers($ids, $type = 'playerIds')
120
    {
121 1
        $filter = ['filter['.$type.']' => $ids];
122
123 1
        return $this->sendRequest('GET', 'players', $filter);
124
    }
125
126
    /**
127
     * Get a collection of teams.
128
     *
129
     * @param array $filters
0 ignored issues
show
Documentation introduced by
There is no parameter named $filters. Did you maybe mean $filter?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
130
     *
131
     * @return array
132
     */
133
    public function getTeams($filter)
134
    {
135
        // need work
136
        return $this->sendRequest('GET', 'teams', $filter);
137
    }
138
139
    /**
140
     * Get Battlerite status.
141
     *
142
     * @return array
143
     */
144 1
    public function getStatus()
145
    {
146 1
        return $this->sendRequest('GET', 'status', [], false);
147
    }
148
149
    /**
150
     * Get all the telemetry data.
151
     *
152
     * @param array $filters
153
     *
154
     * @return array
155
     */
156 1
    public function getMatches($filters)
157
    {
158 1
        $formattedFilter = [];
159 1
        foreach ($filters as $key => $value) {
160 1
            if ($key === 'offset' || $key === 'limit') {
161 1
                $formattedFilter['page['.$key.']'] = $value;
162 1
            } elseif ($key === 'sort') {
163 1
                $formattedFilter['sort'] = $value;
164
            } else {
165 1
                $formattedFilter['filter['.$key.']'] = $value;
166
            }
167
        }
168
169 1
        return $this->sendRequest('GET', 'matches', $formattedFilter);
170
    }
171
172
    /**
173
     * Function to handle unexpected errors.
174
     *
175
     * @param RequestException $error
176
     *
177
     * @return array
178
     */
179
    protected function statusCodeHandling($error)
180
    {
181
        return [
182
            'statuscode' => $error->getResponse()->getStatusCode(),
183
            'error'      => $error->getResponse()->getBody()->getContents(),
184
        ];
185
    }
186
}
187