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

UserTick   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 33
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 11 2
A getEntitiesToLock() 0 3 1
A processEvent() 0 4 2
A __construct() 0 5 1
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