Completed
Pull Request — master (#11)
by Tobias
03:37 queued 01:45
created

Client::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 18
loc 18
ccs 7
cts 9
cp 0.7778
crap 2.0438
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
        } 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 View Code Duplication
    public function post($resource, $content)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67 1
        $url = $this->baseUrl . $resource;
68
69
        $headers = array(
70 1
            'Content-Type' => 'application/json',
71 1
            'Authorization' => $this->auth->getCredential()
72
        );
73
74
        try {
75 1
            $response = $this->browser->post($url, $headers, json_encode($content));
76
        } 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 View Code Duplication
    public function put($resource, $content = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 1
        $url = $this->baseUrl . $resource;
91
        $headers = array(
92 1
            'Content-Type' => 'application/json',
93 1
            'Authorization' => $this->auth->getCredential()
94
        );
95
96
        try {
97 1
            $response = $this->browser->put($url, $headers, json_encode($content));
98
        } 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 View Code Duplication
    public function delete($resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112 1
        $url = $this->baseUrl . $resource;
113
114
        $headers = array(
115 1
            'Authorization' => $this->auth->getCredential()
116
        );
117
118
        try {
119 1
            $response = $this->browser->delete($url, $headers);
120
        } 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): string
137
    {
138 1
        if (count($query) > 0) {
139
            $url .= "?";
140
        }
141
142 1
        foreach ($query as $key => $value) {
143
            $url .= "$key=$value&";
144
        }
145 1
        return $url;
146
    }
147
148
    /**
149
     * Checks if response was successful
150
     *
151
     * @throws RequestException
152
     */
153 4
    private function checkBrowserResponse(): void
154
    {
155 4
        if ($this->browser->getLastResponse()->getStatusCode() > 299) {
156
            throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true));
157
        }
158 4
    }
159
}
160