Passed
Push — dev ( 231d4b...6ea563 )
by Janko
101:30 queued 71:11
created

StationShowStrategy::appendNavigationPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
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
        $progress =  $station->getConstructionProgress();
57 1
        if ($progress !== null && $progress->getRemainingTicks() !== 0) {
58
            $dockedWorkbees = $this->stationUtility->getDockedWorkbeeCount($station);
59
            $neededWorkbees = $this->stationUtility->getNeededWorkbeeCount($station, $station->getRump());
60
61
            $game->setTemplateVar('CONSTRUCTION_PROGRESS_WRAPPER', new ConstructionProgressWrapper(
62
                $progress,
63
                $station,
64
                $dockedWorkbees,
65
                $neededWorkbees
66
            ));
67
        }
68
69 1
        $this->doConstructionStuff($station, $progress, $game);
70 1
        $this->doStationStuff($station, $game);
71
72 1
        return $this;
73
    }
74
75 1
    private function doConstructionStuff(StationInterface $station, ?ConstructionProgressInterface $progress, GameControllerInterface $game): void
76
    {
77 1
        if (!$station->isConstruction()) {
78 1
            return;
79
        }
80
81
        if ($progress === null || $progress->getRemainingTicks() === 0) {
82
            $plans = $this->stationUtility->getStationBuildplansByUser($game->getUser()->getId());
83
            $game->setTemplateVar('POSSIBLE_STATIONS', $plans);
84
85
            $moduleSelectors = [];
86
            foreach ($plans as $plan) {
87
                $ms = $this->colonyLibFactory->createModuleSelector(
88
                    SpacecraftModuleTypeEnum::SPECIAL,
89
                    $station,
90
                    $plan->getRump(),
91
                    $game->getUser()
92
                );
93
94
                $moduleSelectors[] = $ms;
95
            }
96
97
            $game->setTemplateVar('MODULE_SELECTORS', $moduleSelectors);
98
        }
99
    }
100
101 1
    private function doStationStuff(StationInterface $station, GameControllerInterface $game): void
102
    {
103 1
        if ($this->stationUtility->canManageShips($station)) {
104 1
            $game->setTemplateVar('CAN_MANAGE', true);
105
        }
106
107 1
        if ($this->stationUtility->canRepairShips($station)) {
108
            $game->setTemplateVar('CAN_REPAIR', true);
109
110
            $shipRepairProgress = array_map(
111
                fn(StationShipRepairInterface $repair): ShipWrapperInterface => $this->spacecraftWrapperFactory->wrapShip($repair->getShip()),
112
                $this->stationShipRepairRepository->getByStation(
113
                    $station->getId()
114
                )
115
            );
116
117
            $game->setTemplateVar('SHIP_REPAIR_PROGRESS', $shipRepairProgress);
118
        }
119
120 1
        if ($station->getRump()->getRoleId() === SpacecraftRumpEnum::SHIP_ROLE_SHIPYARD) {
121
            $game->setTemplateVar('SHIP_BUILD_PROGRESS', $this->shipyardShipQueueRepository->getByShipyard($station->getId()));
122
        }
123
124 1
        $firstOrbitShip = null;
125
126 1
        $dockedShips = $station->getDockedShips();
127 1
        if (!$dockedShips->isEmpty()) {
128
            // if selected, return the current target
129 1
            $target = request::postInt('target');
130
131 1
            if ($target !== 0) {
132
                foreach ($dockedShips as $ship) {
133
                    if ($ship->getId() === $target) {
134
                        $firstOrbitShip = $ship;
135
                    }
136
                }
137
            }
138 1
            if ($firstOrbitShip === null) {
0 ignored issues
show
introduced by
The condition $firstOrbitShip === null is always true.
Loading history...
139 1
                $firstOrbitShip = $dockedShips->first();
140
            }
141
        }
142
143 1
        $game->setTemplateVar('FIRST_MANAGE_SHIP', $firstOrbitShip !== null ? $this->spacecraftWrapperFactory->wrapShip($firstOrbitShip) : null);
144
145 1
        if ($station->getRump()->isShipyard()) {
146
            $game->setTemplateVar('AVAILABLE_BUILDPLANS', $this->stationUtility->getShipyardBuildplansByUser($game->getUser()->getId()));
147
        }
148
    }
149 1
    #[Override]
150
    public function getViewContext(): ViewContext
151
    {
152 1
        return new ViewContext(ModuleViewEnum::STATION, ShowSpacecraft::VIEW_IDENTIFIER);
153
    }
154
}
155