EventIteratorCollection   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
eloc 32
c 2
b 0
f 0
dl 0
loc 132
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A next() 0 3 1
A count() 0 26 5
A __wakeup() 0 3 1
A __serialize() 0 3 1
A key() 0 3 1
A __unserialize() 0 3 1
A valid() 0 3 1
A __sleep() 0 3 1
A rewind() 0 3 1
A current() 0 14 3
1
<?php
2
3
/*
4
 * event (https://github.com/phpgears/event).
5
 * Event handling.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Event;
15
16
use Gears\Event\Exception\EventException;
17
use Gears\Event\Exception\InvalidEventException;
18
19
/**
20
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
21
 */
22
final class EventIteratorCollection implements EventCollection
23
{
24
    /**
25
     * @var \Iterator<Event>
26
     */
27
    private $iterator;
28
29
    /**
30
     * EventIteratorCollection constructor.
31
     *
32
     * @param \Iterator<mixed> $iterator
33
     */
34
    public function __construct(\Iterator $iterator)
35
    {
36
        $this->iterator = $iterator;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @return Event
43
     */
44
    public function current(): Event
45
    {
46
        /** @var mixed $event */
47
        $event = $this->iterator->current();
48
49
        if (!$event instanceof Event) {
50
            throw new InvalidEventException(\sprintf(
51
                'Event collection only accepts "%s", "%s" given.',
52
                Event::class,
53
                \is_object($event) ? \get_class($event) : \gettype($event)
54
            ));
55
        }
56
57
        return $event;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function next(): void
64
    {
65
        $this->iterator->next();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return string|int|null
72
     */
73
    public function key()
74
    {
75
        return $this->iterator->key();
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function valid(): bool
82
    {
83
        return $this->iterator->valid();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function rewind(): void
90
    {
91
        $this->iterator->rewind();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function count(): int
98
    {
99
        if ($this->iterator instanceof \EmptyIterator) {
100
            return 0;
101
        }
102
103
        if ($this->iterator instanceof \Countable) {
104
            return $this->iterator->count();
105
        }
106
107
        $currentKey = $this->iterator->key();
108
        $this->iterator->rewind();
109
110
        $count = 0;
111
        while ($this->iterator->valid()) {
112
            $count++;
113
114
            $this->iterator->next();
115
        }
116
117
        $this->iterator->rewind();
118
        while ($this->iterator->key() !== $currentKey) {
119
            $this->iterator->next();
120
        }
121
122
        return $count;
123
    }
124
125
    /**
126
     * @return string[]
127
     */
128
    public function __sleep(): array
129
    {
130
        throw new EventException(\sprintf('Event collection "%s" cannot be serialized.', self::class));
131
    }
132
133
    public function __wakeup(): void
134
    {
135
        throw new EventException(\sprintf('Event collection "%s" cannot be unserialized.', self::class));
136
    }
137
138
    /**
139
     * @return array<string, mixed>
140
     */
141
    public function __serialize(): array
142
    {
143
        throw new EventException(\sprintf('Event collection "%s" cannot be serialized.', self::class));
144
    }
145
146
    /**
147
     * @param array<string, mixed> $data
148
     *
149
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
150
     */
151
    public function __unserialize(array $data): void
152
    {
153
        throw new EventException(\sprintf('Event collection "%s" cannot be unserialized.', self::class));
154
    }
155
}
156