Connection::__toString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
crap 12
1
<?php
2
3
namespace TreeHouse\Feeder\Transport;
4
5
class Connection implements \ArrayAccess
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $options;
11
12
    /**
13
     * @param array $options
14
     */
15 132
    public function __construct(array $options)
16
    {
17 132
        $this->options = $options;
18 132
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23 2
    public function offsetSet($offset, $value)
24
    {
25 2
        if (is_null($offset)) {
26
            $this->options[] = $value;
27
        } else {
28 2
            $this->options[$offset] = $value;
29
        }
30 2
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 90
    public function offsetExists($offset)
36
    {
37 90
        return isset($this->options[$offset]);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function offsetUnset($offset)
44
    {
45
        unset($this->options[$offset]);
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 86
    public function offsetGet($offset)
52
    {
53 86
        return isset($this->options[$offset]) ? $this->options[$offset] : null;
54
    }
55
56
    /**
57
     * @return array
58
     */
59
    public function toArray()
60
    {
61
        return $this->options;
62
    }
63
64
    /**
65
     * @return string
66
     */
67 16
    public function getHash()
68
    {
69 16
        return md5(json_encode($this->options));
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function __toString()
76
    {
77
        foreach (['name', 'url', 'file'] as $attempt) {
78
            if (array_key_exists($attempt, $this->options)) {
79
                return $this->options[$attempt];
80
            }
81
        }
82
83
        return json_encode($this);
84
    }
85
}
86