Completed
Push — master ( b6560c...9b1dc4 )
by Tomasz
02:38
created

ConsulCurl   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 14.29%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 43
ccs 4
cts 28
cp 0.1429
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A performGetRequest() 0 9 1
A performPutRequest() 0 20 2
1
<?php
2
3
/**
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Gendoria\CruftFlake\Config;
10
11
/**
12
 * Description of ConsulCurlRequestor
13
 *
14
 * @author Tomasz Struczyński <[email protected]>
15
 */
16
class ConsulCurl
17
{
18
    private $consulBaseUrl;
19
    
20
    private $apiPrefix = "/v1";
21
    
22 7
    function __construct($consulBaseUrl, $apiPrefix = "/v1")
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24 7
        $this->consulBaseUrl = $consulBaseUrl;
25 7
        $this->apiPrefix = $apiPrefix;
26 7
    }
27
    
28
    public function performGetRequest($url)
29
    {
30
        $curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url;
31
        $ch = curl_init($curlUrl);
32
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
33
        $result = curl_exec($ch);
34
        $returnData = json_decode($result, true);
35
        return $returnData;
36
    }
37
    
38
    public function performPutRequest($url, $payload = null)
39
    {
40
        $curlUrl = $this->consulBaseUrl . $this->apiPrefix . $url;
41
        $ch = curl_init($curlUrl);
42
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
43
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
44
        curl_setopt($ch, CURLOPT_TIMEOUT, 6);
45
        if ($payload !== null) {
46
            curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
47
        }
48
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
49
        curl_setopt($ch, CURLOPT_HTTPHEADER,
50
            array(
51
            'Content-Type: application/json',
52
            'Content-Length: ' . strlen($payload))
53
        );
54
        $result = curl_exec($ch);
55
        $returnData = json_decode($result, true);
56
        return $returnData;
57
    }
58
}