ConsulCurl::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 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
    public function __construct($consulBaseUrl, $apiPrefix = "/v1")
23
    {
24
        $this->consulBaseUrl = $consulBaseUrl;
25
        $this->apiPrefix = $apiPrefix;
26
    }
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
}