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

LoadWaypoints   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 60
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
B load() 0 56 8
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 InvalidArgumentException;
10
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...
11
use RuntimeException;
12
use Stu\Orm\Entity\LocationInterface;
13
use Stu\Orm\Entity\MapInterface;
14
use Stu\Orm\Entity\StarSystemMapInterface;
15
use Stu\Orm\Repository\MapRepositoryInterface;
16
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
17
18
final class LoadWaypoints implements LoadWaypointsInterface
19
{
20 10
    public function __construct(private MapRepositoryInterface $mapRepository, private StarSystemMapRepositoryInterface $starSystemMapRepository) {}
21
22 10
    #[Override]
23
    public function load(
24
        LocationInterface $start,
25
        LocationInterface $destination
26
    ): Collection {
27 10
        if ($start instanceof MapInterface !== $destination instanceof MapInterface) {
28 2
            throw new InvalidArgumentException('start and destination have different type');
29
        }
30
31 8
        $startX = $start->getX();
32 8
        $startY = $start->getY();
33
34 8
        $destinationX = $destination->getX();
35 8
        $destinationY = $destination->getY();
36
37 8
        $sortAscending = true;
38
39 8
        if ($startY > $destinationY) {
40 2
            $sortAscending = false;
41
        }
42 8
        if ($startX > $destinationX) {
43 2
            $sortAscending = false;
44
        }
45 8
        if ($start instanceof StarSystemMapInterface) {
46 4
            $waypoints = $this->starSystemMapRepository->getByCoordinateRange(
47 4
                $start->getSystem(),
48 4
                min($startX, $destinationX),
49 4
                max($startX, $destinationX),
50 4
                min($startY, $destinationY),
51 4
                max($startY, $destinationY),
52 4
                $sortAscending
53 4
            );
54
        } else {
55 4
            $layer = $start->getLayer();
56 4
            if ($layer === null) {
57
                throw new RuntimeException('this should not happen');
58
            }
59 4
            $waypoints = $this->mapRepository->getByCoordinateRange(
60 4
                $layer->getId(),
61 4
                min($startX, $destinationX),
62 4
                max($startX, $destinationX),
63 4
                min($startY, $destinationY),
64 4
                max($startY, $destinationY),
65 4
                $sortAscending
66 4
            );
67
        }
68
69 8
        $result = new ArrayCollection();
70
71 8
        foreach ($waypoints as $waypoint) {
72 8
            if ($waypoint !== $start) {
73 8
                $result->add($waypoint);
74
            }
75
        }
76
77 8
        return $result;
78
    }
79
}
80