Passed
Pull Request — master (#1846)
by Nico
32:16
created

LoadWaypoints   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 62
ccs 38
cts 39
cp 0.9744
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

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