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

ColonyShipRepairEventStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
ccs 0
cts 1
cp 0
crap 2
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 Stu\Component\Event\Payload\PayloadWithIdAndTurn;
9
use Stu\Module\Tick\Ship\ManagerComponent\RepairActions;
10
use Stu\Orm\Entity\ColonyShipRepairInterface;
11
use Stu\Orm\Entity\EventInterface;
12
use Stu\Orm\Repository\ColonyShipRepairRepositoryInterface;
13
14
class ColonyShipRepairEventStrategy implements EventStrategyInterface
15
{
16
    public function __construct(
17
        private ColonyShipRepairRepositoryInterface $colonyShipRepairRepository,
18
        private RepairActions $repairActions,
19
        private JsonMapperInterface $jsonMapper
20
    ) {
21
    }
22
23
    public function getEntitiesToLock(EventInterface $event): Collection
24
    {
25
        $result = [];
26
27
        $colonyShipRepair = $this->getShipRepair($event);
28
        if ($colonyShipRepair !== null) {
29
            $colonyUser = $colonyShipRepair->getColony()->getUser();
30
            $shipUser = $colonyShipRepair->getShip()->getUser();
31
32
            $result[$colonyUser->getId()] = $colonyUser;
33
            $result[$shipUser->getId()] = $shipUser;
34
        }
35
36
        return new ArrayCollection($result);
37
    }
38
39
    public function processEvent(EventInterface $event): void
40
    {
41
        $shipRepair = $this->getShipRepair($event);
42
        if ($shipRepair !== null) {
43
            $this->repairActions->repairShipOnColony($shipRepair);
44
        }
45
    }
46
47
    private function getShipRepair(EventInterface $event): ?ColonyShipRepairInterface
48
    {
49
        /** @var PayloadWithIdAndTurn */
50
        $payload = $event->getPayloadAsObject($this->jsonMapper);
51
52
        return $this->colonyShipRepairRepository->find($payload->id);
53
    }
54
}
55