Passed
Push — master ( 67aa47...d37a32 )
by vincent
02:27
created

CurlRequest::setOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace vfalies\tmdb\lib;
4
5
use vfalies\tmdb\Interfaces\HttpRequestInterface;
6
7
class CurlRequest implements HttpRequestInterface
8
{
9
10
    private $handle = null;
11
12
    /**
13
     * Set the url to cUrl call
14
     * @param string $url
15
     * @throws Exception
16
     */
17 4
    public function setUrl(string $url): void
18
    {
19 4
        $this->handle = curl_init($url);
20 4
    }
21
22
    /**
23
     * Close cUrl handle
24
     */
25 1
    public function close(): void
26
    {
27 1
        curl_close($this->handle);
28 1
    }
29
30
    /**
31
     * Execute cUrl call
32
     * @return mixed
33
     * @throws \Exception
34
     */
35 2
    public function execute()
36
    {
37 2
        $result = curl_exec($this->handle);
38 2
        if ($result === false)
39
        {
40 1
            throw new \Exception('cUrl failed : ' . var_export($this->getInfo(), true), 1004);
41
        }
42 1
        return $result;
43
    }
44
45
    /**
46
     * Get cUrl infos
47
     * @param string $name
48
     * @return mixed
49
     * @throws Exception
50
     */
51 2
    public function getInfo(string $name = '')
52
    {
53 2
        if (empty($name))
54
        {
55 2
            $info = curl_getinfo($this->handle);
56
        } else
57
        {
58 1
            $info = curl_getinfo($this->handle, $name);
59
        }
60 2
        return $info;
61
    }
62
63
    /**
64
     * Set cUrl option
65
     * @param string $name
66
     * @param mixed $value
67
     * @throws \Exception
68
     */
69 3
    public function setOption(string $name, $value): HttpRequestInterface
70
    {
71 3
        curl_setopt($this->handle, $name, $value);
72 3
        return $this;
73
    }
74
75
    /**
76
     * Magical getter
77
     * @param string $name
78
     * @return mixed
79
     */
80 1
    public function __get(string $name)
81
    {
82 1
        $response = '';
83
        switch ($name)
84
        {
85 1
            case 'handle':
86 1
                $response = $this->$name;
87 1
                break;
88
        }
89 1
        return $response;
90
    }
91
}
92