Bag   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 7.14%

Importance

Changes 0
Metric Value
wmc 13
dl 0
loc 114
ccs 2
cts 28
cp 0.0714
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 3 1
A key() 0 3 1
A next() 0 3 1
A unserialize() 0 3 1
A __toArray() 0 3 1
A valid() 0 3 1
A serialize() 0 3 1
A rewind() 0 3 1
A current() 0 3 1
A seek() 0 9 3
A jsonSerialize() 0 3 1
1
<?php
2
3
namespace JobQueue\Domain\Utils;
4
5
abstract class Bag implements \Countable, \SeekableIterator, \Serializable, \JsonSerializable
6
{
7
    /**
8
     *
9
     * @var array
10
     */
11
    protected $data = [];
12
13
    /**
14
     *
15
     * @return int
16
     */
17
    public function count(): int
18
    {
19
        return count($this->data);
20
    }
21
22
23
    /**
24
     *
25
     * @param int $key
26
     * @return int
27
     */
28
    public function seek($key)
29
    {
30
        if ($key >= count($this->data)) {
31
            throw new \OutOfBoundsException;
32
        }
33
34
        $this->rewind();
35
        for ($i=0; $i<$key; $i++) {
36
            $this->next();
37
        }
38
    }
39
40
    /**
41
     *
42
     * @return mixed
43
     */
44
    public function current()
45
    {
46
        return current($this->data);
47
    }
48
49
    /**
50
     *
51
     * @return mixed
52
     */
53
    public function next()
54
    {
55
        return next($this->data);
56
    }
57
58
    /**
59
     *
60
     * @return mixed
61
     */
62
    public function key()
63
    {
64
        return key($this->data);
65
    }
66
67
    /**
68
     *
69
     * @return bool
70
     */
71
    public function valid(): bool
72
    {
73
        return null !== key($this->data);
74
    }
75
76
    /**
77
     *
78
     * @return mixed
79
     */
80
    public function rewind()
81
    {
82
        return reset($this->data);
83
    }
84
85
    /**
86
     *
87
     * @return string
88
     */
89
    public function serialize(): string
90
    {
91
        return serialize($this->data);
92
    }
93
94
    /**
95
     *
96
     * @param string $serialized
97
     */
98
    public function unserialize($serialized)
99
    {
100
        $this->data = unserialize($serialized);
101
    }
102
103
    /**
104
     *
105
     * @return array
106
     */
107
    public function jsonSerialize(): array
108
    {
109
        return $this->data;
110
    }
111
112
    /**
113
     *
114
     * @return array
115
     */
116 18
    public function __toArray(): array
117
    {
118 18
        return $this->data;
119
    }
120
}
121