Completed
Push — master ( 7cb621...ebe87d )
by dotzero
11s
created

CurlHandle   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 54
ccs 18
cts 21
cp 0.8571
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 6 2
A open() 0 13 3
A close() 0 12 2
1
<?php
2
3
namespace AmoCRM\Request;
4
5
use AmoCRM\NetworkException;
6
7
/**
8
 * Class CurlHandle
9
 *
10
 * Класс, хранящий повторно используемый обработчик cURL
11
 *
12
 * @package AmoCRM\Request
13
 * @author dotzero <[email protected]>
14
 * @link http://www.dotzero.ru/
15
 * @link https://github.com/dotzero/amocrm-php
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 */
20
class CurlHandle
21
{
22
    /**
23
     * @var resource Повторно используемый обработчик cURL
24
     */
25
    private $handle;
26
27
    /**
28
     * CurlHandle destructor.
29
     */
30 13
    public function __destruct()
31
    {
32 13
        if ($this->handle !== null) {
33
            @curl_close($this->handle);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
34
        }
35 13
    }
36
37
    /**
38
     * Open cURL handle.
39
     *
40
     * @throws NetworkException
41
     *
42
     * @return resource
43
     */
44 3
    public function open()
45
    {
46 3
        if ($this->handle !== null) {
47 1
            return $this->handle;
48
        }
49
50 3
        if (!function_exists('curl_init')) {
51
            throw new NetworkException('The cURL PHP extension was not loaded.');
52
        }
53 3
        $this->handle = curl_init();
54
55 3
        return $this->handle;
56
    }
57
58
    /**
59
     * Close cURL handle.
60
     */
61 3
    public function close()
62
    {
63 3
        if ($this->handle === null) {
64 1
            return;
65
        }
66
67 2
        curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null);
68 2
        curl_setopt($this->handle, CURLOPT_READFUNCTION, null);
69 2
        curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null);
70 2
        curl_setopt($this->handle, CURLOPT_PROGRESSFUNCTION, null);
71 2
        curl_reset($this->handle);
72 2
    }
73
}
74