Completed
Push — master ( b1d0ff...f51f35 )
by Toni
04:43
created

Client::getClan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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