Completed
Push — master ( 5fc11a...9922d2 )
by Igor
01:45
created

Main::sendRequest()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0393

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
ccs 11
cts 14
cp 0.7856
rs 8.8571
cc 2
eloc 16
nc 3
nop 3
crap 2.0393
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
  * @package   src
12
  * Main class
13
  * @author    Igor Guastalla de Lima  <[email protected]>
14
  * @copyright 2018 PHP Battlerite
15
  * @license   MIT https://github.com/guastallaigor/php-battlerite/blob/master/LICENSE
16
  * @link      https://github.com/guastallaigor/php-battlerite
17
  */
18
class Main
19
{
20
    /**
21
     * API URL root of Battlerite.
22
     *
23
     * @var string
24
     */
25
    private static $apiUrl = "https://api.dc01.gamelockerapp.com/shards/global/";
26
27
    /**
28
     * Guzzle Client variable to send all requests.
29
     *
30
     * @var object<GuzzleHttp\Client>
31
     */
32
    private $client;
33
34
    /**
35
     * API Key of your development battlerite account.
36
     *
37
     * @var string
38
     */
39
    private $apiKey;
40
41
    /**
42
     * @var  \guastallaigor\PhpBattlerite\Config
43
     */
44
    private $config;
45
46
    /**
47
     * Main constructor.
48
     *
49
     * @param \guastallaigor\PhpBattlerite\Config $config
50
     */
51 2
    public function __construct(Config $config)
52
    {
53 2
        $this->config = $config;
54 2
        $this->client = new \GuzzleHttp\Client();
55 2
    }
56
57
    /**
58
     * Method to set your API Key provided by your Battlerite development account.
59
     *
60
     * @param string $apiKey
61
     *
62
     * @return void
63
     */
64 2
    public function setAPIKey($apiKey)
65
    {
66 2
        $this->apiKey = $apiKey;
67 2
    }
68
69
    /**
70
     * Function that is going to make all the requests you need.
71
     *
72
     * @param string $method
73
     * @param string $request
74
     * @param array $filter
75
     *
76
     * @return array
77
     */
78 2
    public function sendRequest($method, $request, $filter = [])
79
    {
80 2
        $url = self::$apiUrl . $request;
81
        $header = [
82 2
            "Authorization" => "Bearer " . $this->apiKey,
83 2
            "Accept" =>  "application/vnd.api+json"
84
        ];
85
86
        try {
87 2
            $response = $this->client->request(
88 2
                $method,
89 2
                $url,
90
                [
91 2
                    "query" => $filter,
92 2
                    "connect_timeout" => 10,
93 2
                    "headers" => $header,
94
                ]
95
            );
96
97 2
            return json_decode($response->getBody()->getContents());
98
        } catch (RequestException $error) {
99
            $response = $this->statusCodeHandling($error);
100
            return $response;
101
        }
102
    }
103
104
    /**
105
     * Get a single player request.
106
     *
107
     * @param string $id
108
     *
109
     * @return array
110
     */
111 1
    public function getPlayer($id)
112
    {
113 1
        return $this->sendRequest('GET', 'players/' . $id);
114
    }
115
116
    /**
117
     * Get a collection of players.
118
     *
119
     * @param string $ids
120
     * @param string $type
121
     *
122
     * @return array
123
     */
124 1
    public function getPlayers($ids, $type = 'playerIds')
125
    {
126 1
        $filter = ['filter['. $type .']' => $ids];
127 1
        return $this->sendRequest('GET', 'players', $filter);
128
    }
129
130
    /**
131
     * Get a collection of teams.
132
     *
133
     * @param array $filter
134
     *
135
     * @return array
136
     */
137
    public function getTeams($filter = [])
138
    {
139
        return $this->sendRequest('GET', 'teams', $filter);
140
    }
141
142
    /**
143
     * Get Battlerite status.
144
     *
145
     * @return array
146
     */
147
    public function getStatus()
148
    {
149
        return $this->sendRequest('GET', 'status');
150
    }
151
152
    public function getTelemetry()
153
    {
154
        $response = $this->sendRequest('GET', 'matches');
155
        // need work
156
157
        return $response;
158
    }
159
160
    /**
161
     * Function to handle unexpected errors.
162
     *
163
     * @param RequestException $error
164
     *
165
     * @return void
166
     */
167
    protected function statusCodeHandling($error)
168
    {
169
        $response = [
170
            "statuscode" => $error->getResponse()->getStatusCode(),
171
            "error" => $error->getResponse()->getBody()->getContents(),
172
        ];
173
174
        return $response;
175
    }
176
}
177