|
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); |
|
|
|
|
|
|
68
|
|
|
$user->clearEvents(); |
|
69
|
|
|
|
|
70
|
|
|
Projector::instance()->project($events); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
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.