Passed
Push — master ( 6e6431...f544cb )
by Nico
41:19 queued 16:38
created

ShowScan::handle()   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 79
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 52
nc 12
nop 1
dl 0
loc 79
ccs 0
cts 56
cp 0
crap 132
rs 6.9006
c 0
b 0
f 0

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\Ship\View\ShowScan;
6
7
use request;
8
use Stu\Module\Control\GameControllerInterface;
9
use Stu\Module\Control\ViewControllerInterface;
10
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface;
13
use Stu\Module\Ship\Lib\ShipLoaderInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
use Stu\Module\Ship\View\ShowShip\ShowShip;
16
use Stu\Orm\Entity\ShipInterface;
17
18
final class ShowScan implements ViewControllerInterface
19
{
20
    public const VIEW_IDENTIFIER = 'SHOW_SCAN';
21
22
    private ShipLoaderInterface $shipLoader;
23
24
    private InteractionCheckerInterface $interactionChecker;
25
26
    private PrivateMessageSenderInterface $privateMessageSender;
27
28
    public function __construct(
29
        ShipLoaderInterface $shipLoader,
30
        InteractionCheckerInterface $interactionChecker,
31
        PrivateMessageSenderInterface $privateMessageSender
32
    ) {
33
        $this->shipLoader = $shipLoader;
34
        $this->interactionChecker = $interactionChecker;
35
        $this->privateMessageSender = $privateMessageSender;
36
    }
37
38
    public function handle(GameControllerInterface $game): void
39
    {
40
        $user = $game->getUser();
41
42
        $shipId = request::indInt('id');
43
        $targetId = request::getIntFatal('target');
44
45
        $wrappers = $this->shipLoader->getWrappersBySourceAndUserAndTarget(
46
            $shipId,
47
            $user->getId(),
48
            $targetId
49
        );
50
51
        $wrapper = $wrappers->getSource();
52
        $ship = $wrapper->get();
53
54
        $game->setMacroInAjaxWindow('html/shipmacros.xhtml/entity_not_available');
55
56
        $targetWrapper = $wrappers->getTarget();
57
        if ($targetWrapper === null) {
58
            return;
59
        }
60
61
        $target = $targetWrapper->get();
62
        if ($target->getCloakState()) {
63
            return;
64
        }
65
66
        $game->setPageTitle(_('Scan'));
67
        $game->setMacroInAjaxWindow('html/shipmacros.xhtml/show_ship_scan');
68
        if (!$this->interactionChecker->checkPosition($ship, $target)) {
69
            $game->addInformation(_('Das Schiff befindet sich nicht in diesem Sektor'));
70
            return;
71
        }
72
73
        $epsSystem = $wrapper->getEpsSystemData();
74
        if ($epsSystem === null || $epsSystem->getEps() < 1) {
75
            $game->addInformation("Nicht genügend Energie vorhanden (1 benötigt)");
76
            return;
77
        }
78
79
        $epsSystem->lowerEps(1)->update();
80
81
        if ($target->getDatabaseId() !== 0) {
82
            $game->checkDatabaseItem($target->getDatabaseId());
83
        }
84
        if ($target->getRump()->getDatabaseId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $target->getRump()->getDatabaseId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
85
            $game->checkDatabaseItem($target->getRump()->getDatabaseId());
86
        }
87
88
        $href = sprintf('ship.php?%s=1&id=%d', ShowShip::VIEW_IDENTIFIER, $target->getId());
89
90
        $this->privateMessageSender->send(
91
            $game->getUser()->getId(),
92
            $target->getUser()->getId(),
93
            sprintf(
94
                _('Die %s von Spieler %s hat %s %s bei %s gescannt.'),
95
                $ship->getName(),
96
                $game->getUser()->getName(),
97
                $target->isBase() ? 'deine Station' : 'dein Schiff',
98
                $target->getName(),
99
                $target->getSectorString()
100
            ),
101
            $target->isBase() ? PrivateMessageFolderSpecialEnum::PM_SPECIAL_STATION : PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP,
102
            $href
103
        );
104
105
        $game->setTemplateVar('TARGETWRAPPER', $targetWrapper);
106
        $game->setTemplateVar('SHIELD_PERCENTAGE', $this->calculateShieldPercentage($target));
107
        $game->setTemplateVar('REACTOR_PERCENTAGE', $this->calculateReactorPercentage($targetWrapper));
108
        $game->setTemplateVar('SHIP', $ship);
109
110
        $tradePostCrewCount = null;
111
        $targetTradePost = $target->getTradePost();
112
113
        if ($targetTradePost !== null) {
114
            $tradePostCrewCount = $targetTradePost->getCrewCountOfUser($user);
115
        }
116
        $game->setTemplateVar('TRADE_POST_CREW_COUNT', $tradePostCrewCount);
117
    }
118
119
    private function calculateShieldPercentage(ShipInterface $target): int
120
    {
121
        return $target->getMaxShield() === 0
122
            ? 0
123
            : (int)ceil($target->getShield() / $target->getMaxShield() * 100);
124
    }
125
126
    private function calculateReactorPercentage(ShipWrapperInterface $wrapper): ?int
127
    {
128
        $reactor = $wrapper->getReactorWrapper();
129
        if ($reactor === null) {
130
            return null;
131
        }
132
133
        return $reactor->getCapacity() === 0
134
            ? 0
135
            : (int)ceil($reactor->getLoad() / $reactor->getCapacity() * 100);
136
    }
137
}
138