Completed
Push — master ( d19dfb...5b8d73 )
by Adam
13:31
created

Curl::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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
    private $constants;
0 ignored issues
show
Unused Code introduced by
The property $constants is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
19
    public function __construct($value)
20
    {
21
        parent::__construct($value);
22
//        $this->constants = new Constant();
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
23
        $this->init()
24
            ->setOption($this->headers)
25
            ->setOption($this->timeout)
26
            ->setOption($this->connectTimeout);
27
    }
28
29
    public function exec()
30
    {
31
        return curl_exec($this->getValue());
32
    }
33
34
    private function init()
35
    {
36
        if (! $this->value = curl_init($this->getValue())) {
37
            throw new \Exception;
38
        }
39
40
        return $this;
41
    }
42
43
    public function getOptions()
44
    {
45
        return $this->options;
46
    }
47
48
    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...
49
    {
50
        return isset($this->options[$option]) ? $this->options[$option] : null;
51
    }
52
53
    /**
54
     * @param $option
55
     * @param bool|true $value
56
     * @return $this
57
     */
58
    public function setOption($option, $value = true)
59
    {
60
        if (curl_setopt($this->getValue(), $option, $value)) {
61
            $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...
62
        }
63
64
        return $this;
65
    }
66
67
    public function errorNumber()
68
    {
69
        return curl_errno($this->getValue());
70
    }
71
72
    public function error()
73
    {
74
        return curl_error($this->getValue());
75
    }
76
77
    public function info()
78
    {
79
        return curl_getinfo($this->getValue());
80
    }
81
82
    public function __destruct()
83
    {
84
        curl_close($this->value);
85
    }
86
}
87