Passed
Push — dev ( 302539...72decd )
by Janko
05:12
created

RandomSystemEntry::getDestinationCoordinates()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.8343

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 10
nop 2
dl 0
loc 27
ccs 12
cts 21
cp 0.5714
crap 8.8343
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 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...
8
use RuntimeException;
9
use Stu\Component\Map\DirectionEnum;
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
use Stu\Orm\Entity\StarSystem;
12
use Stu\Orm\Entity\StarSystemMap;
13
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
14
15
class RandomSystemEntry implements RandomSystemEntryInterface
16
{
17 1
    public function __construct(private StarSystemMapRepositoryInterface $starSystemMapRepository) {}
18
19 1
    #[Override]
20
    public function getRandomEntryPoint(SpacecraftWrapperInterface $wrapper, StarSystem $system): StarSystemMap
21
    {
22 1
        [$posx, $posy] = $this->getDestinationCoordinates($wrapper, $system);
23
24
        // the destination starsystem map field
25 1
        $starsystemMap = $this->starSystemMapRepository->getByCoordinates($system->getId(), $posx, $posy);
26
27 1
        if ($starsystemMap === null) {
28
            throw new RuntimeException('starsystem map is missing');
29
        }
30
31 1
        return $starsystemMap;
32
    }
33
34
    /**
35
     * @return array{0: int,1: int}
36
     */
37 1
    private function getDestinationCoordinates(SpacecraftWrapperInterface $wrapper, StarSystem $system): array
38
    {
39 1
        $flightDirection = $wrapper->getComputerSystemDataMandatory()->getFlightDirection();
40 1
        while ($flightDirection === DirectionEnum::NON) {
41 1
            $flightDirection = DirectionEnum::from(random_int(1, 4));
42
        }
43
44
        switch ($flightDirection) {
45 1
            case DirectionEnum::BOTTOM:
46
                $posx = random_int(1, $system->getMaxX());
47
                $posy = 1;
48
                break;
49 1
            case DirectionEnum::TOP:
50
                $posx = random_int(1, $system->getMaxX());
51
                $posy = $system->getMaxY();
52
                break;
53 1
            case DirectionEnum::RIGHT:
54
                $posx = 1;
55
                $posy = random_int(1, $system->getMaxY());
56
                break;
57 1
            case DirectionEnum::LEFT:
58 1
                $posx = $system->getMaxX();
59 1
                $posy = random_int(1, $system->getMaxY());
60 1
                break;
61
        }
62
63 1
        return [$posx, $posy];
0 ignored issues
show
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...
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...
64
    }
65
}
66