Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

FleetDeactivateWarp::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\FleetDeactivateWarp;
6
7
use request;
8
use RuntimeException;
9
use Stu\Component\Ship\System\ShipSystemTypeEnum;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\ActivatorDeactivatorHelperInterface;
13
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertReactionFacadeInterface;
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\Entity\ShipInterface;
18
19
final class FleetDeactivateWarp implements ActionControllerInterface
20
{
21
    public const ACTION_IDENTIFIER = 'B_FLEET_DEACTIVATE_WARP';
22
23
    private ActivatorDeactivatorHelperInterface $helper;
24
25
    private AlertReactionFacadeInterface $alertReactionFacade;
26
27
    private ShipLoaderInterface $shipLoader;
28
29
    public function __construct(
30
        ActivatorDeactivatorHelperInterface $helper,
31
        ShipLoaderInterface $shipLoader,
32
        AlertReactionFacadeInterface $alertReactionFacade
33
    ) {
34
        $this->helper = $helper;
35
        $this->alertReactionFacade = $alertReactionFacade;
36
        $this->shipLoader = $shipLoader;
37
    }
38
39
    public function handle(GameControllerInterface $game): void
40
    {
41
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
42
            request::indInt('id'),
43
            $game->getUser()->getId()
44
        );
45
46
        $success =  $this->helper->deactivateFleet(
47
            $wrapper,
48
            ShipSystemTypeEnum::SYSTEM_WARPDRIVE,
49
            $game
50
        );
51
52
        if ($success) {
53
            $ship = $wrapper->get();
54
            $tractoredShips = $this->getTractoredShipWrappers($wrapper);
55
56
            //Alarm-Rot check for fleet
57
            $this->alertReactionFacade->doItAll($wrapper, $game);
58
59
            //Alarm-Rot check for tractored ships
60
            foreach ($tractoredShips as [$tractoringShipWrapper, $tractoredShipWrapper]) {
61
                $this->alertReactionFacade->doItAll($tractoredShipWrapper, $game, $tractoringShipWrapper);
62
            }
63
64
            if ($ship->isDestroyed()) {
65
                return;
66
            }
67
        }
68
69
        $game->setView(ShowShip::VIEW_IDENTIFIER);
70
    }
71
72
    /** @return array<int, array{0: ShipInterface, 1: ShipWrapperInterface}> */
73
    private function getTractoredShipWrappers(ShipWrapperInterface $leader): array
74
    {
75
        /** @var array<int, array{0: ShipInterface, 1: ShipWrapperInterface}> */
76
        $result = [];
77
78
        $fleetWrapper = $leader->getFleetWrapper();
79
        if ($fleetWrapper === null) {
80
            throw new RuntimeException('this should not happen');
81
        }
82
83
        foreach ($fleetWrapper->getShipWrappers() as $wrapper) {
84
85
            $tractoredWrapper = $wrapper->getTractoredShipWrapper();
86
            if ($tractoredWrapper !== null) {
87
                $result[] = [$wrapper->get(), $tractoredWrapper];
88
            }
89
        }
90
91
        return $result;
92
    }
93
94
    public function performSessionCheck(): bool
95
    {
96
        return true;
97
    }
98
}
99