Passed
Push — dev ( 5b1d95...923b08 )
by Janko
23:33 queued 07:31
created

TrackerDeviceHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 9.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 40
ccs 2
cts 21
cp 0.0952
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B handleSpacecraftTick() 0 34 6
1
<?php
2
3
namespace Stu\Module\Tick\Spacecraft\Handler;
4
5
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...
6
use RuntimeException;
7
use Stu\Lib\Information\InformationInterface;
8
use Stu\Module\Ship\Lib\ShipWrapperInterface;
9
use Stu\Module\Spacecraft\Lib\Interaction\TrackerDeviceManagerInterface;
10
use Stu\Module\Spacecraft\Lib\SpacecraftWrapperInterface;
11
12
class TrackerDeviceHandler implements SpacecraftTickHandlerInterface
13
{
14 1
    public function __construct(
15
        private TrackerDeviceManagerInterface $trackerDeviceManager
16 1
    ) {}
17
18
    #[Override]
19
    public function handleSpacecraftTick(
20
        SpacecraftWrapperInterface $wrapper,
21
        InformationInterface $information
22
    ): void {
23
        if (!$wrapper instanceof ShipWrapperInterface) {
24
            return;
25
        }
26
27
        $ship = $wrapper->get();
28
        $tracker = $wrapper->getTrackerSystemData();
29
30
        if ($tracker === null || $tracker->targetId === null) {
31
            return;
32
        }
33
34
        $targetWrapper = $tracker->getTargetWrapper();
35
        if ($targetWrapper === null) {
36
            throw new RuntimeException('should not happen');
37
        }
38
39
        $target = $targetWrapper->get();
40
        $shipLocation = $ship->getLocation();
41
        $targetLocation = $target->getLocation();
42
        $remainingTicks = $tracker->getRemainingTicks();
43
44
        $reduceByTicks = max(1, (int)ceil((abs($shipLocation->getCx() - $targetLocation->getCx())
45
            +  abs($shipLocation->getCy() - $targetLocation->getCy())) / 50));
46
47
        //reduce remaining ticks
48
        if ($remainingTicks > $reduceByTicks) {
49
            $tracker->setRemainingTicks($remainingTicks - $reduceByTicks)->update();
50
        } else {
51
            $this->trackerDeviceManager->deactivateTrackerIfActive($wrapper, true);
52
        }
53
    }
54
}
55