Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

Transwarp::handle()   C

Complexity

Conditions 15
Paths 13

Size

Total Lines 87
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
cc 15
eloc 47
nc 13
nop 1
dl 0
loc 87
ccs 0
cts 49
cp 0
crap 240
rs 5.9166
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B Transwarp::isSanityCheckFaultyConcrete() 0 59 11
A Transwarp::getFlightRoute() 0 3 1

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
        // target check
61
        $cx = request::postInt('transwarpcx');
62
        $cy = request::postInt('transwarpcy');
63
64
        if (!$cx || !$cy) {
65
            $game->addInformation(_('Zielkoordinaten müssen angegeben werden'));
66
            return true;
67
        }
68
69
        $ship = $wrapper->get();
70
71
        if ($ship->getSystem() !== null) {
72
            $game->addInformation(_('Transwarp kann nur außerhalb von Systemen genutzt werden'));
73
            return true;
74
        }
75
76
        if (!$ship->getWarpState()) {
77
            $game->addInformation(_('Der Warpantrieb muss aktiviert sein'));
78
            return true;
79
        }
80
81
        if ($ship->isTractoring()) {
82
            $game->addInformation(_('Transwarpflug nicht möglich bei aktiviertem Traktorstrahl'));
83
            return true;
84
        }
85
86
        if ($ship->getFleet() !== null) {
87
            $game->addInformation(_('Transwarpflug nicht möglich wenn Teil einer Flotte'));
88
            return true;
89
        }
90
91
        $map = $this->mapRepository->getByCoordinates($layerId, $cx, $cy);
92
93
        if ($map->getLayer()->isHidden()) {
94
            throw new SanityCheckException('tried to access hidden layer');
95
        }
96
97
        if ($map === null) {
98
            $game->addInformation(_('Zielkoordinaten existieren nicht'));
99
            return true;
100
        }
101
102
        if (!$map->getFieldType()->getPassable()) {
103
            $game->addInformation(_('Zielkoordinaten können nicht angeflogen werden'));
104
            return true;
105
        }
106
107
        $this->destination = $map;
108
109
        return false;
110
    }
111
112
    protected function getFlightRoute(ShipWrapperInterface $wrapper): FlightRouteInterface
113
    {
114
        return $this->flightRouteFactory->getRouteForMapDestination($this->destination, true);
115
    }
116
}
117