Test Failed
Push — dev ( d9ea74...46b33e )
by Nico
09:52
created

CreateRelation::handle()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 63
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 37
c 1
b 1
f 0
nc 9
nop 1
dl 0
loc 63
ccs 0
cts 39
cp 0
crap 110
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Alliance\Action\CreateRelation;
6
7
use Stu\Component\Alliance\AllianceEnum;
8
use Stu\Component\Alliance\Event\DiplomaticRelationProposedEvent;
9
use Stu\Component\Alliance\Event\WarDeclaredEvent;
10
use Stu\Exception\AccessViolation;
11
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface;
12
use Stu\Module\Control\ActionControllerInterface;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Orm\Repository\AllianceRelationRepositoryInterface;
15
use Stu\Orm\Repository\AllianceRepositoryInterface;
16
17
final class CreateRelation implements ActionControllerInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    public const ACTION_IDENTIFIER = 'B_NEW_RELATION';
23
24
    private CreateRelationRequestInterface $createRelationRequest;
25
26
    private AllianceRelationRepositoryInterface $allianceRelationRepository;
27
28
    private AllianceActionManagerInterface $allianceActionManager;
29
30
    private AllianceRepositoryInterface $allianceRepository;
31
32
    public function __construct(
33
        CreateRelationRequestInterface $createRelationRequest,
34
        AllianceRelationRepositoryInterface $allianceRelationRepository,
35
        AllianceActionManagerInterface $allianceActionManager,
36
        AllianceRepositoryInterface $allianceRepository
37
    ) {
38
        $this->createRelationRequest = $createRelationRequest;
39
        $this->allianceRelationRepository = $allianceRelationRepository;
40
        $this->allianceActionManager = $allianceActionManager;
41
        $this->allianceRepository = $allianceRepository;
42
    }
43
44
    public function handle(GameControllerInterface $game): void
45
    {
46
        $alliance = $game->getUser()->getAlliance();
47
48
        if ($alliance === null) {
49
            throw new AccessViolation();
50
        }
51
52
        $allianceId = $alliance->getId();
53
        $user = $game->getUser();
54
55
        if (!$this->allianceActionManager->mayManageForeignRelations($alliance, $user)) {
56
            throw new AccessViolation();
57
        }
58
59
        $counterpartId = $this->createRelationRequest->getCounterpartId();
60
        $typeId = $this->createRelationRequest->getRelationType();
61
62
        $counterpart = $this->allianceRepository->find($counterpartId);
63
        if (
64
            $counterpart === null
65
            || $alliance === $counterpart
66
            || !in_array($typeId, AllianceEnum::ALLOWED_RELATION_TYPES, true)
67
        ) {
68
            return;
69
        }
70
71
        $cnt = $this->allianceRelationRepository->getPendingCountByAlliances($allianceId, $counterpartId);
72
        if ($cnt >= 2) {
73
            $game->addInformation('Es gibt bereits ein Angebot für diese Allianz');
74
            return;
75
        }
76
77
        // check if a relation exists
78
        $existingRelations = $this->allianceRelationRepository->getByAlliancePair($allianceId, $counterpartId);
79
80
        // Iteriere durch die gefundenen Einträge
81
        foreach ($existingRelations as $existingRelation) {
82
            $existingRelationType = $existingRelation->getType();
83
84
            if ($existingRelationType === $typeId) {
85
                return;
86
            }
87
        }
88
89
        if ($typeId === AllianceEnum::ALLIANCE_RELATION_WAR) {
90
            $game->triggerEvent(new WarDeclaredEvent(
91
                $alliance,
92
                $counterpart,
93
                $user
94
            ));
95
96
            $game->addInformation(
97
                sprintf('Der Allianz %s wurde der Krieg erklärt', $counterpart->getName())
98
            );
99
        } else {
100
            $game->triggerEvent(new DiplomaticRelationProposedEvent(
101
                $alliance,
102
                $counterpart,
103
                $typeId
104
            ));
105
106
            $game->addInformation('Das Abkommen wurde angeboten');
107
        }
108
    }
109
110
    public function performSessionCheck(): bool
111
    {
112
        return false;
113
    }
114
}