Passed
Push — master ( bebbb3...38c082 )
by Mattia
03:32
created

UsernameResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Resolvers;
6
7
use App\Cache\UserNotFoundCache;
8
use App\Minecraft\MojangClient;
9
use App\Repositories\AccountRepository;
10
use Illuminate\Support\Facades\Log;
11
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
37
     */
38
    public function resolve(string $username): ?string
39
    {
40
        /** @var \App\Models\Account $account */
41
        $account = $this->accountRepository->findLastUpdatedByUsername($username);
42
        if ($account) {
0 ignored issues
show
introduced by
$account is of type App\Models\Account, thus it always evaluated to true.
Loading history...
43
            return $account->uuid;
44
        }
45
46
        if (UserNotFoundCache::has($username)) {
47
            Log::debug('User not found cache hit');
48
49
            return env('DEFAULT_UUID');
50
        }
51
52
        try {
53
            $mojangAccount = $this->mojangClient->sendUsernameInfoRequest($username);
54
55
            return $mojangAccount->getUuid();
56
        } catch (\Throwable $exception) {
57
            UserNotFoundCache::add($username);
58
59
            return env('DEFAULT_UUID');
60
        }
61
    }
62
}
63