Lines
100.00%
34 / 34
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| enter | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | ||
| enterOn | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | ||
| leave | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| attempt | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 3 | ||
| 22 | final readonly class Login | |
| 23 | { | |
| 24 | public const string VERSION = '5.3'; | |
| 25 | ||
| 26 | public static function enter( | |
| 27 | Credentials $credentials, | |
| 28 | CancellationTokenInterface $stop = new NullCancellationToken(), | |
| 29 | LoginClient $logins = new LoginClient(), | |
| 30 | Versions $versions = new InMemoryVersions(), | |
| 31 | ): Client|Ending { | |
| 32 | $tried = $versions->current(); | |
| 33 | $entered = self::attempt($credentials, $stop, $logins, $tried); | |
| 34 | $demanded = $entered instanceof Ending ? ClientVersion::demandedIn((string) $entered->reason) : null; | |
| 35 | if ($demanded === null || $demanded->equals($tried)) { | |
| 36 | return $entered; | |
| 37 | } | |
| 38 | ||
| 39 | $versions->learn($demanded); | |
| 40 | ||
| 41 | return self::attempt($credentials, $stop, $logins, $demanded); | |
| 42 | } | |
| 43 | ||
| 44 | public static function enterOn( | |
| 45 | Connection $connection, | |
| 46 | PlayerName $player, | |
| 47 | #[SensitiveParameter] | |
| 48 | string $token, | |
| 49 | CancellationTokenInterface $stop = new NullCancellationToken(), | |
| 50 | ClientVersion $version = new ClientVersion(self::VERSION), | |
| 51 | ): Client|Ending { | |
| 52 | $entered = Client::enter($connection, new GameLogin($player, $token, $version), $stop); | |
| 53 | if ($entered instanceof Client) { | |
| 54 | return $entered; | |
| 55 | } | |
| 56 | ||
| 57 | if ($entered instanceof Disconnected) { | |
| 58 | return new Ending(EndingStatus::Disconnected, $entered->reason); | |
| 59 | } | |
| 60 | ||
| 61 | $connection->close(); | |
| 62 | ||
| 63 | return $entered instanceof Stopped | |
| 64 | ? new Ending(EndingStatus::Stopped) | |
| 65 | : new Ending(EndingStatus::Failed, Outcome::of($entered)->summary); | |
| 66 | } | |
| 67 | ||
| 68 | public static function leave(Client $client): bool | |
| 69 | { | |
| 70 | try { | |
| 71 | $client->send(new LogoutCommand()); | |
| 72 | ||
| 73 | return true; | |
| 74 | } catch (PeerClosedException) { | |
| 75 | return false; | |
| 76 | } finally { | |
| 77 | $client->close(); | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | private static function attempt( | |
| 82 | Credentials $credentials, | |
| 83 | CancellationTokenInterface $stop, | |
| 84 | LoginClient $logins, | |
| 85 | ClientVersion $version, | |
| 86 | ): Client|Ending { | |
| 87 | try { | |
| 88 | $login = $logins->login($credentials->player, $credentials->password); | |
| 89 | ||
| 90 | $server = ServerChoice::pick($login->servers, $credentials->server); | |
| 91 | if ($server->isNone()) { | |
| 92 | return new Ending(EndingStatus::Failed, 'no open server'); | |
| 93 | } | |
| 94 | ||
| 95 | return self::enterOn( | |
| 96 | Connection::open($server->unwrap()), | |
| 97 | $credentials->player, | |
| 98 | $login->token, | |
| 99 | $stop, | |
| 100 | $version, | |
| 101 | ); | |
| 102 | } catch (Throwable $failure) { | |
| 103 | return new Ending(EndingStatus::Failed, $failure->getMessage()); | |
| 104 | } | |
| 105 | } | |
| 106 | } |