CurlRequest::setOptionFromArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php namespace Phabricator\Client\Curl;
2
3
use BuildR\Foundation\Exception\RuntimeException;
4
use Phabricator\Phabricator;
5
6
/**
7
 * Class CurlRequest
8
 *
9
 * @package Phabricator\Client
10
 * @author Zoltán Borsos <[email protected]>
11
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3
12
 * @version 1.0.0
13
 */
14
class CurlRequest {
15
16
    /**
17
     * @var string The url to the request be made
18
     */
19
    private $requestUrl;
20
21
    /**
22
     * @var Resource CURL resource
23
     */
24
    private $handler;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param string $requestUrl
30
     */
31 6
    public function __construct($requestUrl) {
32 6
        $this->requestUrl = $requestUrl;
33
34 6
        $this->initialize();
35 6
    }
36
37
    /**
38
     * Initialize the CURL session
39
     */
40 6
    public function initialize() {
41 6
        $this->handler = curl_init();
42
43 6
        $this->setOption(CURLOPT_URL, $this->requestUrl)
44 6
             ->setOption(CURLOPT_VERBOSE, 0)
45 6
             ->setOption(CURLOPT_HEADER, 0);
46 6
    }
47
48
    /**
49
     * Set an opt in current curl handler
50
     *
51
     * @param int $option
52
     * @param mixed $value
53
     *
54
     * @throws \BuildR\Foundation\Exception\RuntimeException
55
     *
56
     * @return \Phabricator\Client\Curl\CurlRequest
57
     */
58 6
    public function setOption($option, $value) {
59
        //Silence it because errors handled differently
60 6
        $res = @curl_setopt($this->handler, $option, $value);
61
62 6
        if($res === TRUE) {
63 6
            return $this;
64
        }
65
66
        throw new RuntimeException('Failed to set the following option: ' . $option);
67
    }
68
69
    /**
70
     * Set multiple options with an associative array
71
     *
72
     * @param $options
73
     *
74
     * @return \Phabricator\Client\Curl\CurlRequest
75
     */
76 1
    public function setOptionFromArray($options) {
77 1
        foreach($options as $option => $value) {
78 1
            $this->setOption($option, $value);
79 1
        }
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * Set the posted data. And also set the CURLOPT_POST option to TRUE, if is
86
     * not set already.
87
     *
88
     * @param array $postData
89
     *
90
     * @codeCoverageIgnore
91
     */
92
    public function setPostData(array $postData) {
93
        $this->setOption(CURLOPT_POST, 1)
94
             ->setOption(CURLOPT_POSTFIELDS, $postData);
95
    }
96
97
    /**
98
     * Close the current request
99
     */
100 1
    public function close() {
101 1
        curl_close($this->handler);
102 1
    }
103
104
    /**
105
     * Execute the prepared request and optionally returns the response
106
     *
107
     * @param bool $returnTransfer Returns the returned response
108
     *
109
     * @throws \BuildR\Foundation\Exception\RuntimeException
110
     *
111
     * @return array|\stdClass
112
     *
113
     * @codeCoverageIgnore
114
     */
115
    public function execute($returnTransfer = TRUE) {
116
        //Need transfer return
117
        if($returnTransfer === TRUE) {
118
            $this->setOption(CURLOPT_RETURNTRANSFER, 1);
119
        }
120
121
        $result = curl_exec($this->handler);
122
123
        //Error handling
124
        if(curl_errno($this->handler)) {
125
            $format = [curl_errno($this->handler), curl_error($this->handler)];
126
            $this->close();
127
            throw RuntimeException::createByFormat('Error executing request, error code: %s, Message: %s', $format);
128
        }
129
130
        return $result;
131
    }
132
133
} 
134