Completed
Push — master ( 980fdb...513d47 )
by Matthias
11s
created

Client::put()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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