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

InteractionChecker::checkColonyPosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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