|
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\Domain\Model\Inbox; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\SharedKernel\Domain\Model\AggregateRoot; |
|
18
|
|
|
use Kreta\SharedKernel\Domain\Model\EventSourcedAggregateRoot; |
|
19
|
|
|
use Kreta\SharedKernel\Event\EventStream; |
|
20
|
|
|
|
|
21
|
|
|
class User extends AggregateRoot implements EventSourcedAggregateRoot |
|
22
|
|
|
{ |
|
23
|
|
|
private $id; |
|
24
|
|
|
private $notifications; |
|
25
|
|
|
|
|
26
|
|
|
private function __construct(UserId $id) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->id = $id; |
|
29
|
|
|
$this->notifications = new Notifications(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function signUp(UserId $id) : self |
|
33
|
|
|
{ |
|
34
|
|
|
$instance = new self($id); |
|
35
|
|
|
$instance->publish(new UserSignedUp($id)); |
|
36
|
|
|
|
|
37
|
|
|
return $instance; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function receiveNotification(NotificationId $notificationId, NotificationBody $body) : void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->publish( |
|
43
|
|
|
new UserReceivedNotification( |
|
44
|
|
|
$this->id, |
|
45
|
|
|
$notificationId, |
|
46
|
|
|
$body |
|
47
|
|
|
) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function readNotification(Notification $notification) : void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->publish( |
|
54
|
|
|
new UserReadNotification($this->id, $notification->id()) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function unreadNotification(Notification $notification) : void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->publish( |
|
61
|
|
|
new UserUnreadNotification($this->id, $notification->id()) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function id() : UserId |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->id; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function __toString() : string |
|
71
|
|
|
{ |
|
72
|
|
|
return (string) $this->id()->id(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function notification(NotificationId $notificationId) : Notification |
|
76
|
|
|
{ |
|
77
|
|
|
foreach ($this->notifications as $notification) { |
|
78
|
|
|
if ($notificationId->equals($notification->id())) { |
|
79
|
|
|
return $notification; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
throw new NotificationDoesNotExist(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function applyUserReceivedNotification(UserReceivedNotification $event) : void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->notifications->add( |
|
89
|
|
|
Notification::broadcast( |
|
90
|
|
|
$event->notificationId(), |
|
91
|
|
|
$event->body() |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function applyUserReadNotification(UserReadNotification $event) : void |
|
97
|
|
|
{ |
|
98
|
|
View Code Duplication |
foreach ($this->notifications as $key => $notification) { |
|
|
|
|
|
|
99
|
|
|
if ($notification->id()->equals($event->notificationId())) { |
|
100
|
|
|
$this->notifications->of($key)->read(); |
|
101
|
|
|
|
|
102
|
|
|
break; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function applyUserUnreadNotification(UserUnreadNotification $event) : void |
|
108
|
|
|
{ |
|
109
|
|
View Code Duplication |
foreach ($this->notifications as $key => $notification) { |
|
|
|
|
|
|
110
|
|
|
if ($notification->id()->equals($event->notificationId())) { |
|
111
|
|
|
$this->notifications->of($key)->unread(); |
|
112
|
|
|
|
|
113
|
|
|
break; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
View Code Duplication |
public static function reconstitute(EventStream $stream) : EventSourcedAggregateRoot |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
$receiver = new self($stream->aggregateRootId()); |
|
|
|
|
|
|
121
|
|
|
$events = $stream->events()->toArray(); |
|
122
|
|
|
foreach ($events as $event) { |
|
123
|
|
|
$receiver->apply($event); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $receiver; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.