*/ final class RenameCommand implements CommandInterface { public function __construct( private readonly string $fromMailbox, private readonly string $toMailbox, ) {} public function name(): string { return 'RENAME'; } public function allowedStates(): array { return [ SessionState::Authenticated, SessionState::Selected, ]; } public function encode(string $tag, SessionContext $context): RequestFrame { unset($tag, $context); return new RequestFrame(sprintf( 'RENAME %s %s', $this->quote($this->fromMailbox), $this->quote($this->toMailbox), )); } public function handle(ResponseStream $responses, SessionContext $context): CommandStatusResult { foreach ($responses as $response) { if ($response instanceof TaggedResponse) { if (!$response->isOk()) { throw new ImapException('RENAME failed: ' . $response->text()); } if ($context->selectedMailbox() === $this->fromMailbox) { $context->setSelectedMailbox($this->toMailbox); } return new CommandStatusResult($response->status(), $response->text()); } } throw new ImapException('RENAME did not receive a tagged completion response.'); } private function quote(string $value): string { return '"' . addcslashes($value, "\\\"") . '"'; } }