Passed
Push — master ( a46ba1...b3484f )
by Nico
48:50 queued 24:23
created

Transwarp::isSanityCheckFaultyConcrete()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 63
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 34
c 0
b 0
f 0
nc 11
nop 2
dl 0
loc 63
ccs 0
cts 35
cp 0
crap 156
rs 6.9666

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\Action\Transwarp;
6
7
use request;
8
use Stu\Exception\SanityCheckException;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Message\Lib\DistributedMessageSenderInterface;
11
use Stu\Module\Ship\Action\MoveShip\AbstractDirectedMovement;
12
use Stu\Module\Ship\Action\MoveShip\MoveShipRequestInterface;
13
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteFactoryInterface;
14
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteInterface;
15
use Stu\Module\Ship\Lib\Movement\ShipMoverInterface;
16
use Stu\Module\Ship\Lib\ShipLoaderInterface;
17
use Stu\Module\Ship\Lib\ShipWrapperInterface;
18
use Stu\Orm\Entity\MapInterface;
19
use Stu\Orm\Repository\MapRepositoryInterface;
20
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
21
22
final class Transwarp extends AbstractDirectedMovement
23
{
24
    public const ACTION_IDENTIFIER = 'B_TRANSWARP';
25
26
    private MapRepositoryInterface $mapRepository;
27
28
    private MapInterface $destination;
29
30
    public function __construct(
31
        MoveShipRequestInterface $moveShipRequest,
32
        ShipLoaderInterface $shipLoader,
33
        ShipMoverInterface $shipMover,
34
        FlightRouteFactoryInterface $flightRouteFactory,
35
        StarSystemMapRepositoryInterface $starSystemMapRepository,
36
        DistributedMessageSenderInterface $distributedMessageSender,
37
        MapRepositoryInterface $mapRepository
38
    ) {
39
        parent::__construct(
40
            $moveShipRequest,
41
            $shipLoader,
42
            $shipMover,
43
            $flightRouteFactory,
44
            $starSystemMapRepository,
45
            $distributedMessageSender
46
        );
47
48
        $this->mapRepository = $mapRepository;
49
    }
50
51
    protected function isSanityCheckFaultyConcrete(ShipWrapperInterface $wrapper, GameControllerInterface $game): bool
52
    {
53
        $layerId = request::postIntFatal('transwarplayer');
54
55
        //sanity check if user knows layer
56
        if (!$game->getUser()->hasSeen($layerId)) {
57
            return true;
58
        }
59
60
        if ($wrapper->get()->getTranswarpCooldown() !== null) {
61
            return true;
62
        }
63
64
        // target check
65
        $cx = request::postInt('transwarpcx');
66
        $cy = request::postInt('transwarpcy');
67
68
        if (!$cx || !$cy) {
69
            $game->addInformation(_('Zielkoordinaten müssen angegeben werden'));
70
            return true;
71
        }
72
73
        $ship = $wrapper->get();
74
75
        if ($ship->getSystem() !== null) {
76
            $game->addInformation(_('Transwarp kann nur außerhalb von Systemen genutzt werden'));
77
            return true;
78
        }
79
80
        if (!$ship->getWarpState()) {
81
            $game->addInformation(_('Der Warpantrieb muss aktiviert sein'));
82
            return true;
83
        }
84
85
        if ($ship->isTractoring()) {
86
            $game->addInformation(_('Transwarpflug nicht möglich bei aktiviertem Traktorstrahl'));
87
            return true;
88
        }
89
90
        if ($ship->getFleet() !== null) {
91
            $game->addInformation(_('Transwarpflug nicht möglich wenn Teil einer Flotte'));
92
            return true;
93
        }
94
95
        $map = $this->mapRepository->getByCoordinates($layerId, $cx, $cy);
96
97
        if ($map->getLayer()->isHidden()) {
98
            throw new SanityCheckException('tried to access hidden layer');
99
        }
100
101
        if ($map === null) {
102
            $game->addInformation(_('Zielkoordinaten existieren nicht'));
103
            return true;
104
        }
105
106
        if (!$map->getFieldType()->getPassable()) {
107
            $game->addInformation(_('Zielkoordinaten können nicht angeflogen werden'));
108
            return true;
109
        }
110
111
        $this->destination = $map;
112
113
        return false;
114
    }
115
116
    protected function getFlightRoute(ShipWrapperInterface $wrapper): FlightRouteInterface
117
    {
118
        return $this->flightRouteFactory->getRouteForMapDestination($this->destination, true);
119
    }
120
}
121