Passed
Pull Request — master (#1686)
by Nico
31:24
created

AcceptApplication   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 101
ccs 0
cts 45
cp 0
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A performSessionCheck() 0 3 1
A cancelOtherApplications() 0 20 4
A handle() 0 41 5
A __construct() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Alliance\Action\AcceptApplication;
6
7
use Stu\Exception\AccessViolation;
8
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface;
9
use Stu\Module\Alliance\View\Applications\Applications;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
13
use Stu\Module\PlayerSetting\Lib\UserEnum;
14
use Stu\Orm\Entity\AllianceJobInterface;
15
use Stu\Orm\Repository\AllianceJobRepositoryInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
final class AcceptApplication implements ActionControllerInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    public const ACTION_IDENTIFIER = 'B_ACCEPT_APPLICATION';
24
25
    private AcceptApplicationRequestInterface $acceptApplicationRequest;
26
27
    private AllianceJobRepositoryInterface $allianceJobRepository;
28
29
    private AllianceActionManagerInterface $allianceActionManager;
30
31
    private PrivateMessageSenderInterface $privateMessageSender;
32
33
    private UserRepositoryInterface $userRepository;
34
35
    public function __construct(
36
        AcceptApplicationRequestInterface $acceptApplicationRequest,
37
        AllianceJobRepositoryInterface $allianceJobRepository,
38
        AllianceActionManagerInterface $allianceActionManager,
39
        PrivateMessageSenderInterface $privateMessageSender,
40
        UserRepositoryInterface $userRepository
41
    ) {
42
        $this->acceptApplicationRequest = $acceptApplicationRequest;
43
        $this->allianceJobRepository = $allianceJobRepository;
44
        $this->allianceActionManager = $allianceActionManager;
45
        $this->privateMessageSender = $privateMessageSender;
46
        $this->userRepository = $userRepository;
47
    }
48
49
    public function handle(GameControllerInterface $game): void
50
    {
51
        $game->setView(Applications::VIEW_IDENTIFIER);
52
53
        $userId = $game->getUser()->getId();
54
55
        $alliance = $game->getUser()->getAlliance();
56
57
        if ($alliance === null) {
58
            throw new AccessViolation();
59
        }
60
61
        if (!$this->allianceActionManager->mayEdit($alliance, $game->getUser())) {
62
            throw new AccessViolation();
63
        }
64
65
        $appl = $this->allianceJobRepository->find($this->acceptApplicationRequest->getApplicationId());
66
        if ($appl === null || $appl->getAlliance()->getId() !== $alliance->getId()) {
67
            throw new AccessViolation();
68
        }
69
70
        $applicant = $appl->getUser();
71
        $applicant->setAlliance($alliance);
72
        $applicationsOfUser = $this->allianceJobRepository->getByUser($applicant->getId());
73
74
        $this->cancelOtherApplications($applicationsOfUser, $appl);
75
76
        $this->userRepository->save($applicant);
77
78
        $this->allianceJobRepository->delete($appl);
79
80
        $text = sprintf(
81
            _('Deine Bewerbung wurde akzeptiert - Du bist jetzt Mitglied der Allianz %s'),
82
            $alliance->getName()
83
        );
84
85
        $alliance->getMembers()->add($appl->getUser());
86
87
        $this->privateMessageSender->send($userId, $applicant->getId(), $text);
88
89
        $game->addInformation(_('Die Bewerbung wurde angenommen'));
90
    }
91
92
    /** @param array<AllianceJobInterface> $applications */
93
    private function cancelOtherApplications(array $applications, AllianceJobInterface $currentApplication): void
94
    {
95
        $text = sprintf(
96
            'Der Siedler %s wurde bei einer anderen Allianz aufgenommen',
97
            $currentApplication->getUser()->getName()
98
        );
99
100
        foreach ($applications as $application) {
101
            if ($application === $currentApplication) {
102
                continue;
103
            }
104
105
            $alliance = $application->getAlliance();
106
107
            $this->privateMessageSender->send(UserEnum::USER_NOONE, $alliance->getFounder()->getUserId(), $text);
108
            if ($alliance->getSuccessor() !== null) {
109
                $this->privateMessageSender->send(UserEnum::USER_NOONE, $alliance->getSuccessor()->getUserId(), $text);
110
            }
111
112
            $this->allianceJobRepository->delete($application);
113
        }
114
    }
115
116
    public function performSessionCheck(): bool
117
    {
118
        return true;
119
    }
120
}
121