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

CurlHandle::open()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3.0261
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