Passed
Push — master ( 35eebd...7b8a1a )
by Roman
16:45
created

DomainEventsSubscriber::dispatchCollectedEvents()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Game\Features\Player;
6
7
use Countable;
8
use Doctrine\Common\EventSubscriber;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\EventSubscriber was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\ORM\Event\LifecycleEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\LifecycleEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Doctrine\ORM\Event\PostFlushEventArgs;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Event\PostFlushEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Doctrine\ORM\Events;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\Events was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Messenger\MessageBusInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Messenger\MessageBusInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
final class DomainEventsSubscriber implements EventSubscriber, Countable
15
{
16
    /**
17
     * @var DomainEventInterface[]
18
     */
19
    private array $events = [];
20
    private MessageBusInterface $messageBus;
21
22
    public function __construct(MessageBusInterface $messageBus)
23
    {
24
        $this->messageBus = $messageBus;
25
    }
26
27
    public function count(): int
28
    {
29
        return count($this->events);
30
    }
31
32
    public function getSubscribedEvents(): array
33
    {
34
        return [
35
            Events::postPersist,
36
            Events::postUpdate,
37
            Events::postRemove,
38
            Events::postFlush,
39
        ];
40
    }
41
42
    public function postUpdate(LifecycleEventArgs $event): void
43
    {
44
        $this->doCollect($event);
45
    }
46
47
    public function postPersist(LifecycleEventArgs $event): void
48
    {
49
        $this->doCollect($event);
50
    }
51
52
    public function postRemove(LifecycleEventArgs $event): void
53
    {
54
        $this->doCollect($event);
55
    }
56
57
    public function postFlush(PostFlushEventArgs $args): void
58
    {
59
        $this->dispatchCollectedEvents();
60
    }
61
62
    private function doCollect(LifecycleEventArgs $event): void
63
    {
64
        $entity = $event->getEntity();
65
        if (!$entity instanceof RaiseEventsInterface) {
66
            return;
67
        }
68
69
        foreach ($entity->popEvents() as $item) {
70
            $this->events[spl_object_hash($item)] = $item;
71
        }
72
    }
73
74
    private function dispatchCollectedEvents(): void
75
    {
76
        $events = $this->events;
77
        $this->events = [];
78
79
        foreach ($events as $event) {
80
            $this->messageBus->dispatch($event);
81
        }
82
83
        $this->count() && $this->dispatchCollectedEvents();
84
    }
85
}
86