RecordThatUsesConfigurableFactories   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 3
b 0
f 0
dl 0
loc 30
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createDateTimeInterfaceForNow() 0 5 1
A createDomainEvent() 0 4 1
A recordThat() 0 6 1
1
<?php declare(strict_types=1);
2
namespace Kepawni\Twilted\Basic;
3
4
use DateTimeImmutable;
5
use DateTimeInterface;
6
use DateTimeZone;
7
use Exception;
8
use Kepawni\Twilted\EntityIdentifier;
9
use Kepawni\Twilted\DomainEvent;
10
use Kepawni\Twilted\EventPayload;
11
12
trait RecordThatUsesConfigurableFactories
13
{
14
    protected static $dateTimeClass = DateTimeImmutable::class;
15
    protected static $domainEventClass = SimpleDomainEvent::class;
16
    protected static $timeZoneClass = DateTimeZone::class;
17
    protected $recordedEvents = [];
18
19
    abstract public function getId(): EntityIdentifier;
20
21
    abstract protected function apply(DomainEvent $event): void;
22
23
    protected function createDateTimeInterfaceForNow(): DateTimeInterface
24
    {
25
        $timeZoneClass = self::$timeZoneClass;
26
        $dateTimeClass = self::$dateTimeClass;
27
        return new $dateTimeClass('now', new $timeZoneClass('UTC'));
28
    }
29
30
    protected function createDomainEvent(EventPayload $what, DateTimeInterface $when): SimpleDomainEvent
31
    {
32
        $domainEventClass = self::$domainEventClass;
33
        return new $domainEventClass($what, $this->getId(), $when);
34
    }
35
36
    protected function recordThat(EventPayload $what): void
37
    {
38
        $now = $this->createDateTimeInterfaceForNow();
39
        $recordedEvent = $this->createDomainEvent($what, $now);
40
        $this->apply($recordedEvent);
41
        $this->recordedEvents[] = $recordedEvent;
42
    }
43
}
44