Api   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 1
cbo 0
dl 0
loc 89
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDomain() 0 4 1
A get() 0 10 1
B put() 0 24 1
A exec() 0 9 1
1
<?php
2
3
namespace Cauditor;
4
5
/**
6
 * @author Matthias Mullie <[email protected]>
7
 * @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved.
8
 * @license LICENSE MIT
9
 */
10
class Api
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $domain;
16
17
    /**
18
     * @param string $domain
19
     */
20
    public function __construct($domain)
21
    {
22
        $this->domain = rtrim($domain, '/');
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getDomain()
29
    {
30
        return $this->domain;
31
    }
32
33
    /**
34
     * Read data from cauditor API.
35
     *
36
     * @param string $uri
37
     *
38
     * @return string|bool API response (on success) or false (on failure)
39
     */
40
    public function get($uri)
41
    {
42
        $options = array(
43
            CURLOPT_URL => $this->domain.'/'.ltrim($uri, '/'),
44
            CURLOPT_FOLLOWLOCATION => 1,
45
            CURLOPT_RETURNTRANSFER => 1,
46
        );
47
48
        return $this->exec($options);
49
    }
50
51
    /**
52
     * Submit the data to cauditor API.
53
     *
54
     * @param string $uri
55
     * @param array  $data
56
     *
57
     * @return string|bool API response (on success) or false (on failure)
58
     */
59
    public function put($uri, array $data)
60
    {
61
        // PUT requests need an fopen wrapper, so we'll create a temporary one
62
        // for the data to submit...
63
        $json = json_encode($data);
64
        $file = fopen('php://temp', 'w+');
65
        fwrite($file, $json, strlen($json));
66
        fseek($file, 0);
67
68
        $options = array(
69
            CURLOPT_URL => $this->domain.'/'.ltrim($uri, '/'),
70
            CURLOPT_FOLLOWLOCATION => 1,
71
            CURLOPT_RETURNTRANSFER => 1,
72
            CURLOPT_PUT => 1,
73
            CURLOPT_INFILE => $file,
74
            CURLOPT_INFILESIZE => strlen($json),
75
        );
76
77
        $result = $this->exec($options);
78
79
        fclose($file);
80
81
        return $result;
82
    }
83
84
    /**
85
     * @param array $options array of CURLOPT_* options
86
     *
87
     * @return string|bool API response (on success) or false (on failure)
88
     */
89
    protected function exec($options)
90
    {
91
        $curl = curl_init();
92
        curl_setopt_array($curl, $options);
93
        $result = curl_exec($curl);
94
        curl_close($curl);
95
96
        return $result;
97
    }
98
}
99