DomainEventEmitter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 16
cp 0
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A emit() 0 7 1
A assertInstance() 0 7 2
A resetEvents() 0 4 1
A recordThat() 0 4 1
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