Passed
Pull Request — master (#1830)
by Nico
30:56
created

TrackerSystemData   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 16.66%

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 71
ccs 5
cts 30
cp 0.1666
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setRemainingTicks() 0 4 1
A getRemainingTicks() 0 3 1
A __construct() 0 6 1
A setTarget() 0 4 1
A isUseable() 0 9 3
A getSystemType() 0 3 1
A getTargetWrapper() 0 13 3
A canAttackCloakedTarget() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Ship\System\Data;
6
7
use RuntimeException;
8
use Stu\Component\Ship\System\ShipSystemTypeEnum;
9
use Stu\Module\Ship\Lib\Interaction\InteractionChecker;
10
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
11
use Stu\Module\Ship\Lib\ShipWrapperInterface;
12
use Stu\Orm\Repository\ShipRepositoryInterface;
13
use Stu\Orm\Repository\ShipSystemRepositoryInterface;
14
15
class TrackerSystemData extends AbstractSystemData
16
{
17
    public ?int $targetId = null;
18
    public int $remainingTicks = 0;
19
20 2
    public function __construct(
21
        private ShipRepositoryInterface $shipRepository,
22
        private ShipWrapperFactoryInterface $shipWrapperFactory,
23
        ShipSystemRepositoryInterface $shipSystemRepository
24
    ) {
25 2
        parent::__construct($shipSystemRepository);
26
    }
27
28
    function getSystemType(): ShipSystemTypeEnum
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
    {
30
        return ShipSystemTypeEnum::SYSTEM_TRACKER;
31
    }
32
33
    public function getTargetWrapper(): ?ShipWrapperInterface
34
    {
35
        if ($this->targetId === null) {
36
            return null;
37
        }
38
39
        $target = $this->shipRepository->find($this->targetId);
40
41
        if ($target === null) {
42
            throw new RuntimeException('target ship is not existent');
43
        }
44
45
        return $this->shipWrapperFactory->wrapShip($target);
46
    }
47
48 2
    public function setTarget(?int $targetId): TrackerSystemData
49
    {
50 2
        $this->targetId = $targetId;
51 2
        return $this;
52
    }
53
54
    public function isUseable(): bool
55
    {
56
        if ($this->targetId !== null) {
57
            return false;
58
        }
59
60
        $cooldown = $this->ship->getShipSystem(ShipSystemTypeEnum::SYSTEM_TRACKER)->getCooldown();
61
62
        return $cooldown === null ? true : $cooldown < time();
63
    }
64
65
    public function getRemainingTicks(): int
66
    {
67
        return $this->remainingTicks;
68
    }
69
70
    public function setRemainingTicks(int $ticks): TrackerSystemData
71
    {
72
        $this->remainingTicks = $ticks;
73
        return $this;
74
    }
75
76
    public function canAttackCloakedTarget(): bool
77
    {
78
        $targetWrapper = $this->getTargetWrapper();
79
        if ($targetWrapper === null) {
80
            return false;
81
        }
82
83
        $target = $targetWrapper->get();
84
85
        return (new InteractionChecker())->checkPosition($this->ship, $target) && $target->getCloakState();
86
    }
87
}
88