Curl   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 106
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 6 2
A setOptions() 0 6 2
A setOption() 0 4 1
A getOption() 0 8 2
A hasOption() 0 4 1
A removeOption() 0 6 2
A exec() 0 10 1
1
<?php
2
3
namespace VladimirYuldashev\Flysystem;
4
5
class Curl
6
{
7
    /**
8
     * @var resource
9
     */
10
    protected $curl;
11
12
    /**
13
     * @var array
14
     */
15
    protected $options;
16
17
    /**
18
     * @param array $options Array of the Curl options, where key is a CURLOPT_* constant
19
     */
20
    public function __construct($options = [])
21
    {
22
        $this->curl = curl_init();
23
        $this->options = $options;
24
    }
25
26
    public function __destruct()
27
    {
28
        if (is_resource($this->curl)) {
29
            curl_close($this->curl);
30
        }
31
    }
32
33
    /**
34
     * Set the Curl options.
35
     *
36
     * @param array $options Array of the Curl options, where key is a CURLOPT_* constant
37
     */
38
    public function setOptions($options)
39
    {
40
        foreach ($options as $key => $value) {
41
            $this->setOption($key, $value);
42
        }
43
    }
44
45
    /**
46
     * Set the Curl option.
47
     *
48
     * @param int $key One of the CURLOPT_* constant
49
     * @param mixed $value The value of the CURL option
50
     */
51
    public function setOption($key, $value)
52
    {
53
        $this->options[$key] = $value;
54
    }
55
56
    /**
57
     * Returns the value of the option.
58
     *
59
     * @param  int $key One of the CURLOPT_* constant
60
     * @return mixed|null The value of the option set, or NULL, if it does not exist
61
     */
62
    public function getOption($key)
63
    {
64
        if (!$this->hasOption($key)) {
65
            return null;
66
        }
67
68
        return $this->options[$key];
69
    }
70
71
    /**
72
     * Checking if the option is set.
73
     *
74
     * @param  int $key One of the CURLOPT_* constant
75
     * @return bool
76
     */
77
    public function hasOption($key)
78
    {
79
        return array_key_exists($this->options, $key);
80
    }
81
82
    /**
83
     * Remove the option.
84
     *
85
     * @param  int $key One of the CURLOPT_* constant
86
     */
87
    public function removeOption($key)
88
    {
89
        if ($this->hasOption($key)) {
90
            unset($this->options[$key]);
91
        }
92
    }
93
94
    /**
95
     * Calls curl_exec and returns its result.
96
     *
97
     * @param  array $options Array where key is a CURLOPT_* constant
98
     * @return mixed Results of curl_exec
99
     */
100
    public function exec($options = [])
101
    {
102
        $options = array_replace($this->options, $options);
103
104
        curl_setopt_array($this->curl, $options);
105
        $result = curl_exec($this->curl);
106
        curl_reset($this->curl);
107
108
        return $result;
109
    }
110
}
111