-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepository.php
More file actions
48 lines (42 loc) · 1.34 KB
/
UserRepository.php
File metadata and controls
48 lines (42 loc) · 1.34 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
<?php
namespace Samples\UserService\Repositories;
use Ramsey\Uuid\Uuid;
use Samples\UserService\Entities\User;
use Samples\UserService\Events\UserEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class UserRepository
{
private EventDispatcherInterface $eventDispatcher;
/**
* UserRepository constructor.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
*/
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param \Samples\UserService\Entities\User $user
* @throws \Exception
*/
public function save(User $user)
{
// save user then dispatch event
$this->eventDispatcher->dispatch(
new UserEvent(UserEvent::USER_CREATED, UserEvent::VERSION, Uuid::uuid4()->toString(), $user->toArray()),
UserEvent::USER_CREATED
);
}
/**
* @param \Samples\UserService\Entities\User $user
* @throws \Exception
*/
public function update(User $user)
{
// update user then dispatch event
$this->eventDispatcher->dispatch(
new UserEvent(UserEvent::USER_UPDATED, UserEvent::VERSION, Uuid::uuid4()->toString(), $user->toArray()),
UserEvent::USER_UPDATED
);
}
}