CurlHandle::__destruct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 5
cp 0.6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.2559
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
     * Закрывает обработчик cURL
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
     * Возвращает повторно используемый обработчик cURL или создает новый
39
     *
40
     * @return resource
41
     * @throws NetworkException
42
     */
43 3
    public function open()
44
    {
45 3
        if ($this->handle !== null) {
46 1
            return $this->handle;
47
        }
48
49 3
        if (!function_exists('curl_init')) {
50
            throw new NetworkException('The cURL PHP extension was not loaded.');
51
        }
52 3
        $this->handle = curl_init();
53
54 3
        return $this->handle;
55
    }
56
57
    /**
58
     * Сбрасывает настройки обработчика cURL
59
     */
60 3
    public function close()
61
    {
62 3
        if ($this->handle === null) {
63 1
            return;
64
        }
65
66 2
        curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null);
67 2
        curl_setopt($this->handle, CURLOPT_READFUNCTION, null);
68 2
        curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null);
69 2
        curl_setopt($this->handle, CURLOPT_PROGRESSFUNCTION, null);
70 2
        curl_reset($this->handle);
71 2
    }
72
}
73