Completed
Push — master ( 24a13c...980fdb )
by Tobias
12s
created

Client::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

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