LogAllEvents::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PhpDDD\DomainDrivenDesign\EventLog\EventSubscriber;
4
5
use PhpDDD\DomainDrivenDesign\Event\AbstractEvent;
6
use PhpDDD\DomainDrivenDesign\Event\EventListener;
7
use PhpDDD\DomainDrivenDesign\Event\EventSubscriberInterface;
8
use PhpDDD\DomainDrivenDesign\EventLog\EventLog;
9
use PhpDDD\DomainDrivenDesign\EventLog\Repository\EventLogRepositoryInterface;
10
11
/**
12
 * Subscribe to every events and save them.
13
 */
14
final class LogAllEvents implements EventSubscriberInterface
15
{
16
    /**
17
     * @var EventLogRepositoryInterface
18
     */
19
    private $repository;
20
21
    /**
22
     * @return EventListener[] The event names to listen to
23
     */
24
    public static function getSubscribedEvents()
25
    {
26
        return [
27
            new EventListener('logEvent'), // Subscribe to every event synchronously
28
        ];
29
    }
30
31
    /**
32
     * @param EventLogRepositoryInterface $repository
33
     */
34
    public function __construct(EventLogRepositoryInterface $repository)
35
    {
36
        $this->repository = $repository;
37
    }
38
39
    /**
40
     * @param AbstractEvent $event
41
     *
42
     * @return AbstractEvent
43
     */
44
    public function logEvent(AbstractEvent $event)
45
    {
46
        $eventLog = EventLog::addEventEntry($event);
47
48
        return $this->repository->save($eventLog);
49
    }
50
}
51