Completed
Push — master ( a63df8...25710f )
by Akram
02:20
created

HttpClient   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 83.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 74
ccs 20
cts 24
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuth() 0 6 1
A getClient() 0 9 2
A setClient() 0 4 1
A get() 0 16 2
1
<?php
2
3
namespace Rundeck;
4
5
/**
6
 * Class HttpClient
7
 * @package Rundeck
8
 */
9
class HttpClient
10
{
11
    /**
12
     * @var \GuzzleHttp\Client
13
     */
14
    private $client;
15
    /**
16
     * @var string
17
     */
18
    private $endpoint;
19
    /**
20
     * @var string
21
     */
22
    private $authToken;
23
    /**
24
     * @var int
25
     */
26
    private $version;
27
28
    /**
29
     * @param $endpoint
30
     * @param $authToken
31
     * @param $version
32
     */
33 3
    public function setAuth($endpoint, $authToken, $version)
34
    {
35 3
        $this->endpoint = trim($endpoint, "/");
36 3
        $this->authToken = $authToken;
37 3
        $this->version = $version;
38 3
    }
39
40
    /**
41
     * @return \GuzzleHttp\Client
42
     */
43 45
    public function getClient()
44
    {
45 45
        if (isset($this->client)) {
46 45
            return $this->client;
47
        } else {
48
            $this->client = new \GuzzleHttp\Client();
49
            return $this->client;
50
        }
51
    }
52
53
    /**
54
     * @param mixed $client
55
     */
56 45
    public function setClient($client)
57
    {
58 45
        $this->client = $client;
59 45
    }
60
61
    /**
62
     * @param $uri
63
     * @param $alt
64
     * @return array
65
     */
66 45
    public function get($uri, $alt)
67
    {
68 45
        $options = ['headers'=> ['Accept' => 'application/xml']];
69
70 45
        if ($alt == "json") {
71
            $options = ['headers'=> ['Accept' => 'application/json']];
72
        }
73 45
        $uri = $this->endpoint. "/api/". $this->version .$uri."?authtoken=".$this->authToken;
74
75 45
        $response = $this->getClient()->request('GET', $uri, $options);
76 45
        $xml = simplexml_load_string($response->getBody());
77 45
        $json = json_encode($xml);
78 45
        $data = json_decode($json, true);
79
80 45
        return $data;
81
    }
82
}
83