Completed
Push — master ( a22a10...61cbe3 )
by Zoltán
05:08
created

CurlClient::setOptionsOnRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4286
cc 2
eloc 3
nc 2
nop 2
crap 6
1
<?php namespace Phabricator\Client\Curl;
2
3
use Phabricator\Client\ClientInterface;
4
5
/**
6
 * Simple CURL based client
7
 *
8
 * Phabricator PHP API
9
 *
10
 * @author Zoltán Borsos <[email protected]>
11
 * @package Phabricator
12
 * @subpackage Client\Curl
13
 *
14
 * @copyright    Copyright 2016, Zoltán Borsos.
15
 * @license      https://github.com/Zolli/Phabricator-PHP-API/blob/master/LICENSE.md
16
 * @link         https://github.com/Zolli/Phabricator-PHP-API
17
 */
18
class CurlClient implements ClientInterface {
19
20
    /**
21
     * Hold options that set in request when creating it
22
     *
23
     * @type array
24
     */
25
    protected $options = [];
26
27
    /**
28
     * {@inheritDoc}
29
     *
30
     * @codeCoverageIgnore
31
     */
32
    public function request($url, $requestData) {
33
        $request = new CurlRequest($url);
34
        $request->setPostData($requestData);
35
36
        $this->setOptionsOnRequest($request, $this->options);
37
38
        return $request->execute();
0 ignored issues
show
Bug Compatibility introduced by
The expression $request->execute(); of type array|stdClass adds the type stdClass to the return on line 38 which is incompatible with the return type declared by the interface Phabricator\Client\ClientInterface::request of type array.
Loading history...
39
    }
40
41
    /**
42
     * Set option for CURL request
43
     *
44
     * @param int $option CURLOPT_* constants
45
     * @param mixed $value The option value
46
     *
47
     * @return \Phabricator\Client\Curl\CurlClient
48
     */
49 1
    public function setOption($option, $value) {
50 1
        $this->options[$option] = $value;
51
52 1
        return $this;
53
    }
54
55
    /**
56
     * Set CIRL request options from array. The array key is the option
57
     * and the value is used to option value.
58
     *
59
     * @param array $options
60
     *
61
     * @return \Phabricator\Client\Curl\CurlClient
62
     */
63 1
    public function setOptionArray(array $options) {
64 1
        $this->options = $options;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * Set the defined options on the given CurlRequest instance
71
     *
72
     * @param \Phabricator\Client\Curl\CurlRequest $request
73
     * @param $options
74
     *
75
     * @throws \BuildR\Foundation\Exception\RuntimeException
76
     *
77
     * @codeCoverageIgnore
78
     */
79
    protected function setOptionsOnRequest(CurlRequest $request, $options) {
80
        foreach($options as $option => $value) {
81
            $request->setOption($options, $value);
82
        }
83
    }
84
85
}
86