Passed
Push — master ( 288b46...98b0e3 )
by Nico
31:30 queued 08:00
created

EnterWaypoint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A enterNextWaypoint() 0 20 5
A isWormhole() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Movement\Route;
6
7
use Stu\Module\Control\StuTime;
8
use Stu\Orm\Entity\MapInterface;
9
use Stu\Orm\Entity\ShipInterface;
10
use Stu\Orm\Entity\StarSystemMapInterface;
11
use Stu\Orm\Entity\WormholeEntryInterface;
12
use Stu\Orm\Repository\WormholeEntryRepositoryInterface;
13
14
final class EnterWaypoint implements EnterWaypointInterface
15
{
16
    private WormholeEntryRepositoryInterface $wormholeEntryRepository;
17
18
    private StuTime $stuTime;
19
20 5
    public function __construct(
21
        WormholeEntryRepositoryInterface $wormholeEntryRepository,
22
        StuTime $stuTime
23
    ) {
24 5
        $this->wormholeEntryRepository = $wormholeEntryRepository;
25 5
        $this->stuTime = $stuTime;
26
    }
27
28 5
    public function enterNextWaypoint(
29
        ShipInterface $ship,
30
        bool $isTraversing,
31
        MapInterface|StarSystemMapInterface $waypoint,
32
        ?WormholeEntryInterface $wormholeEntry
33
    ): void {
34
35 5
        $ship->updateLocation(
36 5
            $waypoint instanceof MapInterface ? $waypoint : null,
37 5
            $waypoint instanceof StarSystemMapInterface ? $waypoint : null
38 5
        );
39
40 5
        if ($this->isWormhole($waypoint)) {
41 1
            $ship->setCx(0);
42 1
            $ship->setCy(0);
43
        }
44
45 5
        if ($wormholeEntry !== null) {
46 1
            $wormholeEntry->setLastUsed($this->stuTime->time());
47 1
            $this->wormholeEntryRepository->save($wormholeEntry);
48
        }
49
    }
50
51 5
    private function isWormhole(MapInterface|StarSystemMapInterface $waypoint): bool
52
    {
53 5
        if ($waypoint instanceof MapInterface) {
54 2
            return false;
55
        }
56
57 3
        return $waypoint->getSystem()->isWormhole();
58
    }
59
}
60