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

TrackerSystemData::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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