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

UserTick::getUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Tick\User;
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\Orm\Entity\EventInterface;
14
use Stu\Orm\Entity\UserInterface;
15
use Stu\Orm\Repository\UserRepositoryInterface;
16
17
class UserTick implements EventStrategyInterface
18
{
19
    /** @param array<int, UserTickComponentInterface> $components */
20
    public function __construct(
21
        private UserRepositoryInterface $userRepository,
22
        private array $components,
23
        private JsonMapperInterface $jsonMapper
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
        foreach ($this->components as $component) {
35
            $component->processUser($this->getUser($event));
36
        }
37
    }
38
39
    private function getUser(EventInterface $event): UserInterface
40
    {
41
        /** @var PayloadWithIdAndTurn */
42
        $payload = $event->getPayloadAsObject($this->jsonMapper);
43
44
        $user = $this->userRepository->find($payload->id);
45
        if ($user === null) {
46
            throw new RuntimeException(sprintf('userId %d does not exist', $payload->id));
47
        }
48
49
        return $user;
50
    }
51
}
52