|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Component\Player\Deletion\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Stu\Orm\Entity\UserInterface; |
|
8
|
|
|
use Stu\Orm\Repository\PrivateMessageRepositoryInterface; |
|
9
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Updates the sending user to the fallback one on user deletion |
|
13
|
|
|
*/ |
|
14
|
|
|
final class PrivateMessageDeletionHandler implements PlayerDeletionHandlerInterface |
|
15
|
|
|
{ |
|
16
|
3 |
|
public function __construct( |
|
17
|
|
|
private UserRepositoryInterface $userRepository, |
|
18
|
|
|
private PrivateMessageRepositoryInterface $privateMessageRepository, |
|
19
|
|
|
) { |
|
20
|
3 |
|
} |
|
21
|
|
|
|
|
22
|
3 |
|
public function delete(UserInterface $user): void |
|
23
|
|
|
{ |
|
24
|
3 |
|
$nobody = $this->userRepository->getFallbackUser(); |
|
25
|
|
|
|
|
26
|
3 |
|
$this->setFallbackUserByDeletedSender($user, $nobody); |
|
27
|
3 |
|
$this->unsetInboxReference($user, $nobody); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
private function setFallbackUserByDeletedSender(UserInterface $user, UserInterface $nobody): void |
|
31
|
|
|
{ |
|
32
|
3 |
|
foreach ($this->privateMessageRepository->getBySender($user) as $pm) { |
|
33
|
1 |
|
$pm->setSender($nobody); |
|
34
|
|
|
|
|
35
|
1 |
|
$this->privateMessageRepository->save($pm); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
private function unsetInboxReference(UserInterface $user, UserInterface $nobody): void |
|
40
|
|
|
{ |
|
41
|
3 |
|
foreach ($this->privateMessageRepository->getByReceiver($user) as $pm) { |
|
42
|
|
|
|
|
43
|
2 |
|
$pm->setRecipient($nobody); |
|
44
|
2 |
|
$this->privateMessageRepository->save($pm); |
|
45
|
|
|
|
|
46
|
2 |
|
$outboxPm = $pm->getOutboxPm(); |
|
47
|
2 |
|
if ($outboxPm !== null) { |
|
48
|
1 |
|
$outboxPm->setInboxPm(null); |
|
49
|
1 |
|
$this->privateMessageRepository->save($outboxPm); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|