HttpClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 57.14%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 2
dl 32
loc 96
ccs 20
cts 35
cp 0.5714
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuth() 0 6 1
A getClient() 0 9 2
A setClient() 0 4 1
A get() 16 16 2
A post() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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