WakatimeClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 63
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setApiKey() 0 4 1
A setBaseUrl() 0 4 1
A getApiKey() 0 4 1
A getBaseUrl() 0 4 1
A performRequest() 0 7 1
A buildHeaders() 0 7 1
A getBase64ApiKey() 0 4 1
1
<?php
2
3
namespace Kallencode\Wakatime;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Collection;
7
8
class WakatimeClient
9
{
10
11
    protected $baseUrl;
12
13
    protected $client;
14
15
    protected $apiKey;
16
17
    public function __construct(Client $client, $baseUrl, $apiKey)
18
    {
19
        $this->client = $client;
20
21
        $this->baseUrl = $baseUrl;
22
23
        $this->apiKey = $apiKey;
24
    }
25
26
    public function setApiKey(string $apiKey)
27
    {
28
        $this->apiKey = $apiKey;
29
    }
30
31
    public function setBaseUrl(string $baseUrl)
32
    {
33
        $this->baseUrl = $baseUrl;
34
    }
35
36
    public function getApiKey() : string
37
    {
38
        return $this->apiKey;
39
    }
40
41
42
    public function getBaseUrl() : string
43
    {
44
        return $this->baseUrl;
45
    }
46
47
48
    public function performRequest(string $resource, array $headers = [], array $query = []) : Collection
49
    {
50
        return collect(json_decode($this->client->request('GET', "{$this->baseUrl}{$resource}", [
51
            'headers' => $this->buildHeaders($headers),
52
            'query' => $query
53
        ])->getBody()->getContents(), true));
54
    }
55
56
    protected function buildHeaders(array $headers = []) : array
57
    {
58
        return array_merge([
59
            'Authorization' => 'Basic ' . $this->getBase64ApiKey(),
60
            'Accept' => 'application/json'
61
            ], $headers);
62
    }
63
64
    private function getBase64ApiKey() : string
65
    {
66
        return base64_encode($this->apiKey);
67
    }
68
69
70
}
71