Lines 100.00% 15 / 15
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 login 100.00% 14 / 14 100.00% 1 / 1 2
11final readonly class LoginClient
12{
13    /** @param non-empty-string $url */
14    public function __construct(
15        private string $url = 'https://game.aq.com/game/api/login/now',
16    ) {}
17
18    /** @throws LoginFailedException */
19    public function login(PlayerName $player, #[SensitiveParameter] string $password): LoginResult
20    {
21        $curl = curl_init($this->url);
22
23        curl_setopt_array($curl, [
24            CURLOPT_POST => true,
25            CURLOPT_POSTFIELDS => http_build_query(['user' => (string) $player, 'pass' => $password, 'option' => '1']),
26            CURLOPT_RETURNTRANSFER => true,
27            CURLOPT_FOLLOWLOCATION => true,
28            CURLOPT_TIMEOUT => 10,
29            CURLOPT_CONNECTTIMEOUT => 5,
30            CURLOPT_USERAGENT => 'Mozilla/5.0',
31        ]);
32
33        $response = curl_exec($curl);
34
35        if (!is_string($response)) {
36            throw new LoginFailedException('Could not reach the login API: ' . curl_error($curl));
37        }
38
39        return LoginResult::fromJson($response);
40    }
41}