-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHandlingSensitiveData.php
More file actions
140 lines (119 loc) · 3.86 KB
/
Copy pathHandlingSensitiveData.php
File metadata and controls
140 lines (119 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
/*
* This file is part of the broadway/sensitive-data package.
*
* (c) 2020 Broadway project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__.'/../vendor/autoload.php';
/**
* Invitation aggregate root.
*/
class Invitation extends Broadway\EventSourcing\EventSourcedAggregateRoot
{
private $invitationId;
/**
* Factory method to create an invitation.
*/
public static function invite(string $invitationId, string $name)
{
$invitation = new Invitation();
// After instantiation of the object we apply the "InvitedEvent".
$invitation->apply(new InvitedEvent($invitationId, $name));
return $invitation;
}
/**
* Every aggregate root will expose its id.
*
* {@inheritdoc}
*/
public function getAggregateRootId(): string
{
return $this->invitationId;
}
/**
* The "apply" method of the "InvitedEvent".
*/
protected function applyInvitedEvent(InvitedEvent $event)
{
$this->invitationId = $event->invitationId;
}
}
/**
* A repository that will only store and retrieve Invitation aggregate roots.
*
* This repository uses the base class provided by the EventSourcing component.
*/
class InvitationRepository extends Broadway\EventSourcing\EventSourcingRepository
{
public function __construct(Broadway\EventStore\EventStore $eventStore, Broadway\EventHandling\EventBus $eventBus)
{
parent::__construct($eventStore, $eventBus, 'Invitation', new Broadway\EventSourcing\AggregateFactory\PublicConstructorAggregateFactory());
}
}
/*
* When using CQRS with commands, a lot of times you will find that you have a
* command object and a "dual" event. Mind though that this is not always the
* case. The following classes show the commands and events for our small
* domain model.
*/
/* All commands and events below will cary the id of the aggregate root. For
* our convenience and readability later on we provide base classes that hold
* this data.
*/
class InviteCommand
{
public $invitationId;
public $name;
public $password;
public function __construct($invitationId, $name, $password)
{
$this->invitationId = $invitationId;
$this->name = $name;
$this->password = $password;
}
}
class InvitedEvent
{
public $invitationId;
public $name;
public function __construct($invitationId, $name)
{
$this->invitationId = $invitationId;
$this->name = $name;
}
}
/*
* A command handler will be registered with the command bus and handle the
* commands that are dispatched. The command handler can be seen as a small
* layer between your application code and the actual domain code.
*
* In the end a command handler listens for commands and translates commands to
* method calls on the actual aggregate roots.
*/
class InvitationCommandHandler extends Broadway\CommandHandling\SimpleCommandHandler
{
private $repository;
private $sensitiveDataManager;
public function __construct(
Broadway\EventSourcing\EventSourcingRepository $repository,
Broadway\BroadwaySensitiveData\EventHandling\SensitiveDataManager $sensitiveDataManager
) {
$this->repository = $repository;
$this->sensitiveDataManager = $sensitiveDataManager;
}
/**
* A new invite aggregate root is created and added to the repository.
*/
protected function handleInviteCommand(InviteCommand $command)
{
$invitation = Invitation::invite($command->invitationId, $command->name);
$this->sensitiveDataManager->setSensitiveData(
new Broadway\BroadwaySensitiveData\EventHandling\SensitiveData(['password' => $command->password])
);
$this->repository->save($invitation);
}
}