|
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
|
|
|
|