Completed
Pull Request — master (#366)
by Beñat
06:07
created

EventStoreUserRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 15 2
A save() 0 17 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\Notifier\Infrastructure\Domain\Model\Inbox;
16
17
use Kreta\Notifier\Domain\Model\Inbox\User;
18
use Kreta\Notifier\Domain\Model\Inbox\UserDoesNotExist;
19
use Kreta\Notifier\Domain\Model\Inbox\UserId;
20
use Kreta\Notifier\Domain\Model\Inbox\UserRepository;
21
use Kreta\SharedKernel\Domain\Model\AggregateDoesNotExistException;
22
use Kreta\SharedKernel\Domain\Model\DomainEventCollection;
23
use Kreta\SharedKernel\Event\EventStore;
24
use Kreta\SharedKernel\Event\Stream;
25
use Kreta\SharedKernel\Event\StreamName;
26
use Kreta\SharedKernel\Projection\Projector;
27
28
final class EventStoreUserRepository implements UserRepository
29
{
30
    private const NAME = 'user';
31
32
    private $eventStore;
33
34
    public function __construct(EventStore $eventStore)
35
    {
36
        $this->eventStore = $eventStore;
37
    }
38
39
    public function get(UserId $id) : User
40
    {
41
        try {
42
            return User::reconstitute(
43
                $this->eventStore->streamOfName(
44
                    new StreamName(
45
                        $id,
46
                        self::NAME
47
                    )
48
                )
49
            );
50
        } catch (AggregateDoesNotExistException $exception) {
51
            throw new UserDoesNotExist();
52
        }
53
    }
54
55
    public function save(User $user) : void
56
    {
57
        $events = new DomainEventCollection($user->recordedEvents());
58
59
        $eventStream = new Stream(
60
            new StreamName(
61
                $user->id(),
62
                self::NAME
63
            ),
64
            $events
65
        );
66
67
        $this->eventStore->appendTo($eventStream);
0 ignored issues
show
Bug introduced by
The method appendTo() does not exist on Kreta\SharedKernel\Event\EventStore. Did you maybe mean append()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
        $user->clearEvents();
69
70
        Projector::instance()->project($events);
71
    }
72
}
73