Completed
Push — master ( 1dbbcf...1dbbcf )
by Igor
04:37 queued 02:52
created

Main::getPlayer()   A

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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
  * @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
     * 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 3
    public function __construct(Config $config)
45
    {
46 3
        $this->config = $config;
47 3
        $this->client = new \GuzzleHttp\Client();
48 3
    }
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 3
    public function setAPIKey($apiKey)
58
    {
59 3
        $this->apiKey = $apiKey;
60 3
    }
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 3
    public function sendRequest($method, $request, $filter = [], $global = true)
72
    {
73 3
        $apiUrl = 'https://api.dc01.gamelockerapp.com/';
74 3
        $shardsGlobal = 'shards/global/';
75 3
        $url =  $apiUrl . ($global ?  $shardsGlobal . $request : $request);
76
        $header = [
77 3
            'Authorization' => 'Bearer ' . $this->apiKey,
78 3
            'Accept' =>  'application/vnd.api+json'
79
        ];
80
81
        try {
82 3
            $response = $this->client->request(
83 3
                $method,
84 3
                $url,
85
                [
86 3
                    'query' => $filter,
87 3
                    'headers' => $header,
88
                ]
89
            );
90
91 3
            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 1
        return $this->sendRequest('GET', 'players', $filter);
123
    }
124
125
    /**
126
     * Get a collection of teams.
127
     *
128
     * @param array $filter
129
     *
130
     * @return array
131
     */
132
    public function getTeams($filter)
133
    {
134
        return $this->sendRequest('GET', 'teams', $filter);
135
    }
136
137
    /**
138
     * Get Battlerite status.
139
     *
140
     * @return array
141
     */
142 1
    public function getStatus()
143
    {
144 1
        return $this->sendRequest('GET', 'status', [], false);
145
    }
146
147
    public function getTelemetry()
148
    {
149
        $response = $this->sendRequest('GET', 'matches');
150
        // need work
151
152
        return $response;
153
    }
154
155
    /**
156
     * Function to handle unexpected errors.
157
     *
158
     * @param RequestException $error
159
     *
160
     * @return void
161
     */
162
    protected function statusCodeHandling($error)
163
    {
164
        $response = [
165
            'statuscode' => $error->getResponse()->getStatusCode(),
166
            'error' => $error->getResponse()->getBody()->getContents(),
167
        ];
168
169
        return $response;
170
    }
171
}
172