Completed
Pull Request — master (#366)
by Beñat
04:18
created

EventStoreNotificationRepository::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 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\Notification;
16
17
use Kreta\Notifier\Domain\Model\Inbox\Notification\Notification;
18
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationDoesNotExist;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationId;
20
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationRepository;
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 EventStoreNotificationRepository implements NotificationRepository
29
{
30
    private const NAME = 'notification';
31
32
    private $eventStore;
33
34
    public function __construct(EventStore $eventStore)
35
    {
36
        $this->eventStore = $eventStore;
37
    }
38
39
    public function get(NotificationId $id) : Notification
40
    {
41
        try {
42
            return Notification::reconstitute(
43
                $this->eventStore->streamOfName(
44
                    new StreamName(
45
                        $id,
46
                        self::NAME
47
                    )
48
                )
49
            );
50
        } catch (AggregateDoesNotExistException $exception) {
51
            throw new NotificationDoesNotExist();
52
        }
53
    }
54
55
    public function save(Notification $notification) : void
56
    {
57
        $events = new DomainEventCollection($notification->recordedEvents());
58
59
        $eventStream = new Stream(
60
            new StreamName(
61
                $notification->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
        $notification->clearEvents();
69
70
        Projector::instance()->project($events);
71
    }
72
}
73