Completed
Push — master ( 3aaf31...f42553 )
by Lawrence
01:34
created

Curl   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 93
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B post() 0 44 5
A setOptions() 0 12 1
A __construct() 0 4 1
1
<?php
2
/*
3
 +------------------------------------------------------------------------+
4
 | Plinker-RPC PHP                                                        |
5
 +------------------------------------------------------------------------+
6
 | Copyright (c)2017-2018 (https://github.com/plinker-rpc/core)           |
7
 +------------------------------------------------------------------------+
8
 | This source file is subject to MIT License                             |
9
 | that is bundled with this package in the file LICENSE.                 |
10
 |                                                                        |
11
 | If you did not receive a copy of the license and are unable to         |
12
 | obtain it through the world-wide-web, please send an email             |
13
 | to [email protected] so we can send you a copy immediately.        |
14
 +------------------------------------------------------------------------+
15
 | Authors: Lawrence Cherone <[email protected]>                     |
16
 +------------------------------------------------------------------------+
17
 */
18
19
namespace Plinker\Core\Lib;
20
21
/**
22
 * Plinker\Core\Lib\Curl
23
 */
24
final class Curl
25
{
26
    /**
27
     * @var
28
     */
29
    private $config;
30
31
    /**
32
     * @var
33
     */
34
    private $options;
35
36
    /**
37
     * Class construct
38
     *
39
     * @param  array  $config  - config array which holds object configuration
40
     * @return void
41
     */
42
    public function __construct(array $config = [])
43
    {
44
        //
45
        $this->config = $config;
46
    }
47
48
    /**
49
     *
50
     */
51
    final private function setOptions()
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
52
    {
53
        //
54
        $this->options = [
55
            CURLOPT_FAILONERROR    => true,
56
            CURLOPT_FOLLOWLOCATION => true,
57
            CURLOPT_RETURNTRANSFER => true,
58
            CURLOPT_SSL_VERIFYPEER => false,
59
            CURLOPT_SSL_VERIFYHOST => false,
60
            CURLOPT_ENCODING       => "gzip",
61
            CURLOPT_HTTPHEADER     => [
62
                "Content-Type: application/json"
63
            ]
64
        ];
65
    }
66
67
    /**
68
     *  POST
69
     *
70
     * @param string $url        - url of the plinker server
71
     * @param array  $parameters - post parameters
72
     */
73
    public function post($url, $parameters = [], $headers = [])
74
    {
75
        //
76
        $this->setOptions();
77
78
        //
79
        if (is_array($parameters)) {
0 ignored issues
show
introduced by
The condition is_array($parameters) can never be false.
Loading history...
80
            $parameters = json_encode($parameters);
81
            $parameters = gzdeflate($parameters, 9);
82
        }
83
84
        //
85
        $curl = curl_init($url);
86
87
        //
88
        $this->options[CURLOPT_POST] = true;
89
        $this->options[CURLOPT_POSTFIELDS] = $parameters;
90
91
        //
92
        if (!empty($headers)) {
93
            foreach ($headers as $header) {
94
                $this->options[CURLOPT_HTTPHEADER][] = $header;
95
            }
96
        }
97
98
        //
99
        curl_setopt_array($curl, $this->options);
100
101
        //
102
        $body = curl_exec($curl);
103
104
        if (curl_error($curl)) {
105
            return serialize([
106
                "url"   => $url,
107
                "error" => curl_error($curl),
108
                "code"  => curl_getinfo($curl, CURLINFO_HTTP_CODE)
109
            ]);
110
        }
111
112
        //
113
        curl_close($curl);
114
115
        //
116
        return $body;
117
    }
118
}
119