Lines
100.00%
15 / 15
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| LoginClient | 100.00% | 15 / 15 | 100.00% | 2 / 2 | 3 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| login | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Client\Login; | |
| 6 | ||
| 7 | use Lorekeeper\Client\Exception\LoginFailedException; | |
| 8 | use Lorekeeper\Core\Player\PlayerName; | |
| 9 | use SensitiveParameter; | |
| 10 | ||
| 11 | final 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 | } |