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

ConsulCurl::performGetRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
cc 1
eloc 7
nc 1
nop 1
crap 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
}