Passed
Push — master ( 0a90eb...366c47 )
by Nico
23:43
created

SplitReactorOutput::addGameInfo()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\SplitReactorOutput;
6
7
use request;
8
use Stu\Component\Ship\System\Data\WarpDriveSystemData;
9
use Stu\Exception\SanityCheckException;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\ShipLoaderInterface;
13
use Stu\Module\Ship\View\ShowInformation\ShowInformation;
14
15
final class SplitReactorOutput implements ActionControllerInterface
16
{
17
    public const ACTION_IDENTIFIER = 'B_SPLIT_REACTOR_OUTPUT';
18
19
    private ShipLoaderInterface $shipLoader;
20
21
    public function __construct(
22
        ShipLoaderInterface $shipLoader
23
    ) {
24
        $this->shipLoader = $shipLoader;
25
    }
26
27
    public function handle(GameControllerInterface $game): void
28
    {
29
        $userId = $game->getUser()->getId();
30
31
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
32
            request::indInt('id'),
33
            $userId
34
        );
35
36
        $systemData = $wrapper->getWarpDriveSystemData();
37
        if ($systemData === null) {
38
            throw new SanityCheckException('no warpdrive in fleet leader', self::ACTION_IDENTIFIER);
39
        }
40
41
        $game->setView(ShowInformation::VIEW_IDENTIFIER);
42
43
        $warpsplit = request::postInt('value');
44
        if ($warpsplit < 0) {
45
            $warpsplit = 0;
46
        }
47
        if ($warpsplit > 100) {
48
            $warpsplit = 100;
49
        }
50
51
        $isFleet = request::postIntFatal('fleet') === 1;
52
        $autoCarryOver = request::postIntFatal('autocarryover') === 1;
53
54
        $fleetWrapper = $wrapper->getFleetWrapper();
55
        if ($isFleet && $fleetWrapper !== null) {
56
57
            foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
58
                $systemData = $wrapper->getWarpDriveSystemData();
59
                if ($systemData !== null) {
60
                    $this->setValues($systemData, $warpsplit, $autoCarryOver);
61
                }
62
            }
63
            $this->addGameInfo(true, $warpsplit, $autoCarryOver, $game);
64
            return;
65
        }
66
67
        $this->setValues($systemData, $warpsplit, $autoCarryOver);
68
        $this->addGameInfo(false, $warpsplit, $autoCarryOver, $game);
69
    }
70
71
    private function setValues(WarpDriveSystemData $systemData, int $split, bool $autoCarryOver): void
72
    {
73
        $systemData
74
            ->setWarpDriveSplit($split)
75
            ->setAutoCarryOver($autoCarryOver)
76
            ->update();
77
    }
78
79
    private function addGameInfo(bool $isFleet, int $warpsplit, bool $autoCarryOver, GameControllerInterface $game): void
80
    {
81
        $game->addInformation(sprintf(
82
            _('%sReaktorleistung geht zu %d Prozent in den Warpantrieb (Übertrag %s)'),
83
            $isFleet ? 'Flottenbefehl ausgeführt: ' : '',
84
            100 - $warpsplit,
85
            $autoCarryOver ? 'aktiviert' : 'deaktiviert'
86
        ));
87
    }
88
89
    public function performSessionCheck(): bool
90
    {
91
        return true;
92
    }
93
}
94