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

CheckDestination::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 1
ccs 1
cts 1
cp 1
crap 1
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 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\Exception\SanityCheckException;
10
use Stu\Orm\Entity\MapInterface;
11
use Stu\Orm\Entity\ShipInterface;
12
use Stu\Orm\Entity\StarSystemMapInterface;
13
use Stu\Orm\Repository\MapRepositoryInterface;
14
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
15
16
final class CheckDestination implements CheckDestinationInterface
17
{
18 14
    public function __construct(private MapRepositoryInterface $mapRepository, private StarSystemMapRepositoryInterface $starSystemMapRepository) {}
19
20 14
    #[Override]
21
    public function validate(
22
        ShipInterface $ship,
23
        int $destinationX,
24
        int $destinationY
25
    ): MapInterface|StarSystemMapInterface {
26 14
        $start = $ship->getLocation();
27
28 14
        if ($start->getX() !== $destinationX && $start->getY() !== $destinationY) {
29 2
            throw new SanityCheckException(
30 2
                sprintf(
31 2
                    'userId %d tried to navigate from %s to invalid position %d|%d',
32 2
                    $ship->getUser()->getId(),
33 2
                    $start->getSectorString(),
34 2
                    $destinationX,
35 2
                    $destinationY
36 2
                )
37 2
            );
38
        }
39 12
        if ($destinationX < 1) {
40
            $destinationX = 1;
41
        }
42 12
        if ($destinationY < 1) {
43
            $destinationY = 1;
44
        }
45 12
        if ($start instanceof StarSystemMapInterface) {
46
47 6
            $system = $start->getSystem();
48 6
            if ($destinationX > $system->getMaxX()) {
49 1
                $destinationX = $system->getMaxX();
50
            }
51 6
            if ($destinationY > $system->getMaxY()) {
52 1
                $destinationY = $system->getMaxY();
53
            }
54
55 6
            $result = $this->starSystemMapRepository->getByCoordinates($system->getId(), $destinationX, $destinationY);
56
        } else {
57 6
            $layer = $start->getLayer();
58 6
            if ($layer === null) {
59
                throw new RuntimeException('this should not happen');
60
            }
61
62 6
            if ($destinationX > $layer->getWidth()) {
63 1
                $destinationX = $layer->getWidth();
64
            }
65 6
            if ($destinationY > $layer->getHeight()) {
66 1
                $destinationY = $layer->getHeight();
67
            }
68
69 6
            $result = $this->mapRepository->getByCoordinates($layer, $destinationX, $destinationY);
70
        }
71
72 12
        if ($result === null) {
73 2
            throw new RuntimeException(sprintf('destination %d|%d does not exist', $destinationX, $destinationY));
74
        }
75
76 10
        return $result;
77
    }
78
}
79