RedisEventStore::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Domain\Eventing;
4
5
use Predis\Client;
6
use Domain\Identity\Identity;
7
use Domain\Eventing\CommittedEvents;
8
9
/**
10
 * @author Sebastiaan Hilbers <[email protected]>
11
 */
12
final class RedisEventStore implements EventStore
13
{
14
    private $redis;
15
16
    public function __construct(Client $redis)
17
    {
18
        $this->redis = $redis;
19
    }
20
21
    public function commit(UncommittedEvents $events)
22
    {
23
        foreach ($events as $event) {
24
            $this->redis->rpush(
25
                (string) $event->getAggregateIdentity(),
26
                (string) serialize($event)
27
            );
28
        }
29
    }
30
31
    public function getAggregateHistoryFor(Identity $id, $offset = 0, $max = null)
32
    {
33
        if (is_null($max)) {
34
            $max = $this->redis->llen((string) $id);
35
        }
36
37
        $events = $this->redis->lrange((string) $id, $offset, $max);
38
39
        return new CommittedEvents(
40
            $id,
41
            array_map(function ($raw) {
42
                return unserialize($raw);
43
            }, $events)
44
        );
45
    }
46
}
47