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

ColonyShipRepairEventStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntitiesToLock() 0 14 2
A getShipRepair() 0 6 1
A __construct() 0 5 1
A processEvent() 0 5 2
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