Client::request()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Lever\Api;
4
5
use GuzzleHttp\Client as HttpClient;
6
7
class Client
8
{
9
    /**
10
     * @var HttpClient
11
     */
12
    private $httpClient;
13
14
    /**
15
     * @var string
16
     */
17
    private $endpoint = 'https://api.lever.co/v1';
18
19
    /**
20
     * @var string
21
     */
22
    private $authToken;
23
24
    /**
25
     * @param array $options
26
     */
27 5
    public function __construct(array $options = [])
28
    {
29 5
        if (isset($options['endpoint'])) {
30 1
            $this->endpoint = trim($options['endpoint'], '/');
31 1
        }
32
33 5
        if (isset($options['authToken'])) {
34 5
            $this->authToken = $options['authToken'];
35 5
        }
36
37 5
        if (isset($options['httpClient'])) {
38 5
            $this->httpClient = $options['httpClient'];
39 5
        } else {
40 1
            $this->httpClient = new HttpClient();
41
        }
42 5
    }
43
44
    /**
45
     * @return string
46
     */
47 2
    public function getEndpoint()
48
    {
49 2
        return $this->endpoint;
50
    }
51
52
    /**
53
     * @param string $path
54
     * @param array  $params
55
     *
56
     * @return array
57
     */
58 1
    public function get($path, array $params = [])
59
    {
60 1
        return $this->request('GET', $path, ['query' => $params]);
61
    }
62
63
    /**
64
     * @param string $path
65
     * @param array  $postData
66
     *
67
     * @return array
68
     */
69 1
    public function post($path, array $postData)
70
    {
71 1
        return $this->request('POST', $path, ['form_params' => $postData]);
72
    }
73
74
    /**
75
     * @param string $path
76
     * @param array  $putData
77
     *
78
     * @return array
79
     */
80 1
    public function put($path, array $putData)
81
    {
82 1
        return $this->request('PUT', $path, ['json' => $putData]);
83
    }
84
85
    /**
86
     * @param string $method
87
     * @param string $path
88
     * @param array  $options
89
     *
90
     * @return array
91
     */
92 3
    private function request($method, $path, $options)
93
    {
94 3
        $uri = $this->endpoint . $path;
95
96 3
        $authOption = ['auth' => [$this->authToken, '']];
97 3
        $options = array_merge($options, $authOption);
98
99 3
        $response = $this->httpClient->request($method, $uri, $options);
100
101 3
        return json_decode($response->getBody(), true);
102
    }
103
}
104