Passed
Push — master ( aa3b71...5e2219 )
by Nico
55:07 queued 26:20
created

InterceptShip   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 63
ccs 0
cts 34
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A performSessionCheck() 0 3 1
A __construct() 0 6 1
B handle() 0 45 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\InterceptShip;
6
7
use request;
8
use Stu\Lib\Pirate\PirateReactionInterface;
9
use Stu\Lib\Pirate\PirateReactionTriggerEnum;
10
use Stu\Module\Control\ActionControllerInterface;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\Interaction\InteractionCheckerInterface;
13
use Stu\Module\Ship\Lib\Interaction\InterceptShipCoreInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Module\Ship\View\ShowShip\ShowShip;
16
17
final class InterceptShip implements ActionControllerInterface
18
{
19
    public const ACTION_IDENTIFIER = 'B_INTERCEPT';
20
21
    public function __construct(
22
        private ShipLoaderInterface $shipLoader,
23
        private InterceptShipCoreInterface $interceptShipCore,
24
        private InteractionCheckerInterface $interactionChecker,
25
        private PirateReactionInterface $pirateReaction
26
    ) {
27
    }
28
29
    public function handle(GameControllerInterface $game): void
30
    {
31
        $game->setView(ShowShip::VIEW_IDENTIFIER);
32
33
        $userId = $game->getUser()->getId();
34
35
        $shipId = request::indInt('id');
36
        $targetId = request::indInt('target');
37
38
        $wrappers = $this->shipLoader->getWrappersBySourceAndUserAndTarget(
39
            $shipId,
40
            $userId,
41
            $targetId
42
        );
43
44
        $wrapper = $wrappers->getSource();
45
        $ship = $wrapper->get();
46
47
        $targetWrapper = $wrappers->getTarget();
48
        if ($targetWrapper === null) {
49
            return;
50
        }
51
        $target = $targetWrapper->get();
52
53
        if (!$this->interactionChecker->checkPosition($target, $ship)) {
54
            return;
55
        }
56
57
        if (!$ship->hasEnoughCrew($game)) {
58
            return;
59
        }
60
61
        if (!$target->getWarpState()) {
62
            return;
63
        }
64
        if (!$ship->canIntercept()) {
65
            return;
66
        }
67
68
        $this->interceptShipCore->intercept($ship, $target, $game);
69
70
        $this->pirateReaction->checkForPirateReaction(
71
            $target,
72
            PirateReactionTriggerEnum::ON_INTERCEPTION,
73
            $ship
74
        );
75
    }
76
77
    public function performSessionCheck(): bool
78
    {
79
        return true;
80
    }
81
}
82