Completed
Push — master ( 5c637b...29a871 )
by Adam
02:41
created

Curl::auto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\Request;
4
5
use BestServedCold\PhalueObjects\Contract\AutoDependency;
6
use BestServedCold\PhalueObjects\Contract\Request;
7
use BestServedCold\PhalueObjects\Exception\InvalidTypeException;
8
use BestServedCold\PhalueObjects\VOResource;
9
10
/**
11
 * Class Curl
12
 *
13
 * @package BestServedCold\PhalueObjects\Request
14
 */
15
class Curl extends VOResource implements Request, AutoDependency
16
{
17
    /**
18
     * @var array $options
19
     */
20
    protected static $options = [];
21
22
    /**
23
     * Curl constructor.
24
     *
25
     * @param $value
26
     */
27 6
    public function __construct($value)
28
    {
29 6
        if (! is_resource($value) || get_resource_type($value) !== 'curl') {
30 1
            throw new InvalidTypeException($value, [ 'curl resource' ]);
31 1
        }
32
33 6
        parent::__construct($value);
34 6
    }
35
36
    /**
37
     * @param  $resource
38
     * @param  array     $options
39
     * @return static
40
     */
41 5
    public static function fromOptions($resource, array $options = [])
42
    {
43 5
        return new static(self::setOptions($resource, self::$options + $options));
44
    }
45
46
    /**
47
     * @param  array  $options
48
     * @return static
49
     */
50 4
    public static function auto(array $options = [])
51
    {
52 4
        return self::fromOptions(curl_init(), $options);
53
    }
54
55
    /**
56
     * @param array $options
57
     * @param $resource
58
     */
59 5
    protected static function setOptions($resource, array $options)
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...
60
    {
61 5
        foreach ($options as $name => $value) {
62 2
            curl_setopt($resource, $name, $value);
63 5
        }
64
65 5
        return $resource;
66
    }
67
68
    /**
69
     * @return mixed
70
     */
71 1
    public function getContents()
72
    {
73 1
        return curl_exec($this->getValue());
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79 1
    public function getInfo()
80
    {
81 1
        return curl_getinfo($this->getValue());
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 6
    public function close()
88
    {
89 6
        curl_close($this->getValue());
90 6
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 6
    public function __destruct()
96
    {
97 6
        $this->close();
98 6
    }
99
}
100