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

AbstractDirectedMovement   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 55
c 0
b 0
f 0
dl 0
loc 117
ccs 53
cts 53
cp 1
rs 10
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 34 3
A __construct() 0 14 1
A performSessionCheck() 0 3 1
B isSanityCheckFaulty() 0 42 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\MoveShip;
6
7
use Stu\Module\Control\ActionControllerInterface;
8
use Stu\Module\Control\GameControllerInterface;
9
use Stu\Module\Message\Lib\DistributedMessageSenderInterface;
10
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
11
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteFactoryInterface;
12
use Stu\Module\Ship\Lib\Movement\Route\FlightRouteInterface;
13
use Stu\Module\Ship\Lib\Movement\ShipMoverInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Ship\Lib\ShipWrapperInterface;
16
use Stu\Module\Ship\View\ShowShip\ShowShip;
17
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
18
19
abstract class AbstractDirectedMovement implements ActionControllerInterface
20
{
21
    protected MoveShipRequestInterface $moveShipRequest;
22
23
    protected FlightRouteFactoryInterface $flightRouteFactory;
24
25
    protected StarSystemMapRepositoryInterface $starSystemMapRepository;
26
27
    private ShipLoaderInterface $shipLoader;
28
29
    private ShipMoverInterface $shipMover;
30
31
    private DistributedMessageSenderInterface $distributedMessageSender;
32
33 13
    public function __construct(
34
        MoveShipRequestInterface $moveShipRequest,
35
        ShipLoaderInterface $shipLoader,
36
        ShipMoverInterface $shipMover,
37
        FlightRouteFactoryInterface $flightRouteFactory,
38
        StarSystemMapRepositoryInterface $starSystemMapRepository,
39
        DistributedMessageSenderInterface $distributedMessageSender
40
    ) {
41 13
        $this->moveShipRequest = $moveShipRequest;
42 13
        $this->shipLoader = $shipLoader;
43 13
        $this->shipMover = $shipMover;
44 13
        $this->flightRouteFactory = $flightRouteFactory;
45 13
        $this->starSystemMapRepository = $starSystemMapRepository;
46 13
        $this->distributedMessageSender = $distributedMessageSender;
47
    }
48
49
    abstract protected function isSanityCheckFaultyConcrete(ShipWrapperInterface $wrapper, GameControllerInterface $game): bool;
50
51
    abstract protected function getFlightRoute(ShipWrapperInterface $wrapper): FlightRouteInterface;
52
53 13
    public function handle(GameControllerInterface $game): void
54
    {
55 13
        $userId = $game->getUser()->getId();
56
57 13
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
58 13
            $this->moveShipRequest->getShipId(),
59 13
            $userId
60 13
        );
61
62 13
        if ($this->isSanityCheckFaulty($wrapper, $game)) {
63 5
            return;
64
        }
65
66 8
        $ship = $wrapper->get();
67
68 8
        $messages = $this->shipMover->checkAndMove(
69 8
            $wrapper,
70 8
            $this->getFlightRoute($wrapper)
71 8
        );
72 8
        $game->addInformationWrapper($messages->getInformationDump());
73
74
75 8
        $this->distributedMessageSender->distributeMessageCollection(
76 8
            $messages,
77 8
            $userId,
78 8
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP
79 8
        );
80
81
82 8
        if ($ship->isDestroyed()) {
83 1
            return;
84
        }
85
86 7
        $game->setView(ShowShip::VIEW_IDENTIFIER);
87
    }
88
89 7
    public function performSessionCheck(): bool
90
    {
91 7
        return true;
92
    }
93
94 13
    private function isSanityCheckFaulty(ShipWrapperInterface $wrapper, GameControllerInterface $game): bool
95
    {
96 13
        $ship = $wrapper->get();
97
98 13
        if (!$ship->hasEnoughCrew($game)) {
99 1
            return true;
100
        }
101
102 12
        if ($ship->isTractored()) {
103 1
            $game->addInformation(_('Das Schiff wird von einem Traktorstrahl gehalten'));
104 1
            return true;
105
        }
106
107 11
        if ($ship->getHoldingWeb() !== null && $ship->getHoldingWeb()->isFinished()) {
108 1
            $game->addInformation(_('Das Schiff ist in einem Energienetz gefangen'));
109 1
            return true;
110
        }
111
112 10
        $fleet = $ship->getFleet();
113
114
        if (
115 10
            $fleet !== null
116 10
            && $ship->isFleetLeader()
117 10
            && $fleet->getDefendedColony() !== null
118
        ) {
119 1
            $game->addInformation(_('Flug während Kolonie-Verteidigung nicht möglich'));
120
121 1
            return true;
122
        }
123
124
        if (
125 9
            $fleet !== null
126 9
            && $ship->isFleetLeader()
127 9
            && $fleet->getBlockedColony() !== null
128
        ) {
129 1
            $game->addInformation(_('Flug während Kolonie-Blockierung nicht möglich'));
130
131 1
            return true;
132
        }
133
134
135 8
        return $this->isSanityCheckFaultyConcrete($wrapper, $game);
136
    }
137
}
138