Passed
Push — dev ( b01017...737777 )
by Janko
05:10
created

AbstractJoinFleet   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 17.31%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 89
ccs 9
cts 52
cp 0.1731
rs 10
c 0
b 0
f 0
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isTholianWebPreventing() 0 13 5
B tryToAddToFleet() 0 60 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Action\JoinFleet;
6
7
use request;
8
use Stu\Component\Game\GameEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Game\GameEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Stu\Exception\AccessViolationException;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Logging\LoggerUtilFactoryInterface;
12
use Stu\Module\Logging\LoggerUtilInterface;
13
use Stu\Module\Spacecraft\Lib\Interaction\InteractionCheckerInterface;
14
use Stu\Module\Ship\Lib\ShipLoaderInterface;
15
use Stu\Orm\Entity\Ship;
16
use Stu\Orm\Repository\FleetRepositoryInterface;
17
18
abstract class AbstractJoinFleet
19
{
20
    protected LoggerUtilInterface $loggerUtil;
21
22 1
    public function __construct(
23
        private FleetRepositoryInterface $fleetRepository,
24
        protected ShipLoaderInterface $shipLoader,
25
        private InteractionCheckerInterface $interactionChecker,
26
        LoggerUtilFactoryInterface $loggerUtilFactory
27
    ) {
28 1
        $this->loggerUtil = $loggerUtilFactory->getLoggerUtil();
29
    }
30
31 1
    public function tryToAddToFleet(Ship $ship, GameControllerInterface $game): void
32
    {
33 1
        $fleetId = request::indInt('fleetid');
34 1
        $fleet = $this->fleetRepository->find($fleetId);
35
36 1
        if ($fleet === null || $fleet->getUserId() !== $game->getUser()->getId()) {
37
            throw new AccessViolationException();
38
        }
39
40 1
        if ($ship->getFleet() !== null) {
41 1
            $game->getInfo()->addInformationf(_('%s: Das Schiff ist bereits in einer Flotte.'), $ship->getName());
42 1
            return;
43
        }
44
45
        if ($ship->getTakeoverPassive() !== null) {
46
            $game->getInfo()->addInformationf(_('%s: Schiffsübernahme verhindert den Beitritt.'), $ship->getName());
47
            return;
48
        }
49
50
        if ($this->isTholianWebPreventing($fleet->getLeadShip(), $ship)) {
51
            $game->getInfo()->addInformationf(_('%s: Ein Energienetz verhindert den Beitritt.'), $ship->getName());
52
            return;
53
        }
54
55
        if ($ship->isStation()) {
56
            return;
57
        }
58
59
        if ($fleet->getLeadShip()->getId() === $ship->getId()) {
60
            return;
61
        }
62
        if (!$this->interactionChecker->checkPosition($fleet->getLeadShip(), $ship)) {
63
            return;
64
        }
65
        if ($ship->isTractored()) {
66
            $game->getInfo()->addInformationf(
67
                _('%s: Aktion nicht möglich, da Schiff von einem Traktorstrahl gehalten wird.'),
68
                $ship->getName(),
69
            );
70
            return;
71
        }
72
        $newCrewAmount = $fleet->getCrewSum() + ($ship->getBuildplan()?->getCrew() ?? 0);
73
        if ($newCrewAmount > GameEnum::CREW_PER_FLEET) {
74
            $game->getInfo()->addInformation(sprintf(
75
                _('%s: Es sind maximal %d Crew pro Flotte möglich'),
76
                $ship->getName(),
77
                GameEnum::CREW_PER_FLEET
78
            ));
79
            return;
80
        }
81
        $ship->setFleet($fleet);
82
83
        $fleet->getShips()->add($ship);
84
85
        $this->shipLoader->save($ship);
86
87
        $game->getInfo()->addInformation(sprintf(
88
            _('Die %s ist der Flotte %s beigetreten'),
89
            $ship->getName(),
90
            $fleet->getName()
91
        ));
92
    }
93
94
    private function isTholianWebPreventing(Ship $fleetLeader, Ship $ship): bool
95
    {
96
        $fleetLeaderWeb = $fleetLeader->getHoldingWeb();
97
        $shipWeb = $ship->getHoldingWeb();
98
99
        if ($fleetLeaderWeb === $shipWeb) {
100
            return false;
101
        }
102
103
        if ($fleetLeaderWeb !== null && $fleetLeaderWeb->isFinished()) {
104
            return true;
105
        }
106
        return $shipWeb !== null && $shipWeb->isFinished();
107
    }
108
}
109