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

NpcTick::getEntitiesToLock()   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 1
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\Tick\Npc;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use JsonMapper\JsonMapperInterface;
10
use RuntimeException;
11
use Stu\Component\Event\Payload\PayloadWithIdAndTurn;
12
use Stu\Component\Event\Strategy\EventStrategyInterface;
13
use Stu\Module\Tick\Ship\ManagerComponent\NpcShipHandling;
14
use Stu\Orm\Entity\EventInterface;
15
use Stu\Orm\Entity\UserInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
class NpcTick implements EventStrategyInterface
19
{
20
    public function __construct(
21
        private UserRepositoryInterface $userRepository,
22
        private JsonMapperInterface $jsonMapper,
23
        private NpcShipHandling $npcShipHandling
24
    ) {
25
    }
26
27
    public function getEntitiesToLock(EventInterface $event): Collection
28
    {
29
        return new ArrayCollection([$this->getUser($event)]);
30
    }
31
32
    public function processEvent(EventInterface $event): void
33
    {
34
        $this->npcShipHandling->processUser($this->getUser($event));
35
    }
36
37
    private function getUser(EventInterface $event): UserInterface
38
    {
39
        /** @var PayloadWithIdAndTurn */
40
        $payload = $event->getPayloadAsObject($this->jsonMapper);
41
42
        $user = $this->userRepository->find($payload->id);
43
        if ($user === null) {
44
            throw new RuntimeException(sprintf('userId %d does not exist', $payload->id));
45
        }
46
47
        return $user;
48
    }
49
}
50