Passed
Push — dev ( a8d822...a0d6e4 )
by Janko
09:58 queued 04:46
created

RandomSystemEntry::getDestinationCoordinates()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 12.7161

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 10
nop 2
dl 0
loc 27
ccs 9
cts 21
cp 0.4286
crap 12.7161
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Spacecraft\Lib\Movement\Route;
6
7
use RuntimeException;
8
use Stu\Component\Map\DirectionEnum;
9
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
10
use Stu\Orm\Entity\StarSystem;
11
use Stu\Orm\Entity\StarSystemMap;
12
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
13
14
class RandomSystemEntry implements RandomSystemEntryInterface
15
{
16 1
    public function __construct(private StarSystemMapRepositoryInterface $starSystemMapRepository) {}
17
18 1
    #[\Override]
19
    public function getRandomEntryPoint(SpacecraftWrapperInterface $wrapper, StarSystem $system): StarSystemMap
20
    {
21 1
        [$posx, $posy] = $this->getDestinationCoordinates($wrapper, $system);
22
23
        // the destination starsystem map field
24 1
        $starsystemMap = $this->starSystemMapRepository->getByCoordinates($system->getId(), $posx, $posy);
25
26 1
        if ($starsystemMap === null) {
27
            throw new RuntimeException('starsystem map is missing');
28
        }
29
30 1
        return $starsystemMap;
31
    }
32
33
    /**
34
     * @return array{0: int,1: int}
35
     */
36 1
    private function getDestinationCoordinates(SpacecraftWrapperInterface $wrapper, StarSystem $system): array
37
    {
38 1
        $flightDirection = $wrapper->getComputerSystemDataMandatory()->getFlightDirection();
39 1
        while ($flightDirection === DirectionEnum::NON) {
40 1
            $flightDirection = DirectionEnum::from(random_int(1, 4));
41
        }
42
43
        switch ($flightDirection) {
44 1
            case DirectionEnum::BOTTOM:
45 1
                $posx = random_int(1, $system->getMaxX());
46 1
                $posy = 1;
47 1
                break;
48
            case DirectionEnum::TOP:
49
                $posx = random_int(1, $system->getMaxX());
50
                $posy = $system->getMaxY();
51
                break;
52
            case DirectionEnum::RIGHT:
53
                $posx = 1;
54
                $posy = random_int(1, $system->getMaxY());
55
                break;
56
            case DirectionEnum::LEFT:
57
                $posx = $system->getMaxX();
58
                $posy = random_int(1, $system->getMaxY());
59
                break;
60
        }
61
62 1
        return [$posx, $posy];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $posy does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $posx does not seem to be defined for all execution paths leading up to this point.
Loading history...
63
    }
64
}
65