Completed
Push — master ( 1b6d66...884e6a )
by Adam
02:37
created

Curl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
crap 2
1
<?php namespace BestServedCold\PhalueObjects\Access;
2
3
use BestServedCold\PhalueObjects\Utility\Native\Constant;
4
use BestServedCold\PhalueObjects\ValueObject;
5
6
final class Curl extends ValueObject
7
{
8
    public $connectTimeout  = CURLOPT_CONNECTTIMEOUT;
9
    public $followRedirects = CURLOPT_FOLLOWLOCATION;
10
    public $headers         = CURLOPT_HEADER;
11
    public $maxRedirects    = CURLOPT_MAXREDIRS;
12
    public $noBody          = CURLOPT_NOBODY;
13
    public $returnTransfer  = CURLOPT_RETURNTRANSFER;
14
    public $timeout         = CURLOPT_TIMEOUT;
15
16
    private $options = [];
17
18
    public function __construct($value)
19
    {
20
        parent::__construct($value);
21
        $this->init()
22
            ->setOption($this->headers)
23
            ->setOption($this->timeout)
24
            ->setOption($this->connectTimeout);
25
    }
26
27
    public function exec()
28
    {
29
        return curl_exec($this->getValue());
30
    }
31
32
    private function init()
33
    {
34
        if (! $this->value = curl_init($this->getValue())) {
35
            throw new \Exception;
36
        }
37
38
        return $this;
39
    }
40
41
    public function getOptions()
42
    {
43
        return $this->options;
44
    }
45
46
    public function getOption($option)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        return isset($this->options[$option]) ? $this->options[$option] : null;
49
    }
50
51
    /**
52
     * @param $option
53
     * @param bool|true $value
54
     * @return $this
55
     */
56
    public function setOption($option, $value = true)
57
    {
58
        if (curl_setopt($this->getValue(), $option, $value)) {
59
            $this->options[Constant::init()->curl($option)] = $value;
0 ignored issues
show
Documentation Bug introduced by
The method curl does not exist on object<BestServedCold\Ph...tility\Native\Constant>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
60
        }
61
62
        return $this;
63
    }
64
65
    public function errorNumber()
66
    {
67
        return curl_errno($this->getValue());
68
    }
69
70
    public function error()
71
    {
72
        return curl_error($this->getValue());
73
    }
74
75
    public function info()
76
    {
77
        return curl_getinfo($this->getValue());
78
    }
79
80
    public function __destruct()
81
    {
82
        curl_close($this->value);
83
    }
84
}
85