DomainEventEmitter::resetEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\EventSource\Domain\Event;
6
7
use Generator;
8
9
class DomainEventEmitter
10
{
11
    private static ?self $instance = null;
12
    private EventCollection $events;
13
14
    private function __construct()
15
    {
16
        $this->resetEvents();
17
    }
18
19
    private function resetEvents(): void
20
    {
21
        /** @var EventCollection events */
22
        $this->events = EventCollection::createEmptyCollection();
23
    }
24
25
    public static function recordThat(AggregateChanged $event): void
26
    {
27
        $self = self::assertInstance();
28
        $self->events->addItem($event);
29
    }
30
31
    public static function emit(): Generator
32
    {
33
        $self = self::assertInstance();
34
        $events = $self->events;
35
        $self->resetEvents();
36
37
        yield from $events;
38
    }
39
40
    private static function assertInstance(): self
41
    {
42
        if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
43
            static::$instance = new self;
44
        }
45
46
        return static::$instance;
47
    }
48
}
49