Passed
Push — dev ( eeaa0f...91a85a )
by Janko
26:10
created

getAlertedBattleParties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Component\Event\Strategy;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use JsonMapper\JsonMapperInterface;
8
use RuntimeException;
9
use Stu\Component\Event\Payload\PayloadWithIdAndTurn;
10
use Stu\Lib\Information\InformationWrapper;
11
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertDetectionInterface;
12
use Stu\Module\Ship\Lib\Battle\AlertDetection\AlertReactionFacadeInterface;
13
use Stu\Module\Ship\Lib\Battle\Party\AlertStateBattleParty;
14
use Stu\Module\Ship\Lib\ShipWrapperFactoryInterface;
15
use Stu\Orm\Entity\EventInterface;
16
use Stu\Orm\Entity\ShipInterface;
17
use Stu\Orm\Repository\ShipRepositoryInterface;
18
19
class AlertReactionEventStrategy implements EventStrategyInterface
20
{
21
    /** @var array<int, AlertStateBattleParty> */
22
    private ?array $alertedBattleParties = null;
23
24
    public function __construct(
25
        private ShipRepositoryInterface $shipRepository,
26
        private AlertDetectionInterface $alertDetection,
27
        private AlertReactionFacadeInterface $alertReactionFacade,
28
        private ShipWrapperFactoryInterface $shipWrapperFactory,
29
        private JsonMapperInterface $jsonMapper
30
    ) {
31
    }
32
33
    public function getEntitiesToLock(EventInterface $event): Collection
34
    {
35
        $incomingShip = $this->getIncomingShip($event);
36
        $user = $incomingShip->getUser();
37
38
39
        $alertedBattleParties = $this->getAlertedBattleParties($event);
40
41
        $result = [$user->getId() => $user];
42
        foreach ($alertedBattleParties as $battleParty) {
43
44
            $user = $battleParty->getUser();
45
            $result[$user->getId()] = $user;
46
        }
47
48
        return new ArrayCollection($result);
49
    }
50
51
    public function processEvent(EventInterface $event): void
52
    {
53
        $incomingShip = $this->getIncomingShip($event);
54
55
        $this->alertReactionFacade->doItAll(
56
            $this->shipWrapperFactory->wrapShip($incomingShip),
57
            new InformationWrapper(),
58
            $incomingShip->getTractoringShip(),
59
            $this->getAlertedBattleParties($event)
60
        );
61
    }
62
63
    /** @return array<int, AlertStateBattleParty> */
64
    private function getAlertedBattleParties(EventInterface $event): array
65
    {
66
        if ($this->alertedBattleParties === null) {
67
            $incomingShip = $this->getIncomingShip($event);
68
69
            $this->alertedBattleParties =
70
                $this->alertDetection->detectAlertedBattleParties(
71
                    $incomingShip,
72
                    new InformationWrapper(),
73
                    $incomingShip->getTractoringShip()
74
                );
75
        }
76
77
        return $this->alertedBattleParties;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->alertedBattleParties could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80
    private function getIncomingShip(EventInterface $event): ShipInterface
81
    {
82
        /** @var PayloadWithIdAndTurn */
83
        $payload = $event->getPayloadAsObject($this->jsonMapper);
84
85
        $ship = $this->shipRepository->find($payload->id);
86
        if ($ship === null) {
87
            throw new RuntimeException('no existing ship found for id %d', $payload->id);
88
        }
89
90
        return $ship;
91
    }
92
}
93