Passed
Push — master ( f4068b...77f637 )
by Nico
40:27 queued 14:09
created

InteractionChecker   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 39
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkColonyPosition() 0 3 1
B canInteractWith() 0 26 9
A checkPosition() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Interaction;
6
7
use Stu\Module\Control\GameControllerInterface;
8
use Stu\Orm\Entity\ColonyInterface;
9
use Stu\Orm\Entity\ShipInterface;
10
11
final class InteractionChecker implements InteractionCheckerInterface
12
{
13
    public function checkPosition(ShipInterface $shipa, ShipInterface $shipb): bool
14
    {
15
        return $shipa->getMap() === $shipb->getMap() && $shipa->getStarsystemMap() === $shipb->getStarsystemMap();
16
    }
17
18
    public function checkColonyPosition(ColonyInterface $col, ShipInterface $ship): bool
19
    {
20
        return $col->getStarsystemMap() === $ship->getStarsystemMap();
21
    }
22
23
    //TODO intercept script attacks, e.g. beam from cloaked or warped ship
24
    public static function canInteractWith(
25
        ShipInterface $ship,
26
        ShipInterface|ColonyInterface $target,
27
        GameControllerInterface $game,
28
        bool $doCloakCheck = false
29
    ): bool {
30
        if ($target->getUser()->isVacationRequestOldEnough()) {
31
            $game->addInformation(_('Aktion nicht möglich, der Spieler befindet sich im Urlaubsmodus!'));
32
33
            return false;
34
        }
35
36
        if ($ship->getCloakState()) {
37
            return false;
38
        }
39
40
        $interactionChecker = new InteractionChecker();
41
        if ($target instanceof ColonyInterface) {
42
            return $interactionChecker->checkColonyPosition($target, $ship) && $target->getId() != $ship->getId();
43
        } elseif (!$interactionChecker->checkPosition($ship, $target)) {
44
            return false;
45
        }
46
        if ($target->getShieldState() && $target->getUserId() != $ship->getUser()->getId()) {
0 ignored issues
show
Deprecated Code introduced by
The function Stu\Orm\Entity\ShipInterface::getUserId() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
        if ($target->getShieldState() && /** @scrutinizer ignore-deprecated */ $target->getUserId() != $ship->getUser()->getId()) {
Loading history...
47
            return false;
48
        }
49
        return !($doCloakCheck && $target->getCloakState());
50
    }
51
}
52