Test Failed
Push — master ( ab043e...0f6c77 )
by Nico
50:13 queued 22:41
created

unsetInboxReference()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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