1 | <?php |
||
14 | class LoadUser |
||
15 | { |
||
16 | /** |
||
17 | * @var Client |
||
18 | */ |
||
19 | private $redis; |
||
20 | |||
21 | /** |
||
22 | * @param Client $redis |
||
23 | * @Inject("@redis") |
||
24 | */ |
||
25 | 5 | public function __construct(Client $redis) |
|
29 | |||
30 | /** |
||
31 | * @param string $username |
||
32 | * @return UserVO |
||
33 | * @throws UserNotFoundException |
||
34 | */ |
||
35 | 2 | public function loadUserByUsername(string $username) : UserVO |
|
45 | |||
46 | /** |
||
47 | * @param int $userId |
||
48 | * @return UserVO |
||
49 | * @throws UserNotFoundException |
||
50 | */ |
||
51 | 2 | public function loadUserById(int $userId) : UserVO |
|
52 | { |
||
53 | 2 | $redisUser = $this->redis->hgetall($this->getKey($userId)); |
|
54 | |||
55 | 2 | if (empty($redisUser)) { |
|
56 | 1 | throw new UserNotFoundException(sprintf('User "%d" does not exist.', $userId)); |
|
57 | } |
||
58 | |||
59 | 1 | return $this->buildUserVO($userId, $redisUser); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param int $userId |
||
64 | * @return string |
||
65 | */ |
||
66 | 2 | private function getKey(int $userId) : string |
|
70 | |||
71 | /** |
||
72 | * @param int $userId |
||
73 | * @param $redisUser |
||
74 | * @return UserVO |
||
75 | */ |
||
76 | 1 | private function buildUserVO(int $userId, array $redisUser) : UserVO |
|
89 | } |
||
90 |