Files
provider_imap/lib/Client/Protocol/Command/LoginCommand.php
T
2026-09-25 12:14:37 -04:00

62 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace KTXM\ProviderImap\Client\Protocol\Command;
use KTXM\ProviderImap\Client\Protocol\CompletionChecker;
use KTXM\ProviderImap\Client\Protocol\StringEncoder;
use KTXM\ProviderImap\Client\Result\CommandCompletion;
use KTXM\ProviderImap\Client\ImapException;
use KTXM\ProviderImap\Client\Protocol\RequestFrame;
use KTXM\ProviderImap\Client\Protocol\Response\TaggedResponse;
use KTXM\ProviderImap\Client\Protocol\ResponseStream;
use KTXM\ProviderImap\Client\Protocol\SessionContext;
use KTXM\ProviderImap\Client\Protocol\SessionState;
/**
* @implements CommandInterface<CommandCompletion>
*/
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) {
CompletionChecker::assertSuccess($this->name(), $response);
$context->setState(SessionState::Authenticated);
return new CommandCompletion($response->status(), $response->text());
}
}
throw new ImapException('LOGIN did not receive a tagged completion response.');
}
}