Completed
Pull Request — master (#66)
by Frank
07:06 queued 04:59
created

retrieve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\Snapshotting;
6
7
use EventSauce\EventSourcing\AggregateRootId;
8
use EventSauce\EventSourcing\AggregateRootRepository;
9
use EventSauce\EventSourcing\Message;
10
use EventSauce\EventSourcing\MessageRepository;
11
use Generator;
12
13
final class ConstructingAggregateRootRepositoryWithSnapshotting implements AggregateRootRepositoryWithSnapshotting
14
{
15
    /**
16
     * @var string
17
     */
18
    private $aggregateRootClassName;
19
20
    /**
21
     * @var MessageRepository
22
     */
23
    private $messageRepository;
24
25
    /**
26
     * @var SnapshotRepository
27
     */
28
    private $snapshotRepository;
29
30
    /**
31
     * @var AggregateRootRepository
32
     */
33
    private $regularRepository;
34
35
    public function __construct(
36
        string $aggregateRootClassName,
37
        MessageRepository $messageRepository,
38
        SnapshotRepository $snapshotRepository,
39
        AggregateRootRepository $regularRepository
40
    ) {
41
        $this->aggregateRootClassName = $aggregateRootClassName;
42
        $this->messageRepository = $messageRepository;
43
        $this->snapshotRepository = $snapshotRepository;
44
        $this->regularRepository = $regularRepository;
45
    }
46
47
    public function retrieveFromSnapshot(AggregateRootId $aggregateRootId): object
48
    {
49
        $snapshot = $this->snapshotRepository->retrieve($aggregateRootId);
50
51
        if ( ! $snapshot instanceof Snapshot) {
52
            return $this->retrieve($aggregateRootId);
53
        }
54
55
        /** @var AggregateRootWithSnapshotting $className */
56
        $className = $this->aggregateRootClassName;
57
        $events = $this->retrieveAllEventsAfterVersion($aggregateRootId, $snapshot->aggregateRootVersion());
58
59
        return $className::reconstituteFromSnapshotAndEvents($snapshot, $events);
60
    }
61
62
    public function storeSnapshot(AggregateRootWithSnapshotting $aggregateRoot): void
63
    {
64
        $snapshot = $aggregateRoot->createSnapshot();
65
        $this->snapshotRepository->persist($snapshot);
66
    }
67
68
    private function retrieveAllEventsAfterVersion(AggregateRootId $aggregateRootId, int $version)
69
    {
70
        /** @var Message[]|Generator $messages */
71
        $messages = $this->messageRepository->retrieveAllAfterVersion($aggregateRootId, $version);
72
73
        foreach ($messages as $message) {
74
            yield $message->event();
75
        }
76
77
        return $messages->getReturn();
78
    }
79
80
    public function retrieve(AggregateRootId $aggregateRootId): object
81
    {
82
        return $this->regularRepository->retrieve($aggregateRootId);
83
    }
84
85
    public function persist(object $aggregateRoot): void
86
    {
87
        $this->regularRepository->persist($aggregateRoot);
88
    }
89
90
    public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, object ...$events): void
91
    {
92
        $this->regularRepository->persistEvents($aggregateRootId, $aggregateRootVersion, ...$events);
93
    }
94
}
95