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

ThreatReaction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 73
ccs 36
cts 36
cp 1
rs 10
c 1
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A reactToThreat() 0 47 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Ship\Lib\Interaction;
6
7
use Stu\Component\Player\PlayerRelationDeterminatorInterface;
8
use Stu\Component\Ship\ShipAlertStateEnum;
9
use Stu\Module\Control\GameControllerInterface;
10
use Stu\Module\Message\Lib\PrivateMessageFolderSpecialEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
use Stu\Module\Ship\Lib\Battle\FightLibInterface;
13
use Stu\Module\Ship\Lib\Battle\ShipAttackCycleInterface;
14
use Stu\Module\Ship\Lib\ShipWrapperInterface;
15
16
final class ThreatReaction implements ThreatReactionInterface
17
{
18
    private PlayerRelationDeterminatorInterface $playerRelationDeterminator;
19
20
    private ShipAttackCycleInterface $shipAttackCycle;
21
22
    private FightLibInterface $fightLib;
23
24
    private PrivateMessageSenderInterface $privateMessageSender;
25
26
    private GameControllerInterface $game;
27
28 5
    public function __construct(
29
        PlayerRelationDeterminatorInterface $playerRelationDeterminator,
30
        ShipAttackCycleInterface $shipAttackCycle,
31
        FightLibInterface $fightLib,
32
        PrivateMessageSenderInterface $privateMessageSender,
33
        GameControllerInterface $game
34
    ) {
35 5
        $this->playerRelationDeterminator = $playerRelationDeterminator;
36 5
        $this->shipAttackCycle = $shipAttackCycle;
37 5
        $this->fightLib = $fightLib;
38 5
        $this->privateMessageSender = $privateMessageSender;
39 5
        $this->game = $game;
40
    }
41
42 5
    public function reactToThreat(
43
        ShipWrapperInterface $wrapper,
44
        ShipWrapperInterface $targetWrapper,
45
        string $cause
46
    ): bool {
47
48 5
        $target = $targetWrapper->get();
49 5
        if ($target->getAlertState() === ShipAlertStateEnum::ALERT_GREEN) {
50 1
            return false;
51
        }
52
53 4
        $ship = $wrapper->get();
54 4
        $user = $ship->getUser();
55 4
        if ($target->getUser() === $user) {
56 1
            return false;
57
        }
58
59 3
        if ($this->playerRelationDeterminator->isFriend($target->getUser(), $user)) {
60 1
            return false;
61
        }
62
63
64 2
        $messageCollection = $this->shipAttackCycle->cycle(
65 2
            $this->fightLib->getAttackers($targetWrapper),
66 2
            [$wrapper],
67 2
            true
68 2
        );
69
70 2
        if ($messageCollection->isEmpty()) {
71 1
            return false;
72
        }
73
74 1
        $informations = $messageCollection->getInformationDump();
75 1
        $this->game->addInformationWrapper($informations);
76
77 1
        $this->privateMessageSender->send(
78 1
            $user->getId(),
79 1
            $target->getUser()->getId(),
80 1
            sprintf(
81 1
                "%s\nFolgende Aktionen wurden ausgeführt:\n%s",
82 1
                $cause,
83 1
                $informations->getInformationsAsString()
84 1
            ),
85 1
            PrivateMessageFolderSpecialEnum::PM_SPECIAL_SHIP
86 1
        );
87
88 1
        return true;
89
    }
90
}
91