Completed
Push — master ( 8f9174...4bbbcd )
by Andy
02:52
created

CurlOpts::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Palmtree\Curl;
4
5
use Palmtree\Curl\Exception\InvalidArgumentException;
6
7
class CurlOpts implements \ArrayAccess, \IteratorAggregate
8
{
9
    /** @var array */
10
    public static $defaults = [
11
        CURLOPT_RETURNTRANSFER => true,
12
        CURLOPT_FOLLOWLOCATION => true,
13
        CURLOPT_AUTOREFERER    => true,
14
        CURLOPT_USERAGENT      => 'Palmtree\Curl',
15
        CURLOPT_HEADER         => true,
16
    ];
17
    /** @var array */
18
    private $opts = [];
19
20 12
    public function __construct(array $opts = [])
21
    {
22 12
        foreach (\array_replace(self::$defaults, $opts) as $key => $value) {
23 12
            $this->set($key, $value);
24
        }
25 12
    }
26
27 10
    public function toArray(): array
28
    {
29 10
        return $this->opts;
30
    }
31
32 12
    public function set(int $key, $value): self
33
    {
34 12
        if ($key === CURLOPT_HEADER && !$value) {
35 1
            throw new InvalidArgumentException('CURLOPT_HEADER cannot be set to false');
36
        }
37
38 12
        $this->opts[$key] = $value;
39
40 12
        return $this;
41
    }
42
43
    public function offsetExists($offset)
44
    {
45
        return isset($this->opts[$offset]);
46
    }
47
48 1
    public function offsetGet($offset)
49
    {
50 1
        return $this->opts[$offset];
51
    }
52
53
    public function offsetSet($offset, $value)
54
    {
55
        $this->set($offset, $value);
56
    }
57
58
    public function offsetUnset($offset)
59
    {
60
        unset($this->opts[$offset]);
61
    }
62
63
    public function getIterator()
64
    {
65
        return new \ArrayIterator($this->opts);
66
    }
67
}
68