Passed
Push — dev ( 368876...ca533f )
by Janko
16:00
created

StateIconAndTitle::getForActivePassive()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Ui;
6
7
use JBBCode\Parser;
8
use Stu\Component\Ship\AstronomicalMappingEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Ship\AstronomicalMappingEnum 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\Component\Spacecraft\SpacecraftStateEnum;
10
use Stu\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
11
use Stu\Module\Control\GameControllerInterface;
12
use Stu\Module\Ship\Lib\ShipWrapperInterface;
13
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
14
use Stu\Orm\Entity\ShipInterface;
15
use Stu\Orm\Entity\SpacecraftInterface;
16
17
class StateIconAndTitle
18
{
19 8
    public function __construct(
20
        private GameControllerInterface $game,
21
        private Parser $bbCodeParser
22 8
    ) {}
23
24
    /**
25
     * @return array<string>|null
26
     */
27 11
    public function getStateIconAndTitle(SpacecraftWrapperInterface $wrapper): ?array
28
    {
29 11
        $spacecraft = $wrapper->get();
30 11
        $state = $spacecraft->getState();
31
32
        return match ($state) {
33 11
            SpacecraftStateEnum::RETROFIT => ['buttons/konstr1', 'Schiff wird umgerüstet'],
34 11
            SpacecraftStateEnum::REPAIR_ACTIVE => $this->getForActiveRepair($spacecraft),
35 9
            SpacecraftStateEnum::REPAIR_PASSIVE => $this->getForActivePassive($wrapper),
36 7
            SpacecraftStateEnum::ASTRO_FINALIZING => $this->getForAstroFinalizing($wrapper),
37 6
            SpacecraftStateEnum::ACTIVE_TAKEOVER => $this->getForActiveTakeover($wrapper),
38 5
            SpacecraftStateEnum::GATHER_RESOURCES => $this->getForGatherResources($spacecraft),
39 11
            default => $this->getForPassiveTakeover($wrapper)
40
        };
41
    }
42
43
    /** @return array<string> */
44 2
    private function getForActiveRepair(SpacecraftInterface $spacecraft): array
45
    {
46 2
        $isStation = $spacecraft->isStation();
47 2
        return ['buttons/rep2', sprintf(
48 2
            '%s repariert %s',
49 2
            $isStation ? 'Stationscrew' : 'Schiffscrew',
50 2
            $isStation ? 'die Station' : 'das Schiff',
51 2
        )];
52
    }
53
54
    /** @return array<string> */
55 2
    private function getForActivePassive(SpacecraftWrapperInterface $wrapper): array
56
    {
57 2
        return ['buttons/rep2', sprintf(
58 2
            '%s wird repariert (noch %d Runden)',
59 2
            $wrapper->get()->isStation() ? 'Station' : 'Schiff',
60 2
            $wrapper->getRepairDuration()
61 2
        )];
62
    }
63
64
    /** @return array<string>|null */
65 1
    private function getForAstroFinalizing(SpacecraftWrapperInterface $wrapper): ?array
66
    {
67 1
        $astroLab = $wrapper instanceof ShipWrapperInterface ? $wrapper->getAstroLaboratorySystemData() : null;
68 1
        if ($astroLab === null) {
69
            return null;
70
        }
71
72 1
        return ['buttons/map1', sprintf(
73 1
            'Schiff kartographiert (noch %d Runden)',
74 1
            $astroLab->getAstroStartTurn() + AstronomicalMappingEnum::TURNS_TO_FINISH - $this->game->getCurrentRound()->getTurn()
75 1
        )];
76
    }
77
78
    /** @return array<string>|null */
79 1
    private function getForActiveTakeover(SpacecraftWrapperInterface $wrapper): ?array
80
    {
81 1
        $takeover = $wrapper->get()->getTakeoverActive();
82 1
        if ($takeover === null) {
83
            return null;
84
        }
85
86 1
        return ['buttons/take2', sprintf(
87 1
            'Schiff übernimmt die "%s" (noch %d Runden)',
88 1
            $this->bbCodeParser->parse($takeover->getTargetSpacecraft()->getName())->getAsText(),
89 1
            $wrapper->getTakeoverTicksLeft($takeover)
90 1
        )];
91
    }
92
93
    /** @return array<string>|null */
94
    private function getForGatherResources(SpacecraftInterface $spacecraft): ?array
95
    {
96
        if (!$spacecraft instanceof ShipInterface) {
97
            return null;
98
        }
99
100
        $miningqueue = $spacecraft->getMiningQueue();
101
        $module = $spacecraft->getSpacecraftSystem(SpacecraftSystemTypeEnum::BUSSARD_COLLECTOR)->getModule();
102
        if ($miningqueue === null || $module === null) {
103
            return null;
104
        }
105
106
        $locationmining = $miningqueue->getLocationMining();
107
        $gathercount = $module->getFactionId() == null ? 100 : 200;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $module->getFactionId() of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
108
109
        return [sprintf('commodities/%s', $locationmining->getCommodity()->getId()), sprintf(
110
            'Schiff sammelt Ressourcen (~%d %s/Tick)',
111
            $gathercount,
112
            $locationmining->getCommodity()->getName()
113
        )];
114
    }
115
116
    /** @return array<string>|null */
117 5
    private function getForPassiveTakeover(SpacecraftWrapperInterface $wrapper): ?array
118
    {
119 5
        $takeover = $wrapper->get()->getTakeoverPassive();
120 5
        if ($takeover === null) {
121 4
            return null;
122
        }
123
124 1
        $sourceUserNamePlainText = $this->bbCodeParser->parse($takeover->getSourceSpacecraft()->getUser()->getName())->getAsText();
125 1
        return ['buttons/untake2', sprintf(
126 1
            'Schiff wird von Spieler "%s" übernommen (noch %d Runden)',
127 1
            $sourceUserNamePlainText,
128 1
            $wrapper->getTakeoverTicksLeft($takeover)
129 1
        )];
130
    }
131
}
132