Completed
Push — master ( f2b0ce...6a806c )
by Akram
02:02
created

HttpClient::post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
ccs 0
cts 11
cp 0
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 6
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 View Code Duplication
    public function get($uri, $alt)
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...
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
    /**
84
     * @param $uri
85
     * @param $alt
86
     * @return array
87
     */
88 View Code Duplication
    public function post($uri, $alt)
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
        $options = ['headers'=> ['Accept' => 'application/xml']];
91
92
        if ($alt == "json") {
93
            $options = ['headers'=> ['Accept' => 'application/json']];
94
        }
95
        $uri = $this->endpoint. "/api/". $this->version .$uri."?authtoken=".$this->authToken;
96
97
        $response = $this->getClient()->request('POST', $uri, $options);
98
        $xml = simplexml_load_string($response->getBody());
99
        $json = json_encode($xml);
100
        $data = json_decode($json, true);
101
102
        return $data;
103
    }
104
}
105