Completed
Push — master ( 4666a5...b856d5 )
by Tobias
9s
created

Client::addQuery()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 2
dl 0
loc 10
ccs 5
cts 8
cp 0.625
crap 3.4746
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SolutionDrive\HipchatAPIv2Client;
4
5
use Buzz\Browser;
6
use Buzz\Client\Curl;
7
use SolutionDrive\HipchatAPIv2Client\Auth\AuthInterface;
8
use SolutionDrive\HipchatAPIv2Client\Exception\RequestException;
9
10
class Client implements ClientInterface
11
{
12
    protected $baseUrl;
13
14
    /** @var AuthInterface */
15
    protected $auth;
16
17
    /** @var Browser */
18
    protected $browser;
19
20
    /**
21
     * Client constructor
22
     *
23
     * @param AuthInterface $auth Authentication you want to use to access the api
24
     * @param Browser $browser Client you want to use, by default browser with curl will be used
25
     * @param string $baseUrl URL to the HipChat server endpoint
26
     *
27
     * @return self
28
     */
29 5
    public function __construct(AuthInterface $auth, Browser $browser = null, $baseUrl = 'https://api.hipchat.com')
30
    {
31 5
        $this->auth = $auth;
32 5
        $this->baseUrl = $baseUrl;
33 5
        if ($browser === null) {
34
            $client = new Curl();
35
            $this->browser = new Browser($client);
36
        } else {
37 5
            $this->browser = $browser;
38
        }
39 5
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 1
    public function get($resource, $query = array())
45
    {
46 1
        $url = $this->baseUrl . $resource;
47 1
        $url = $this->addQuery($query, $url);
48
49 1
        $headers = array("Authorization" => $this->auth->getCredential());
50
51
        try {
52 1
            $response = $this->browser->get($url, $headers);
53 1
        } catch (\Buzz\Exception\ClientException $e) {
54
            throw new RequestException($e->getMessage());
55
        }
56
57 1
        $this->checkBrowserResponse();
58
59 1
        return json_decode($response->getContent(), true);
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 1
    public function post($resource, $content)
66
    {
67 1
        $url = $this->baseUrl . $resource;
68
69
        $headers = array(
70 1
            'Content-Type' => 'application/json',
71 1
            'Authorization' => $this->auth->getCredential()
72 1
        );
73
74
        try {
75 1
            $response = $this->browser->post($url, $headers, json_encode($content));
76 1
        } catch (\Buzz\Exception\ClientException $e) {
77
            throw new RequestException($e->getMessage());
78
        }
79
80 1
        $this->checkBrowserResponse();
81
82 1
        return json_decode($response->getContent(), true);
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 1
    public function put($resource, $content = array())
89
    {
90 1
        $url = $this->baseUrl . $resource;
91
        $headers = array(
92 1
            'Content-Type' => 'application/json',
93 1
            'Authorization' => $this->auth->getCredential()
94 1
        );
95
96
        try {
97 1
            $response = $this->browser->put($url, $headers, json_encode($content));
98 1
        } catch (\Buzz\Exception\ClientException $e) {
99
            throw new RequestException($e->getMessage());
100
        }
101
102 1
        $this->checkBrowserResponse();
103
104 1
        return json_decode($response->getContent(), true);
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110 1
    public function delete($resource)
111
    {
112 1
        $url = $this->baseUrl . $resource;
113
114
        $headers = array(
115 1
            'Authorization' => $this->auth->getCredential()
116 1
        );
117
118
        try {
119 1
            $response = $this->browser->delete($url, $headers);
120 1
        } catch (\Buzz\Exception\ClientException $e) {
121
            throw new RequestException($e->getMessage());
122
        }
123
124 1
        $this->checkBrowserResponse();
125
126 1
        return json_decode($response->getContent(), true);
127
    }
128
129
    /**
130
     * Add query string if necessary
131
     *
132
     * @param $query
133
     * @param $url
134
     * @return string
135
     */
136 1
    private function addQuery($query, $url)
137
    {
138 1
        if (count($query) > 0) {
139
            $url .= "?";
140
        }
141
142 1
        foreach ($query as $key => $value) {
143
            $url .= "$key=$value&";
144 1
        }
145 1
        return $url;
146
    }
147
148
    /**
149
     * Checks if response was successful
150
     *
151
     * @throws RequestException
152
     */
153 4
    private function checkBrowserResponse()
154
    {
155 4
        if ($this->browser->getLastResponse()->getStatusCode() > 299) {
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Buzz\Message\MessageInterface. It seems like you code against a sub-type of Buzz\Message\MessageInterface such as Buzz\Message\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
        if ($this->browser->getLastResponse()->/** @scrutinizer ignore-call */ getStatusCode() > 299) {
Loading history...
156
            throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true));
157
        }
158 4
    }
159
}
160