Passed
Push — master ( 6d8b29...d7e053 )
by Janko
27:40
created

FlightRoute::isRouteDangerous()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Movement\Route;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
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...
10
use RuntimeException;
11
use Stu\Module\Ship\Lib\Message\MessageCollectionInterface;
12
use Stu\Module\Ship\Lib\Movement\Component\Consequence\FlightConsequenceInterface;
13
use Stu\Module\Ship\Lib\ShipWrapperInterface;
14
use Stu\Orm\Entity\LocationInterface;
15
use Stu\Orm\Entity\MapInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
use Stu\Orm\Entity\StarSystemMapInterface;
18
use Stu\Orm\Entity\WormholeEntryInterface;
19
20
final class FlightRoute implements FlightRouteInterface
21
{
22
    //Members
23
    private bool $isTraversing = false;
24
25
    private RouteModeEnum $routeMode = RouteModeEnum::ROUTE_MODE_FLIGHT;
26
27
    private LocationInterface $current;
28
29
    private ?WormholeEntryInterface $wormholeEntry = null;
30
31
    /**
32
     * @var Collection<int, LocationInterface>
33
     */
34
    private Collection $waypoints;
35
36
    /**
37
     * @param array<string, FlightConsequenceInterface> $flightConsequences
38
     * @param array<string, FlightConsequenceInterface> $postFlightConsequences
39
     */
40 19
    public function __construct(
41
        private CheckDestinationInterface $checkDestination,
42
        private LoadWaypointsInterface $loadWaypoints,
43
        private EnterWaypointInterface $enterWaypoint,
44
        private array $flightConsequences,
45
        private array $postFlightConsequences,
46
    ) {
47 19
        $this->waypoints = new ArrayCollection();
48
    }
49
50 8
    #[Override]
51
    public function setDestination(
52
        MapInterface|StarSystemMapInterface $destination,
53
        bool $isTranswarp
54
    ): FlightRouteInterface {
55 8
        $this->waypoints->add($destination);
56
57 8
        if ($destination instanceof MapInterface) {
58 6
            if ($isTranswarp) {
59 1
                $this->routeMode = RouteModeEnum::ROUTE_MODE_TRANSWARP;
60
            } else {
61 5
                $this->routeMode = RouteModeEnum::ROUTE_MODE_SYSTEM_EXIT;
62
            }
63
        } else {
64 2
            $this->routeMode = RouteModeEnum::ROUTE_MODE_SYSTEM_ENTRY;
65
        }
66
67 8
        return $this;
68
    }
69
70 2
    #[Override]
71
    public function setDestinationViaWormhole(WormholeEntryInterface $wormholeEntry, bool $isEntry): FlightRouteInterface
72
    {
73 2
        $this->wormholeEntry = $wormholeEntry;
74 2
        if ($isEntry) {
75 1
            $this->waypoints->add($wormholeEntry->getSystemMap());
76 1
            $this->routeMode = RouteModeEnum::ROUTE_MODE_WORMHOLE_ENTRY;
77
        } else {
78 1
            $this->waypoints->add($wormholeEntry->getMap());
79 1
            $this->routeMode = RouteModeEnum::ROUTE_MODE_WORMHOLE_EXIT;
80
        }
81
82 2
        return $this;
83
    }
84
85 6
    #[Override]
86
    public function setDestinationViaCoordinates(ShipInterface $ship, int $x, int $y): FlightRouteInterface
87
    {
88 6
        $start = $ship->getLocation();
89 6
        $this->current = $start;
90 6
        $destination = $this->checkDestination->validate($ship, $x, $y);
91
92 6
        if ($start !== $destination) {
0 ignored issues
show
introduced by
The condition $start !== $destination is always true.
Loading history...
93 5
            $this->waypoints = $this->loadWaypoints->load($start, $destination);
94 5
            $this->isTraversing = true;
95
        }
96
97 6
        return $this;
98
    }
99
100
    #[Override]
101
    public function getCurrentWaypoint(): LocationInterface
102
    {
103
        return $this->current;
104
    }
105
106 8
    #[Override]
107
    public function getNextWaypoint(): LocationInterface
108
    {
109 8
        if ($this->waypoints->isEmpty()) {
110 1
            throw new RuntimeException('isDestinationArrived has to be called beforehand');
111
        }
112
113 7
        return $this->waypoints->first();
114
    }
115
116 6
    #[Override]
117
    public function stepForward(): void
118
    {
119 6
        $first =  $this->waypoints->first();
120
121 6
        if (!$first) {
122 1
            return;
123
        }
124
125 6
        $this->current = $first;
126 6
        $this->waypoints->removeElement($this->current);
127
    }
128
129 1
    #[Override]
130
    public function abortFlight(): void
131
    {
132 1
        $this->waypoints->clear();
133
    }
134
135 6
    #[Override]
136
    public function enterNextWaypoint(
137
        ShipWrapperInterface $wrapper,
138
        MessageCollectionInterface $messages
139
    ): void {
140
141
        // flight consequences
142 6
        $this->walkConsequences($this->flightConsequences, $wrapper, $messages);
143
144
        // enter waypoint
145 6
        $this->enterWaypoint->enterNextWaypoint(
146 6
            $wrapper->get(),
147 6
            $this->isTraversing,
148 6
            $this->getNextWaypoint(),
149 6
            $this->wormholeEntry
150 6
        );
151
152
        // post flight consequences
153 6
        $this->walkConsequences($this->postFlightConsequences, $wrapper, $messages);
154
    }
155
156
    /**
157
     * @param array<string, FlightConsequenceInterface> $consequences
158
     */
159 6
    private function walkConsequences(
160
        array $consequences,
161
        ShipWrapperInterface $wrapper,
162
        MessageCollectionInterface $messages
163
    ): void {
164 6
        array_walk(
165 6
            $consequences,
166 6
            fn(FlightConsequenceInterface $consequence) => $consequence->trigger($wrapper, $this, $messages)
167 6
        );
168
    }
169
170 8
    #[Override]
171
    public function isDestinationArrived(): bool
172
    {
173 8
        return $this->waypoints->isEmpty();
174
    }
175
176 6
    #[Override]
177
    public function getRouteMode(): RouteModeEnum
178
    {
179 6
        return $this->routeMode;
180
    }
181
182
    #[Override]
183
    public function isTraversing(): bool
184
    {
185
        return $this->isTraversing;
186
    }
187
188
    #[Override]
189
    public function isImpulseDriveNeeded(): bool
190
    {
191
        $routeMode = $this->routeMode;
192
193
        if (
194
            $routeMode === RouteModeEnum::ROUTE_MODE_SYSTEM_ENTRY
195
            || $routeMode === RouteModeEnum::ROUTE_MODE_WORMHOLE_ENTRY
196
        ) {
197
            return true;
198
        }
199
200
        return $routeMode === RouteModeEnum::ROUTE_MODE_FLIGHT
201
            && $this->getNextWaypoint() instanceof StarSystemMapInterface;
202
    }
203
204
    #[Override]
205
    public function isWarpDriveNeeded(): bool
206
    {
207
        $routeMode = $this->routeMode;
208
209
        if (
210
            $routeMode === RouteModeEnum::ROUTE_MODE_SYSTEM_EXIT
211
            || $routeMode === RouteModeEnum::ROUTE_MODE_TRANSWARP
212
        ) {
213
            return true;
214
        }
215
216
        return $routeMode === RouteModeEnum::ROUTE_MODE_FLIGHT
217
            && $this->getNextWaypoint() instanceof MapInterface;
218
    }
219
220
    #[Override]
221
    public function isTranswarpCoilNeeded(): bool
222
    {
223
        return $this->routeMode === RouteModeEnum::ROUTE_MODE_TRANSWARP;
224
    }
225
226 2
    #[Override]
227
    public function hasSpecialDamageOnField(): bool
228
    {
229 2
        foreach ($this->waypoints as $waypoint) {
230 2
            if ($waypoint->getFieldType()->getSpecialDamage() > 0) {
231 1
                return true;
232
            }
233
        }
234
235 1
        return false;
236
    }
237
238 2
    #[Override]
239
    public function isDestinationInAdminRegion(array $regionIds): bool
240
    {
241 2
        $destination = $this->waypoints->last();
242
243 2
        return $destination instanceof MapInterface
244 2
            && in_array($destination->getAdminRegionId(), $regionIds);
245
    }
246
247 4
    #[Override]
248
    public function isDestinationAtTradepost(): bool
249
    {
250 4
        $destination = $this->waypoints->last();
251
252 4
        return $destination instanceof MapInterface
253 4
            && $destination->getShips()->exists(fn(int $key, ShipInterface $ship): bool => $ship->getTradePost() !== null);
254
    }
255
}
256