Test Failed
Push — dev ( 930c46...368876 )
by Janko
18:05
created

StateIconAndTitle::getForActivePassive()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
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 8
{
19
    public function __construct(
20
        private GameControllerInterface $game,
21 8
        private Parser $bbCodeParser
22
    ) {}
23
24
    /**
25
     * @return array<string>|null
26 11
     */
27
    public function getStateIconAndTitle(SpacecraftWrapperInterface $wrapper): ?array
28 11
    {
29 11
        $spacecraft = $wrapper->get();
30
        $state = $spacecraft->getState();
31 11
32
        return match ($state) {
33
            SpacecraftStateEnum::RETROFIT => ['buttons/konstr1', 'Schiff wird umgerüstet'],
34
            SpacecraftStateEnum::REPAIR_ACTIVE => $this->getForActiveRepair($spacecraft),
35 11
            SpacecraftStateEnum::REPAIR_PASSIVE => $this->getForActivePassive($wrapper),
36 2
            SpacecraftStateEnum::ASTRO_FINALIZING => $this->getForAstroFinalizing($wrapper),
37 2
            SpacecraftStateEnum::ACTIVE_TAKEOVER => $this->getForActiveTakeover($wrapper),
38 2
            SpacecraftStateEnum::GATHER_RESOURCES => $this->getForGatherResources($spacecraft),
39 2
            default => $this->getForPassiveTakeover($wrapper)
40 2
        };
41 2
    }
42
43
    /** @return array<string> */
44 9
    private function getForActiveRepair(SpacecraftInterface $spacecraft): array
45 2
    {
46 2
        $isStation = $spacecraft->isStation();
47 2
        return ['buttons/rep2', sprintf(
48
            '%s repariert %s',
49
            $isStation ? 'Stationscrew' : 'Schiffscrew',
50 7
            $isStation ? 'die Station' : 'das Schiff',
51
        )];
52 7
    }
53 7
54
    /** @return array<string> */
55 1
    private function getForActivePassive(SpacecraftWrapperInterface $wrapper): array
56 1
    {
57 1
        return ['buttons/rep2', sprintf(
58 1
            '%s wird repariert (noch %d Runden)',
59
            $wrapper->get()->isStation() ? 'Station' : 'Schiff',
60
            $wrapper->getRepairDuration()
61 6
        )];
62
    }
63 6
64 6
    /** @return array<string>|null */
65
    private function getForAstroFinalizing(SpacecraftWrapperInterface $wrapper): ?array
66 1
    {
67 1
        $astroLab = $wrapper instanceof ShipWrapperInterface ? $wrapper->getAstroLaboratorySystemData() : null;
68 1
        if ($astroLab === null) {
69 1
            return null;
70 1
        }
71 1
72
        return ['buttons/map1', sprintf(
73
            'Schiff kartographiert (noch %d Runden)',
74 5
            $astroLab->getAstroStartTurn() + AstronomicalMappingEnum::TURNS_TO_FINISH - $this->game->getCurrentRound()->getTurn()
75 5
        )];
76 1
    }
77 1
78 1
    /** @return array<string>|null */
79 1
    private function getForActiveTakeover(SpacecraftWrapperInterface $wrapper): ?array
80 1
    {
81 1
        $takeover = $wrapper->get()->getTakeoverActive();
82
        if ($takeover === null) {
83
            return null;
84
        }
85 4
86 4
        return ['buttons/take2', sprintf(
87
            'Schiff übernimmt die "%s" (noch %d Runden)',
88
            $this->bbCodeParser->parse($takeover->getTargetSpacecraft()->getName())->getAsText(),
89
            $wrapper->getTakeoverTicksLeft($takeover)
90
        )];
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 4
        }
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
    private function getForPassiveTakeover(SpacecraftWrapperInterface $wrapper): ?array
118
    {
119
        $takeover = $wrapper->get()->getTakeoverPassive();
120
        if ($takeover === null) {
121
            return null;
122
        }
123
124
        $sourceUserNamePlainText = $this->bbCodeParser->parse($takeover->getSourceSpacecraft()->getUser()->getName())->getAsText();
125
        return ['buttons/untake2', sprintf(
126
            'Schiff wird von Spieler "%s" übernommen (noch %d Runden)',
127
            $sourceUserNamePlainText,
128
            $wrapper->getTakeoverTicksLeft($takeover)
129
        )];
130
    }
131
}
132