Passed
Push — master ( f4068b...77f637 )
by Nico
40:27 queued 14:09
created

CancelTholianWeb::handle()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 5
nop 1
dl 0
loc 49
ccs 0
cts 31
cp 0
crap 42
rs 8.8337
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\TholianWeb;
6
7
use request;
8
use Stu\Component\Ship\ShipStateEnum;
9
use Stu\Exception\SanityCheckException;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Logging\LoggerUtilFactoryInterface;
13
use Stu\Module\Logging\LoggerUtilInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Ship\Lib\Interaction\TholianWebUtilInterface;
16
use Stu\Module\Ship\View\ShowShip\ShowShip;
17
18
final class CancelTholianWeb implements ActionControllerInterface
19
{
20
    public const ACTION_IDENTIFIER = 'B_CANCEL_WEB';
21
22
    private ShipLoaderInterface $shipLoader;
23
24
    private TholianWebUtilInterface $tholianWebUtil;
25
26
    private LoggerUtilInterface $loggerUtil;
27
28
    public function __construct(
29
        ShipLoaderInterface $shipLoader,
30
        TholianWebUtilInterface $tholianWebUtil,
31
        LoggerUtilFactoryInterface $loggerUtilFactory
32
    ) {
33
        $this->shipLoader = $shipLoader;
34
        $this->tholianWebUtil = $tholianWebUtil;
35
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
36
    }
37
38
    public function handle(GameControllerInterface $game): void
39
    {
40
        $game->setView(ShowShip::VIEW_IDENTIFIER);
41
42
        $userId = $game->getUser()->getId();
43
        $shipId = request::indInt('id');
44
45
        $wrapper = $this->shipLoader->getWrapperByIdAndUser(
46
            $shipId,
47
            $userId
48
        );
49
50
        $emitter = $wrapper->getWebEmitterSystemData();
51
52
        $this->loggerUtil->log('1');
53
        if ($emitter === null || $emitter->ownedWebId === null) {
54
            $this->loggerUtil->log('2');
55
            throw new SanityCheckException('emitter = null or no owned web', self::ACTION_IDENTIFIER);
56
        }
57
        $this->loggerUtil->log('3');
58
59
        $ship = $wrapper->get();
60
        //check if system healthy
61
        if (!$ship->isWebEmitterHealthy()) {
62
            throw new SanityCheckException('emitter not healthy', self::ACTION_IDENTIFIER);
63
        }
64
65
        $this->loggerUtil->log('5');
66
67
        $web = $emitter->getOwnedTholianWeb();
68
        if ($web === null) {
69
            throw new SanityCheckException('no own web', self::ACTION_IDENTIFIER);
70
        }
71
72
        $this->loggerUtil->log(sprintf('capturedSize: %d', count($web->getCapturedShips())));
73
        $this->loggerUtil->log('6');
74
        //unlink targets
75
        $this->tholianWebUtil->releaseAllShips($web, $wrapper->getShipWrapperFactory());
76
        $this->loggerUtil->log('7');
77
78
        if ($emitter->ownedWebId === $emitter->webUnderConstructionId) {
79
            $emitter->setWebUnderConstructionId(null);
80
        }
81
        $emitter->setOwnedWebId(null)->update();
82
83
        $ship->setState(ShipStateEnum::SHIP_STATE_NONE);
84
        $this->shipLoader->save($ship);
85
86
        $game->addInformation("Der Aufbau des Energienetz wurde abgebrochen");
87
    }
88
89
    public function performSessionCheck(): bool
90
    {
91
        return true;
92
    }
93
}
94