Completed
Pull Request — master (#366)
by Beñat
05:52 queued 01:06
created

RedisEventStore   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A appendTo() 0 13 2
A streamOfId() 0 22 3
A redisKey() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\SharedKernel\Infrastructure\Persistence\Redis\EventStore;
16
17
use Kreta\SharedKernel\Domain\Model\AggregateDoesNotExistException;
18
use Kreta\SharedKernel\Domain\Model\DomainEventCollection;
19
use Kreta\SharedKernel\Domain\Model\Identity\BaseId as Id;
20
use Kreta\SharedKernel\Event\EventStore;
21
use Kreta\SharedKernel\Event\EventStream;
22
use Kreta\SharedKernel\Serialization\Serializer;
23
use Predis\Client;
24
25
final class RedisEventStore implements EventStore
26
{
27
    private const REDIS_KEY_PLACEHOLDER = 'events: %s';
28
29
    private $predis;
30
    private $serializer;
31
32
    public function __construct(Client $predis, Serializer $serializer)
33
    {
34
        $this->predis = $predis;
35
        $this->serializer = $serializer;
36
    }
37
38
    public function appendTo(EventStream $stream) : void
39
    {
40
        foreach ($stream->events() as $event) {
41
            $serializedEvent = $this->serializer->serialize(
42
                [
43
                    'type' => get_class($event),
44
                    'data' => $this->serializer->serialize($event),
45
                ]
46
            );
47
48
            $this->predis->rpush($this->redisKey($stream->aggregateRootId()), $serializedEvent);
49
        }
50
    }
51
52
    public function streamOfId(Id $aggregateRootId) : EventStream
53
    {
54
        if (!$this->predis->exists($this->redisKey($aggregateRootId))) {
55
            throw new AggregateDoesNotExistException($aggregateRootId->id());
56
        }
57
58
        $serializedEvents = $this->predis->lrange($this->redisKey($aggregateRootId), 0, -1);
59
60
        $events = new DomainEventCollection();
61
        foreach ($serializedEvents as $serializedEvent) {
62
            $eventData = $this->serializer->deserialize($serializedEvent, 'array');
0 ignored issues
show
Unused Code introduced by
The call to Serializer::deserialize() has too many arguments starting with 'array'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
63
64
            $events->add(
65
                $this->serializer->deserialize(
66
                    $eventData['data'],
67
                    $eventData['type']
0 ignored issues
show
Unused Code introduced by
The call to Serializer::deserialize() has too many arguments starting with $eventData['type'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
68
                )
69
            );
70
        }
71
72
        return new EventStream($aggregateRootId, $events);
73
    }
74
75
    private function redisKey(Id $aggregateRootId)
76
    {
77
        return sprintf(self::REDIS_KEY_PLACEHOLDER, $aggregateRootId->id());
78
    }
79
}
80