*/ final class LoginCommand implements CommandInterface { public function __construct( private readonly string $username, private readonly string $password, ) {} public function name(): string { return 'LOGIN'; } public function allowedStates(): array { return [SessionState::NotAuthenticated]; } public function encode(string $tag, SessionContext $context): RequestFrame { unset($tag, $context); return new RequestFrame(sprintf( 'LOGIN %s %s', StringEncoder::quote($this->username), StringEncoder::quote($this->password), )); } public function handle(ResponseStream $responses, SessionContext $context): CommandCompletion { foreach ($responses as $response) { if ($response instanceof TaggedResponse) { if (!$response->isOk()) { throw new ImapException('LOGIN failed: ' . $response->text()); } $context->setState(SessionState::Authenticated); return new CommandCompletion($response->status(), $response->text()); } } throw new ImapException('LOGIN did not receive a tagged completion response.'); } }