Completed
Push — master ( 6de68d...83f122 )
by Vladimir
02:45 queued 49s
created

Curl   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

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 7 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
		return $this->options[$key];
68
	}
69
70
	/**
71
	 * Checking if the option is set
72
	 * 
73
	 * @param  int $key One of the CURLOPT_* constant
74
	 * @return boolean     
75
	 */
76
	public function hasOption($key)
77
	{
78
		return array_key_exists($this->options, $key);
79
	}
80
81
	/**
82
	 * Remove the option 
83
	 * 
84
	 * @param  int $key One of the CURLOPT_* constant
85
	 */
86
	public function removeOption($key)
87
	{
88
		if ($this->hasOption($key)) {
89
			unset($this->options[$key]);
90
		}
91
	}
92
93
	/**
94
	 * Calls curl_exec and returns its result
95
	 *
96
	 * @param  array $options Array where key is a CURLOPT_* constant
97
	 * @return mixed Results of curl_exec
98
	 */
99
	public function exec($options = [])
100
	{
101
		$options = array_replace($this->options, $options);
102
103
		curl_setopt_array($this->curl, $options);
104
		$result = curl_exec($this->curl);
105
		curl_reset($this->curl);
106
107
		return $result;
108
	}
109
}