Passed
Pull Request — master (#1801)
by Nico
23:50
created

unsetInboxReference()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 11
ccs 8
cts 8
cp 1
crap 3
rs 10
c 2
b 2
f 0
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