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

ConsulCurl::performPutRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 9.4285
cc 2
eloc 16
nc 2
nop 2
crap 6
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
}