Total Complexity | 8 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
12 | class UsernameResolver |
||
13 | { |
||
14 | /** |
||
15 | * @var AccountRepository |
||
16 | */ |
||
17 | private AccountRepository $accountRepository; |
||
18 | /** |
||
19 | * @var MojangClient |
||
20 | */ |
||
21 | private MojangClient $mojangClient; |
||
22 | |||
23 | public function __construct( |
||
24 | AccountRepository $accountRepository, |
||
25 | MojangClient $mojangClient |
||
26 | ) { |
||
27 | $this->accountRepository = $accountRepository; |
||
28 | $this->mojangClient = $mojangClient; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param string $username |
||
33 | * |
||
34 | * @throws \Exception |
||
35 | * |
||
36 | * @return string|null |
||
37 | */ |
||
38 | public function resolve(string $username): ?string |
||
39 | { |
||
40 | if (!$this->isValidUsername($username)) { |
||
41 | return null; |
||
42 | } |
||
43 | |||
44 | /** @var \Minepic\Models\Account $account */ |
||
45 | $account = $this->accountRepository->findLastUpdatedByUsername($username); |
||
46 | if ($account !== null) { |
||
47 | return $account->uuid; |
||
48 | } |
||
49 | |||
50 | if (!UserNotFoundCache::has($username)) { |
||
51 | try { |
||
52 | $mojangAccount = $this->mojangClient->sendUsernameInfoRequest($username); |
||
53 | |||
54 | return $mojangAccount->getUuid(); |
||
55 | } catch (\Throwable $exception) { |
||
56 | UserNotFoundCache::add($username); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | return null; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $username |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | private function isValidUsername(string $username): bool |
||
71 | } |
||
72 | } |
||
73 |