Completed
Push — increase-coverage ( 9b611d...2a6b33 )
by Tobias
01:58
created

Client::setBaseUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
        if (count($query) > 0) {
48
            $url .= "?";
49
        }
50
51 1
        foreach ($query as $key => $value) {
52
            $url .= "$key=$value&";
53 1
        }
54
55 1
        $headers = array("Authorization" => $this->auth->getCredential());
56
57
        try {
58 1
            $response = $this->browser->get($url, $headers);
59 1
        } catch (\Buzz\Exception\ClientException $e) {
60
            throw new RequestException($e->getMessage());
61
        }
62
63 1
        if ($this->browser->getLastResponse()->getStatusCode() > 299) {
64
            throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true));
65
        }
66
67 1
        return json_decode($response->getContent(), true);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 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...
74
    {
75 1
        $url = $this->baseUrl . $resource;
76
77
        $headers = array(
78 1
            'Content-Type' => 'application/json',
79 1
            'Authorization' => $this->auth->getCredential()
80 1
        );
81
82
        try {
83 1
            $response = $this->browser->post($url, $headers, json_encode($content));
84 1
        } catch (\Buzz\Exception\ClientException $e) {
85
            throw new RequestException($e->getMessage());
86
        }
87
88 1
        if ($this->browser->getLastResponse()->getStatusCode() > 299) {
89
            throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true));
90
        }
91
92 1
        return json_decode($response->getContent(), true);
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 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...
99
    {
100 1
        $url = $this->baseUrl . $resource;
101
        $headers = array(
102 1
            'Content-Type' => 'application/json',
103 1
            'Authorization' => $this->auth->getCredential()
104 1
        );
105
106
        try {
107 1
            $response = $this->browser->put($url, $headers, json_encode($content));
108 1
        } catch (\Buzz\Exception\ClientException $e) {
109
            throw new RequestException($e->getMessage());
110
        }
111
112 1
        if ($this->browser->getLastResponse()->getStatusCode() > 299) {
113
            throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true));
114
        }
115
116 1
        return json_decode($response->getContent(), true);
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 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...
123
    {
124 1
        $url = $this->baseUrl . $resource;
125
126
        $headers = array(
127 1
            'Authorization' => $this->auth->getCredential()
128 1
        );
129
130
        try {
131 1
            $response = $this->browser->delete($url, $headers);
132 1
        } catch (\Buzz\Exception\ClientException $e) {
133
            throw new RequestException($e->getMessage());
134
        }
135
136 1
        if ($this->browser->getLastResponse()->getStatusCode() > 299) {
137
            throw new RequestException(json_decode($this->browser->getLastResponse()->getContent(), true));
138
        }
139
140 1
        return json_decode($response->getContent(), true);
141
    }
142
}
143