| Total Complexity | 8 |
| Total Lines | 38 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | class UsernameResolver |
||
| 13 | { |
||
| 14 | public function __construct( |
||
| 15 | private AccountRepository $accountRepository, |
||
| 16 | private MojangClient $mojangClient |
||
| 17 | ) { |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @throws \Exception |
||
| 22 | */ |
||
| 23 | public function resolve(string $username): ?string |
||
| 24 | { |
||
| 25 | if (!$this->isValidUsername($username)) { |
||
| 26 | return null; |
||
| 27 | } |
||
| 28 | |||
| 29 | $account = $this->accountRepository->findLastUpdatedByUsername($username); |
||
| 30 | if ($account !== null) { |
||
| 31 | return $account->uuid; |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!UserNotFoundCache::has($username)) { |
||
| 35 | try { |
||
| 36 | $mojangAccount = $this->mojangClient->sendUsernameInfoRequest($username); |
||
| 37 | |||
| 38 | return $mojangAccount->getUuid(); |
||
| 39 | } catch (\Throwable $exception) { |
||
| 40 | UserNotFoundCache::add($username); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | return null; |
||
| 45 | } |
||
| 46 | |||
| 47 | private function isValidUsername(string $username): bool |
||
| 50 | } |
||
| 51 | } |
||
| 52 |