RedisEventStore   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 35
wmc 5
lcom 1
cbo 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A commit() 0 9 2
A getAggregateHistoryFor() 0 15 2
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