Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

SpacecraftConfigurator::setSpacecraftName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Module\Spacecraft\Lib\Creation;
4
5
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...
6
use RuntimeException;
7
use Stu\Component\Spacecraft\SpacecraftAlertStateEnum;
8
use Stu\Component\Spacecraft\System\SpacecraftSystemModeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Spacecraft...pacecraftSystemModeEnum 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\System\SpacecraftSystemTypeEnum;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Crew\Lib\CrewCreatorInterface;
12
use Stu\Module\Spacecraft\Lib\ActivatorDeactivatorHelperInterface;
13
use Stu\Module\Spacecraft\Lib\Torpedo\ShipTorpedoManagerInterface;
14
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
15
use Stu\Orm\Entity\LocationInterface;
16
use Stu\Orm\Repository\CrewAssignmentRepositoryInterface;
17
use Stu\Orm\Repository\SpacecraftRepositoryInterface;
18
use Stu\Orm\Repository\TorpedoTypeRepositoryInterface;
19
20
/**
21
 * @template T of SpacecraftWrapperInterface
22
 * 
23
 * @implements SpacecraftConfiguratorInterface<T>
24
 */
25
class SpacecraftConfigurator implements SpacecraftConfiguratorInterface
26
{
27
    /**
28
     * @psalm-param T $wrapper
29
     */
30
    public function __construct(
31
        private SpacecraftWrapperInterface $wrapper,
32
        private TorpedoTypeRepositoryInterface $torpedoTypeRepository,
33
        private ShipTorpedoManagerInterface $torpedoManager,
34
        private CrewCreatorInterface $crewCreator,
35
        private CrewAssignmentRepositoryInterface $shipCrewRepository,
36
        private SpacecraftRepositoryInterface $spacecraftRepository,
37
        private ActivatorDeactivatorHelperInterface $activatorDeactivatorHelper,
38
        private GameControllerInterface $game
39
    ) {}
40
41
    #[Override]
42
    public function setLocation(LocationInterface $location): SpacecraftConfiguratorInterface
43
    {
44
        $this->wrapper->get()->setLocation($location);
45
46
        return $this;
47
    }
48
49
    #[Override]
50
    public function loadEps(int $percentage): SpacecraftConfiguratorInterface
51
    {
52
        $epsSystem = $this->wrapper->getEpsSystemData();
53
54
        if ($epsSystem !== null) {
55
            $epsSystem
56
                ->setEps((int)floor($epsSystem->getTheoreticalMaxEps() / 100 * $percentage))
57
                ->update();
58
        }
59
60
        return $this;
61
    }
62
63
    #[Override]
64
    public function loadBattery(int $percentage): SpacecraftConfiguratorInterface
65
    {
66
        $epsSystem = $this->wrapper->getEpsSystemData();
67
68
        if ($epsSystem !== null) {
69
            $epsSystem
70
                ->setBattery((int)floor($epsSystem->getMaxBattery() / 100 * $percentage))
71
                ->update();
72
        }
73
74
        return $this;
75
    }
76
77
    #[Override]
78
    public function loadReactor(int $percentage): SpacecraftConfiguratorInterface
79
    {
80
        $reactor = $this->wrapper->getReactorWrapper();
81
        if ($reactor !== null) {
82
            $reactor->setLoad((int)floor($reactor->getCapacity() / 100 * $percentage));
83
        }
84
85
        return $this;
86
    }
87
88
    #[Override]
89
    public function loadWarpdrive(int $percentage): SpacecraftConfiguratorInterface
90
    {
91
        $warpdrive = $this->wrapper->getWarpDriveSystemData();
92
        if ($warpdrive !== null) {
93
            $warpdrive
94
                ->setWarpDrive((int)floor($warpdrive->getMaxWarpdrive() / 100 * $percentage))
95
                ->update();
96
        }
97
98
        return $this;
99
    }
100
101
    #[Override]
102
    public function maxOutSystems(): SpacecraftConfiguratorInterface
103
    {
104
        $this->loadEps(100)
105
            ->loadReactor(100)
106
            ->loadWarpdrive(100)
107
            ->loadBattery(100);
108
109
        $ship = $this->wrapper->get();
110
111
        $ship->setShield($ship->getMaxShield());
112
113
        return $this;
114
    }
115
116
    #[Override]
117
    public function createCrew(): SpacecraftConfiguratorInterface
118
    {
119
        $ship = $this->wrapper->get();
120
121
        $buildplan = $ship->getBuildplan();
122
        if ($buildplan !== null) {
123
            $crewAmount = $buildplan->getCrew();
124
            for ($j = 1; $j <= $crewAmount; $j++) {
125
                $crewAssignment = $this->crewCreator->create($ship->getUser()->getId());
126
                $crewAssignment->setSpacecraft($ship);
127
                $this->shipCrewRepository->save($crewAssignment);
128
129
                $ship->getCrewAssignments()->add($crewAssignment);
130
            }
131
132
            if ($crewAmount > 0) {
133
                $ship->getShipSystem(SpacecraftSystemTypeEnum::SYSTEM_LIFE_SUPPORT)->setMode(SpacecraftSystemModeEnum::MODE_ALWAYS_ON);
134
            }
135
        }
136
137
        return $this;
138
    }
139
140
    #[Override]
141
    public function setAlertState(SpacecraftAlertStateEnum $alertState): SpacecraftConfiguratorInterface
142
    {
143
        $this->activatorDeactivatorHelper->setAlertState(
144
            $this->wrapper,
145
            $alertState,
146
            $this->game
147
        );
148
149
        return $this;
150
    }
151
152
    #[Override]
153
    public function setTorpedo(?int $torpedoTypeId = null): SpacecraftConfiguratorInterface
154
    {
155
        $ship = $this->wrapper->get();
156
        if ($ship->getMaxTorpedos() === 0) {
157
            return $this;
158
        }
159
160
        $ship = $this->wrapper->get();
161
162
        if ($torpedoTypeId !== null) {
163
            $torpedoType = $this->torpedoTypeRepository->find($torpedoTypeId);
164
            if ($torpedoType === null) {
165
                throw new RuntimeException(sprintf('torpedoTypeId %d does not exist', $torpedoTypeId));
166
            }
167
        } else {
168
            $torpedoLevel = $ship->getRump()->getTorpedoLevel();
169
            if ($torpedoLevel === 0) {
170
                return $this;
171
            }
172
173
            $torpedoTypes = $this->torpedoTypeRepository->getByLevel($torpedoLevel);
174
            if ($torpedoTypes === []) {
175
                return $this;
176
            }
177
            shuffle($torpedoTypes);
178
179
            $torpedoType = current($torpedoTypes);
180
        }
181
182
        $this->torpedoManager->changeTorpedo($this->wrapper, $ship->getMaxTorpedos(), $torpedoType);
183
184
        return $this;
185
    }
186
187
    #[Override]
188
    public function setSpacecraftName(string $name): SpacecraftConfiguratorInterface
189
    {
190
        $ship = $this->wrapper->get();
191
        $ship->setName($name);
192
193
        return $this;
194
    }
195
196
    #[Override]
197
    public function finishConfiguration()
198
    {
199
        $this->spacecraftRepository->save($this->wrapper->get());
200
201
        return $this->wrapper;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->wrapper returns the type Stu\Module\Spacecraft\Li...cecraftWrapperInterface which is incompatible with the return type mandated by Stu\Module\Spacecraft\Li...::finishConfiguration() of Stu\Module\Spacecraft\Lib\Creation\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
202
    }
203
}
204