Completed
Push — develop ( 09c691...a3cf7f )
by Stéphane
26:40 queued 11:07
created

CurlHandle   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.56%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 13
c 5
b 1
f 0
lcom 1
cbo 2
dl 0
loc 105
ccs 40
cts 41
cp 0.9756
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setDefaults() 0 9 1
A __destruct() 0 4 1
A open() 0 7 2
A close() 0 8 2
A execute() 0 19 3
A reset() 0 10 3
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport\Handle
10
 */
11
12
namespace Bee4\Transport\Handle;
13
14
use Bee4\Transport\Exception\CurlException;
15
16
/**
17
 * Define cURL handle wrapper
18
 * @package Bee4\Transport\Handle
19
 */
20
class CurlHandle extends AbstractHandle
21
{
22
    /**
23
     * cURL resource handle
24
     * @var resource
25
     */
26
    protected $handle;
27
28
    /**
29
     * Initialize cURL resource
30
     */
31 9
    public function __construct()
32
    {
33 9
        $this->setDefaults();
34 9
        $this->open();
35 9
    }
36
37
    /**
38
     * Set default CURL options
39
     */
40 14
    private function setDefaults()
41
    {
42 14
        $this->options = [
43 14
            CURLOPT_RETURNTRANSFER => true,
44 14
            CURLOPT_FOLLOWLOCATION => true,
45 14
            CURLOPT_HEADER => true,
46 14
            CURLINFO_HEADER_OUT => true
47 14
        ];
48 14
    }
49
50
    /**
51
     * Handle destructor
52
     * @codeCoverageIgnore
53
     */
54
    public function __destruct()
55
    {
56
        $this->close();
57
    }
58
59
    /**
60
     * Open the curl handle to be used
61
     * @return Handle
62
     */
63 9
    public function open()
64
    {
65 9
        if (!is_resource($this->handle)) {
66 9
            $this->handle = curl_init();
67 9
        }
68 9
        return $this;
69
    }
70
71
    /**
72
     * Close currently opened handle
73
     * @return Handle
74
     */
75 2
    public function close()
76
    {
77 2
        if (is_resource($this->handle)) {
78 2
            curl_close($this->handle);
79 2
        }
80 2
        $this->handle = null;
81 2
        return $this;
82
    }
83
84
    /**
85
     * Execute current handle and return result
86
     * @throws \RuntimeException
87
     * @throws CurlException
88
     * @return string
89
     */
90 13
    public function execute()
91
    {
92 13
        if (!is_resource($this->handle)) {
93 1
            throw new \RuntimeException('Curl handle has been closed, just open it before execute...');
94
        }
95
96 12
        curl_setopt_array($this->handle, $this->options);
97
98 12
        $return = curl_exec($this->handle);
99 12
        $this->infos = curl_getinfo($this->handle);
100 12
        if ($return === false) {
101 2
            throw new CurlException(
102 2
                curl_error($this->handle),
103 2
                curl_errno($this->handle)
104 2
            );
105
        }
106
107 10
        return $return;
108
    }
109
110
    /**
111
     * Check PHP version and reset handle option if possible
112
     * @return boolean
113
     */
114 5
    public function reset()
115
    {
116 5
        if (is_resource($this->handle) && function_exists('curl_reset')) {
117 5
            curl_reset($this->handle);
118 5
            $this->setDefaults();
119 5
            return true;
120
        }
121
122
        return false;
123
    }
124
}
125