generated from Nodarx/template
67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXM\ProviderImap\Client\Command;
|
|
|
|
use KTXM\ProviderImap\Client\Command\Result\CommandStatusResult;
|
|
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\SessionContext;
|
|
use KTXM\ProviderImap\Client\SessionState;
|
|
|
|
/**
|
|
* @implements CommandInterface<CommandStatusResult>
|
|
*/
|
|
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',
|
|
$this->quote($this->username),
|
|
$this->quote($this->password),
|
|
));
|
|
}
|
|
|
|
public function handle(ResponseStream $responses, SessionContext $context): CommandStatusResult
|
|
{
|
|
foreach ($responses as $response) {
|
|
if ($response instanceof TaggedResponse) {
|
|
if (!$response->isOk()) {
|
|
throw new ImapException('LOGIN failed: ' . $response->text());
|
|
}
|
|
|
|
$context->setState(SessionState::Authenticated);
|
|
|
|
return new CommandStatusResult($response->status(), $response->text());
|
|
}
|
|
}
|
|
|
|
throw new ImapException('LOGIN did not receive a tagged completion response.');
|
|
}
|
|
|
|
private function quote(string $value): string
|
|
{
|
|
return '"' . addcslashes($value, "\\\"") . '"';
|
|
}
|
|
} |