Passed
Push — dev ( b851e4...f48036 )
by Janko
48:24 queued 21:52
created

AbstractWeaponPhase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 12
dl 0
loc 25
ccs 12
cts 12
cp 1
crap 1
rs 9.9
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Battle\Weapon;
6
7
use Stu\Component\Building\BuildingManagerInterface;
8
use Stu\Component\Ship\ShipModuleTypeEnum;
9
use Stu\Component\Ship\System\ShipSystemManagerInterface;
10
use Stu\Lib\Pirate\Component\PirateWrathManagerInterface;
11
use Stu\Module\Control\StuRandom;
12
use Stu\Module\History\Lib\EntryCreatorInterface;
13
use Stu\Module\Logging\LoggerUtilFactoryInterface;
14
use Stu\Module\Logging\LoggerUtilInterface;
15
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
16
use Stu\Module\PlayerSetting\Lib\UserEnum;
17
use Stu\Module\Prestige\Lib\CreatePrestigeLogInterface;
18
use Stu\Module\Ship\Lib\Battle\Provider\AttackerInterface;
19
use Stu\Module\Ship\Lib\Damage\ApplyDamageInterface;
20
use Stu\Module\Ship\Lib\ModuleValueCalculatorInterface;
21
use Stu\Module\Ship\Lib\ShipRemoverInterface;
22
use Stu\Orm\Entity\ModuleInterface;
23
use Stu\Orm\Entity\ShipInterface;
24
use Stu\Orm\Entity\UserInterface;
25
use Stu\Orm\Repository\WeaponRepositoryInterface;
26
27
abstract class AbstractWeaponPhase
28
{
29
    protected LoggerUtilInterface $loggerUtil;
30
31 1
    public function __construct(
32
        protected ShipSystemManagerInterface $shipSystemManager,
33
        protected WeaponRepositoryInterface $weaponRepository,
34
        protected EntryCreatorInterface $entryCreator,
35
        protected ShipRemoverInterface $shipRemover,
36
        protected ApplyDamageInterface $applyDamage,
37
        protected ModuleValueCalculatorInterface $moduleValueCalculator,
38
        protected BuildingManagerInterface $buildingManager,
39
        protected CreatePrestigeLogInterface $createPrestigeLog,
40
        protected PrivateMessageSenderInterface $privateMessageSender,
41
        protected StuRandom $stuRandom,
42
        private PirateWrathManagerInterface $pirateWrathManager,
43
        LoggerUtilFactoryInterface $loggerUtilFactory
44
    ) {
45 1
        $this->shipSystemManager = $shipSystemManager;
46 1
        $this->weaponRepository = $weaponRepository;
47 1
        $this->entryCreator = $entryCreator;
48 1
        $this->shipRemover = $shipRemover;
49 1
        $this->applyDamage = $applyDamage;
50 1
        $this->moduleValueCalculator = $moduleValueCalculator;
51 1
        $this->buildingManager = $buildingManager;
52 1
        $this->createPrestigeLog = $createPrestigeLog;
53 1
        $this->privateMessageSender = $privateMessageSender;
54 1
        $this->stuRandom = $stuRandom;
55 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
56
    }
57
58 1
    public function handleDestruction(
59
        AttackerInterface $attacker,
60
        ShipInterface $target,
61
        bool $isAlertRed
62
    ): void {
63 1
        if ($isAlertRed) {
64
            $this->entryCreator->addEntry(
65
                '[b][color=red]Alarm-Rot:[/color][/b] Die ' . $target->getName() . ' (' . $target->getRump()->getName() . ') wurde in Sektor ' . $target->getSectorString() . ' von der ' . $attacker->getName() . ' zerstört',
66
                $attacker->getUser()->getId(),
67
                $target
68
            );
69
        } else {
70 1
            $entryMsg = sprintf(
71 1
                'Die %s (%s) wurde in Sektor %s von der %s zerstört',
72 1
                $target->getName(),
73 1
                $target->getRump()->getName(),
74 1
                $target->getSectorString(),
75 1
                $attacker->getName()
76 1
            );
77 1
            $this->entryCreator->addEntry(
78 1
                $entryMsg,
79 1
                $attacker->getUser()->getId(),
80 1
                $target
81 1
            );
82
        }
83
84 1
        $this->checkForPrestige($attacker->getUser(), $target);
85 1
        $this->descreasePirateWrath($attacker->getUser(), $target->getUser());
86
    }
87
88 1
    public function checkForPrestige(UserInterface $destroyer, ShipInterface $target): void
89
    {
90 1
        $rump = $target->getRump();
91 1
        $amount = $rump->getPrestige();
92
93
        // nothing to do
94 1
        if ($amount === 0) {
95 1
            return;
96
        }
97
98
        // empty escape pods to five times negative prestige
99
        if ($rump->isEscapePods() && $target->getCrewCount() === 0) {
100
            $amount *= 5;
101
        }
102
103
        $description = sprintf(
104
            '%s%d%s Prestige erhalten für die Zerstörung von: %s',
105
            $amount < 0 ? '[b][color=red]' : '',
106
            $amount,
107
            $amount < 0 ? '[/color][/b]' : '',
108
            $rump->getName()
109
        );
110
111
        $this->createPrestigeLog->createLog($amount, $description, $destroyer, time());
112
113
        // system pm only for negative prestige
114
        if ($amount < 0) {
115
            $this->sendSystemMessage($description, $destroyer->getId());
116
        }
117
    }
118
119 1
    private function descreasePirateWrath(UserInterface $attacker, UserInterface $targetUser): void
120
    {
121 1
        if ($attacker->getId() !== UserEnum::USER_NPC_KAZON) {
122 1
            return;
123
        }
124
125
        $this->pirateWrathManager->decreaseWrath($targetUser, 2);
126
    }
127
128
    private function sendSystemMessage(string $description, int $userId): void
129
    {
130
        $this->privateMessageSender->send(
131
            UserEnum::USER_NOONE,
132
            $userId,
133
            $description
134
        );
135
    }
136
137 1
    protected function getModule(ShipInterface $ship, ShipModuleTypeEnum $moduleType): ?ModuleInterface
138
    {
139 1
        $buildplan = $ship->getBuildplan();
140 1
        if ($buildplan === null) {
141 1
            return null;
142
        }
143
144
        $buildplanModule = current($buildplan->getModulesByType($moduleType));
145
        if (!$buildplanModule) {
146
            return null;
147
        }
148
149
        return $buildplanModule->getModule();
150
    }
151
}
152