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