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

NoOneTick   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 30
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processEvent() 0 3 1
A getUser() 0 11 2
A __construct() 0 5 1
A getEntitiesToLock() 0 3 1
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\LowerHull;
14
use Stu\Orm\Entity\EventInterface;
15
use Stu\Orm\Entity\UserInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
18
class NoOneTick implements EventStrategyInterface
19
{
20
    public function __construct(
21
        private UserRepositoryInterface $userRepository,
22
        private JsonMapperInterface $jsonMapper,
23
        private LowerHull $lowerHull
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->lowerHull->work();
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