Client::request()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace ClashOfClans;
4
5
use ClashOfClans\Api\Clan\Clan;
6
use ClashOfClans\Api\Clan\Player;
7
use ClashOfClans\Api\WarLog\WarLog;
8
use ClashOfClans\Api\League\League;
9
use ClashOfClans\Api\Location\Location;
10
use ClashOfClans\Api\Location\LocationList;
11
use ClashOfClans\Api\ResponseMediator;
12
use ClashOfClans\Api\Player\Player as FullPlayer;
13
use GuzzleHttp\Client as GuzzleClient;
14
use GuzzleHttp\ClientInterface;
15
16
class Client
17
{
18
19
    protected $httpClient;
20
21
    protected $token;
22
23
    public function __construct($token)
24
    {
25
        $this->token = $token;
26
    }
27
28
    /**
29
     * Get full details for specific clan
30
     *
31
     * @param string $tag
32
     * @return Clan
33
     */
34
    public function getClan($tag)
35
    {
36
        $response = $this->request('clans/' . urlencode($tag));
37
38
        return Clan::makeFromArray($response);
39
    }
40
41
    /**
42
     * Search for clans using parameters
43
     * @see Documentation at https://developer.clashofclans.com/
44
     *
45
     * @param $params
46
     * @return array
47
     */
48
    public function getClans($params)
49
    {
50
        $params = is_array($params) ? $params : ['name' => $params];
51
52
        $response = $this->request('clans?' . http_build_query($params));
53
54
        return array_map(function ($item) {
55
            return Clan::makeFromArray($item);
56
        }, $response['items']);
57
    }
58
    
59
    /**
60
     * Get warlog for specific clan
61
     * 
62
     * @param string $tag
63
     * @return WarLog
64
     */
65
    public function getClanWarLog($tag)
66
    {
67
        $response = $this->request('clans/' . urlencode($tag).'/warlog');
68
        
69
        return array_map(function ($item) {
70
            return WarLog::makeFromArray($item);
71
        }, $response['items']);
72
73
    }
74
    
75
    /**
76
     * Get player info for specific tag
77
     * 
78
     * @param string $tag
79
     * @return PlayerInfo
80
     */
81
    public function getPlayer($tag)
82
    {
83
        $response = $this->request('players/' . urlencode($tag));
84
        
85
        return FullPlayer::makeFromArray($response);
86
    }
87
88
    /**
89
     * Get details for specific location
90
     * @param $id
91
     * @return Location
92
     */
93
    public function getLocation($id)
94
    {
95
        return Location::makeFromArray($this->request('locations/' . urlencode($id)));
96
    }
97
98
    /**
99
     * Get list of all locations
100
     *
101
     * @return array
102
     */
103
    public function getLocations()
104
    {
105
        return array_map(function ($item) {
106
            return Location::makeFromArray($item);
107
        }, $this->request('locations')['items']);
108
    }
109
110
    /**
111
     * Get rankings for specific location
112
     * @param $locationId
113
     * @param $rankingId
114
     * @return array
115
     */
116
    public function getRankingsForLocation($locationId, $rankingId)
117
    {
118
        $url = 'locations/' . $locationId . '/rankings/' . $rankingId;
119
120
        if ($rankingId == 'clans') {
121
            return array_map(function ($item) {
122
                return Clan::makeFromArray($item);
123
            }, $this->request($url)['items']);
124
        }
125
126
        return array_map(function ($item) {
127
            return Player::makeFromArray($item);
128
        }, $this->request($url)['items']);
129
    }
130
131
    /**
132
     * Get all available leagues
133
     *
134
     * @return array
135
     */
136
    public function getLeagues()
137
    {
138
        return array_map(function ($item) {
139
            return League::makeFromArray($item);
140
        }, $this->request('leagues')['items']);
141
    }
142
143
    /**
144
     * @param $url
145
     * @return array
146
     */
147
    protected function request($url)
148
    {
149
        $response = $this->getHttpClient()
150
                         ->request('GET', $url, ['headers' => ['authorization' => 'Bearer ' . $this->getToken()]]);
151
152
        return ResponseMediator::convertResponseToArray($response);
153
    }
154
155
    /**
156
     * @return GuzzleClient
157
     */
158
    public function getHttpClient()
159
    {
160
        if ($this->httpClient === null) {
161
            $this->httpClient = new GuzzleClient(['base_uri' => 'https://api.clashofclans.com/v1/']);
162
        }
163
164
        return $this->httpClient;
165
    }
166
167
    public function setHttpClient(ClientInterface $client)
168
    {
169
        $this->httpClient = $client;
170
171
        return $this;
172
    }
173
174
    /**
175
     * @return string|null
176
     */
177
    public function getToken()
178
    {
179
        return $this->token;
180
    }
181
182
}
183