Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

AlertReactionFacade::performAttackCycle()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 58
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 40
nc 2
nop 3
dl 0
loc 58
ccs 0
cts 47
cp 0
crap 20
rs 9.28
c 1
b 0
f 0

How to fix   Long Method   

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\Ship\Lib\Battle\AlertDetection;
6
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
9
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
10
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertDetectionInterface;
11
use Stu\Module\Ship\Lib\Battle\Party\AlertedBattlePartyInterface;
12
use Stu\Module\Ship\Lib\Battle\Party\BattlePartyFactoryInterface;
13
use Stu\Module\Ship\Lib\Battle\Party\IncomingBattleParty;
14
use Stu\Module\Ship\Lib\Battle\ShipAttackCycleInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
18
final class AlertReactionFacade implements AlertReactionFacadeInterface
19
{
20
    public function __construct(
21
        private PrivateMessageSenderInterface $privateMessageSender,
22
        private ShipAttackCycleInterface $shipAttackCycle,
23
        private BattlePartyFactoryInterface $battlePartyFactory,
24
        private AlertDetectionInterface $alertDetection,
25
    ) {
26
    }
27
28
    public function doItAll(
29
        ShipWrapperInterface $incomingShipWrapper,
30
        InformationInterface $informations,
31
        ?ShipInterface $tractoringShip = null,
32
    ): void {
33
34
        $incomingBattleParty = $this->battlePartyFactory->createIncomingBattleParty($incomingShipWrapper);
35
        if ($incomingBattleParty->getActiveMembers()->isEmpty()) {
36
            return;
37
        }
38
39
        $incomingShip = $incomingShipWrapper->get();
40
        $alertedBattleParties = $this->alertDetection->detectAlertedBattleParties($incomingShip, $informations, $tractoringShip);
41
        if (empty($alertedBattleParties)) {
42
            return;
43
        }
44
45
        shuffle($alertedBattleParties);
46
        foreach ($alertedBattleParties as $alertedBattleParty) {
47
48
            if ($incomingBattleParty->isDefeated()) {
49
                break;
50
            }
51
52
            if ($alertedBattleParty->getActiveMembers()->isEmpty()) {
53
                break;
54
            }
55
56
            $this->performAttackCycle(
57
                $alertedBattleParty,
58
                $incomingBattleParty,
59
                $informations
60
            );
61
        }
62
    }
63
64
    public function performAttackCycle(
65
        AlertedBattlePartyInterface $alertedParty,
66
        IncomingBattleParty $incomingParty,
67
        InformationInterface $informations
68
    ): void {
69
        $alertUserId = $alertedParty->getUser()->getId();
70
        $incomingUserId = $incomingParty->getUser()->getId();
71
72
        $alertLeader = $alertedParty->getLeader()->get();
73
        $incomingLeader = $incomingParty->getLeader()->get();
74
75
        $messageCollection = $this->shipAttackCycle->cycle(
76
            $alertedParty,
77
            $incomingParty,
78
            $alertedParty->getAttackCause()
79
        );
80
81
        $fightInformations = $messageCollection->getInformationDump();
82
83
        $pm = sprintf(
84
            _("Eigene Schiffe auf %s, Kampf in Sektor %s\n%s"),
85
            $alertedParty->getAlertDescription(),
86
            $incomingLeader->getSectorString(),
87
            $fightInformations->getInformationsAsString()
88
        );
89
        $href = sprintf(_('ship.php?SHOW_SHIP=1&id=%d'), $alertLeader->getId());
90
        $this->privateMessageSender->send(
91
            $incomingUserId,
92
            $alertUserId,
93
            $pm,
94
            $alertedParty->isBase() ? PrivateMessageFolderSpecialEnum::PM_SPECIAL_STATION : PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP,
95
            $alertLeader->isDestroyed() ? null : $href
96
        );
97
        $pm = sprintf(
98
            _("Fremde Schiffe auf %s, Kampf in Sektor %s\n%s"),
99
            $alertedParty->getAlertDescription(),
100
            $incomingLeader->getSectorString(),
101
            $fightInformations->getInformationsAsString()
102
        );
103
        $this->privateMessageSender->send(
104
            $alertUserId,
105
            $incomingUserId,
106
            $pm,
107
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP
108
        );
109
110
        if ($incomingLeader->isDestroyed()) {
111
            $fightInformations->dumpTo($informations);
112
            return;
113
        }
114
115
        $informations->addInformation(sprintf(
116
            _('%s fremder Schiffe auf Feld %d|%d, Angriff durchgeführt') . "\n",
117
            $alertedParty->getAlertDescription(),
118
            $incomingLeader->getPosX(),
119
            $incomingLeader->getPosY()
120
        ));
121
        $fightInformations->dumpTo($informations);
122
    }
123
}
124