Passed
Push — dev ( f29cfd...d7eff6 )
by Janko
11:00
created

StationShowStrategy::doConstructionStuff()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 18.4094

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 14
nc 4
nop 3
dl 0
loc 23
ccs 3
cts 16
cp 0.1875
crap 18.4094
rs 9.4888
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Station\View\ShowStation;
6
7
use Override;
0 ignored issues
show
Bug introduced by
The type Override 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...
8
use request;
9
use Stu\Component\Spacecraft\SpacecraftModuleTypeEnum;
10
use Stu\Component\Spacecraft\SpacecraftRumpEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft\SpacecraftRumpEnum 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...
11
use Stu\Component\Station\StationUtilityInterface;
12
use Stu\Module\Colony\Lib\ColonyLibFactoryInterface;
13
use Stu\Module\Control\GameControllerInterface;
14
use Stu\Component\Game\ModuleViewEnum;
15
use Stu\Module\Control\ViewContext;
16
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperFactoryInterface;
17
use Stu\Module\Ship\Lib\ShipWrapperInterface;
18
use Stu\Module\Spacecraft\View\ShowSpacecraft\ShowSpacecraft;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Spacecraft\Vi...acecraft\ShowSpacecraft 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...
19
use Stu\Module\Spacecraft\View\ShowSpacecraft\SpacecraftTypeShowStragegyInterface;
20
use Stu\Module\Station\Lib\StationLoaderInterface;
21
use Stu\Orm\Entity\ConstructionProgressInterface;
22
use Stu\Orm\Entity\StationInterface;
23
use Stu\Orm\Entity\StationShipRepairInterface;
24
use Stu\Orm\Repository\ShipyardShipQueueRepositoryInterface;
25
use Stu\Orm\Repository\StationShipRepairRepositoryInterface;
26
27
final class StationShowStrategy implements SpacecraftTypeShowStragegyInterface
28
{
29 1
    public function __construct(
30
        private StationLoaderInterface $stationLoader,
31
        private StationShipRepairRepositoryInterface $stationShipRepairRepository,
32
        private ShipyardShipQueueRepositoryInterface $shipyardShipQueueRepository,
33
        private StationUtilityInterface $stationUtility,
34
        private SpacecraftWrapperFactoryInterface $spacecraftWrapperFactory,
35
        private ColonyLibFactoryInterface $colonyLibFactory,
36 1
    ) {}
37
38 1
    #[Override]
39
    public function appendNavigationPart(GameControllerInterface $game): SpacecraftTypeShowStragegyInterface
40
    {
41 1
        $game->appendNavigationPart('station.php', _('Stationen'));
42
43 1
        return $this;
44
    }
45
46 1
    #[Override]
47
    public function setTemplateVariables(int $spacecraftId, GameControllerInterface $game): SpacecraftTypeShowStragegyInterface
48
    {
49 1
        $station = $this->stationLoader->getByIdAndUser(
50 1
            $spacecraftId,
51 1
            $game->getUser()->getId(),
52 1
            true,
53 1
            false
54 1
        );
55
56 1
        $game->setTemplateVar('STATION', $station);
57
58 1
        $progress =  $station->getConstructionProgress();
59 1
        if ($progress !== null && $progress->getRemainingTicks() !== 0) {
60
            $dockedWorkbees = $this->stationUtility->getDockedWorkbeeCount($station);
61
            $neededWorkbees = $this->stationUtility->getNeededWorkbeeCount($station, $station->getRump());
62
63
            $game->setTemplateVar('CONSTRUCTION_PROGRESS_WRAPPER', new ConstructionProgressWrapper(
64
                $progress,
65
                $station,
66
                $dockedWorkbees,
67
                $neededWorkbees
68
            ));
69
        }
70
71 1
        $this->doConstructionStuff($station, $progress, $game);
72 1
        $this->doStationStuff($station, $game);
73
74 1
        return $this;
75
    }
76
77 1
    private function doConstructionStuff(StationInterface $station, ?ConstructionProgressInterface $progress, GameControllerInterface $game): void
78
    {
79 1
        if (!$station->isConstruction()) {
80 1
            return;
81
        }
82
83
        if ($progress === null || $progress->getRemainingTicks() === 0) {
84
            $plans = $this->stationUtility->getStationBuildplansByUser($game->getUser()->getId());
85
            $game->setTemplateVar('POSSIBLE_STATIONS', $plans);
86
87
            $moduleSelectors = [];
88
            foreach ($plans as $plan) {
89
                $ms = $this->colonyLibFactory->createModuleSelector(
90
                    SpacecraftModuleTypeEnum::SPECIAL,
91
                    $station,
92
                    $plan->getRump(),
93
                    $game->getUser()
94
                );
95
96
                $moduleSelectors[] = $ms;
97
            }
98
99
            $game->setTemplateVar('MODULE_SELECTORS', $moduleSelectors);
100
        }
101
    }
102
103 1
    private function doStationStuff(StationInterface $station, GameControllerInterface $game): void
104
    {
105 1
        if ($this->stationUtility->canManageShips($station)) {
106 1
            $game->setTemplateVar('CAN_MANAGE', true);
107
        }
108
109 1
        if ($this->stationUtility->canRepairShips($station)) {
110
            $game->setTemplateVar('CAN_REPAIR', true);
111
112
            $shipRepairProgress = array_map(
113
                fn(StationShipRepairInterface $repair): ShipWrapperInterface => $this->spacecraftWrapperFactory->wrapShip($repair->getShip()),
114
                $this->stationShipRepairRepository->getByStation(
115
                    $station->getId()
116
                )
117
            );
118
119
            $game->setTemplateVar('SHIP_REPAIR_PROGRESS', $shipRepairProgress);
120
        }
121
122 1
        if ($station->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_SHIPYARD) {
123
            $game->setTemplateVar('SHIP_BUILD_PROGRESS', $this->shipyardShipQueueRepository->getByShipyard($station->getId()));
124
        }
125
126 1
        $firstOrbitShip = null;
127
128 1
        $dockedShips = $station->getDockedShips();
129 1
        if (!$dockedShips->isEmpty()) {
130
            // if selected, return the current target
131 1
            $target = request::postInt('target');
132
133 1
            if ($target !== 0) {
134
                foreach ($dockedShips as $ship) {
135
                    if ($ship->getId() === $target) {
136
                        $firstOrbitShip = $ship;
137
                    }
138
                }
139
            }
140 1
            if ($firstOrbitShip === null) {
0 ignored issues
show
introduced by
The condition $firstOrbitShip === null is always true.
Loading history...
141 1
                $firstOrbitShip = $dockedShips->first();
142
            }
143
        }
144
145 1
        $game->setTemplateVar('FIRST_MANAGE_SHIP', $firstOrbitShip !== null ? $this->spacecraftWrapperFactory->wrapShip($firstOrbitShip) : null);
146
147 1
        if ($station->getRump()->isShipyard()) {
148
            $game->setTemplateVar('AVAILABLE_BUILDPLANS', $this->stationUtility->getShipyardBuildplansByUser($game->getUser()->getId()));
149
        }
150
    }
151 1
    #[Override]
152
    public function getViewContext(): ViewContext
153
    {
154 1
        return new ViewContext(ModuleViewEnum::STATION, ShowSpacecraft::VIEW_IDENTIFIER);
155
    }
156
}
157