SchedulableEventCollection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
dl 0
loc 110
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A rewind() 0 3 1
A valid() 0 3 1
A count() 0 3 1
A filterByEvent() 0 10 1
A key() 0 3 1
A next() 0 3 1
A remove() 0 7 2
A add() 0 3 1
A __construct() 0 6 2
A contains() 0 3 1
A current() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Adlogix\EventScheduler;
6
7
use Countable;
8
use Iterator;
9
use SplObjectStorage;
10
11
/**
12
 * @author Toni Van de Voorde <[email protected]>
13
 */
14
final class SchedulableEventCollection implements Countable, Iterator
15
{
16
    /**
17
     * @var SplObjectStorage|SchedulableEvent[]
18
     */
19
    private $events;
20
21
    /**
22
     * @param array $events
23
     */
24 12
    public function __construct(array $events = [])
25
    {
26 12
        $this->events = new SplObjectStorage;
27
28 12
        foreach ($events as $event) {
29 7
            $this->add($event);
30
        }
31 12
    }
32
33
    /**
34
     * @param SchedulableEvent $schedulableEvent
35
     */
36 8
    public function add(SchedulableEvent $schedulableEvent)
37
    {
38 8
        $this->events->attach($schedulableEvent);
39 8
    }
40
41
    /**
42
     * @param SchedulableEvent $schedulableEvent
43
     * @return bool
44
     */
45
    public function contains(SchedulableEvent $schedulableEvent): bool
46
    {
47
        return $this->events->contains($schedulableEvent);
48
    }
49
50
    /**
51
     * @param SchedulableEvent $schedulableEvent
52
     */
53 2
    public function remove(SchedulableEvent $schedulableEvent)
54
    {
55 2
        if (!$this->events->contains($schedulableEvent)) {
56 1
            throw Exception\NotScheduledEventException::create();
57
        }
58
59 1
        $this->events->detach($schedulableEvent);
60 1
    }
61
62
    /**
63
     * @param EventInterface $event
64
     * @return SchedulableEventCollection
65
     */
66 11
    public function filterByEvent(EventInterface $event): self
67
    {
68 11
        $filteredEvents = array_filter(
69 11
            iterator_to_array($this->events),
70
            function (SchedulableEvent $scheduledEvent) use ($event) {
71 7
                return $event->equals($scheduledEvent->event());
72 11
            }
73
        );
74
75 11
        return new self($filteredEvents);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 7
    public function current(): SchedulableEvent
82
    {
83 7
        return $this->events->current();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 6
    public function next()
90
    {
91 6
        $this->events->next();
92 6
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function key(): int
98
    {
99
        return $this->events->key();
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 10
    public function valid(): bool
106
    {
107 10
        return $this->events->valid();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 10
    public function rewind()
114
    {
115 10
        $this->events->rewind();
116 10
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function count(): int
122
    {
123 1
        return $this->events->count();
124
    }
125
}
126