Skip to content

Commit edfca9c

Browse files
author
Mikhail Sazanov
committed
add rename delete create noop commands
1 parent f006c73 commit edfca9c

File tree

11 files changed

+210
-9
lines changed

11 files changed

+210
-9
lines changed

src/Commands/CreateCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Sazanof\PhpImapSockets\Commands;
4+
5+
use Sazanof\PhpImapSockets\Traits\PrepareArgument;
6+
7+
class CreateCommand extends Command
8+
{
9+
protected string $name = 'CREATE';
10+
11+
use PrepareArgument;
12+
13+
public function __construct(string $pathOrName)
14+
{
15+
16+
$this->setArguments($this->addQuotes($pathOrName));
17+
}
18+
}

src/Commands/DeleteCommand.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Sazanof\PhpImapSockets\Commands;
4+
5+
use Sazanof\PhpImapSockets\Traits\PrepareArgument;
6+
7+
class DeleteCommand extends Command
8+
{
9+
use PrepareArgument;
10+
11+
protected string $name = 'DELETE';
12+
13+
public function __construct(string $pathOrName)
14+
{
15+
16+
$this->setArguments($this->addQuotes($pathOrName));
17+
}
18+
}

src/Commands/ExamineCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ class ExamineCommand extends Command
1818
public function __construct(string $root)
1919
{
2020

21-
$this->setArguments($this->addQuotes($root));
21+
$this->setArguments($root);
2222
}
2323
}

src/Commands/ListCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(string $root, string $search)
2424
$this->root = $root;
2525
$this->search = $search;
2626
if (str_contains($this->root, ' ') || str_contains($this->root, '-') || strlen($this->root) === 0) {
27-
$this->root = '"' . $this->addQuotes($this->root) . '"';
27+
$this->root = '"' . $this->root . '"';
2828
}
2929
$this->setArguments($this->root . ' ' . $this->search);
3030
}

src/Commands/NoopCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Sazanof\PhpImapSockets\Commands;
4+
5+
class NoopCommand extends Command
6+
{
7+
protected string $name = 'NOOP';
8+
}

src/Commands/RenameCommand.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Sazanof\PhpImapSockets\Commands;
4+
5+
use Sazanof\PhpImapSockets\Traits\PrepareArgument;
6+
7+
class RenameCommand extends Command
8+
{
9+
protected string $name = 'RENAME';
10+
11+
use PrepareArgument;
12+
13+
public function __construct(string $currentName, string $newName)
14+
{
15+
$this->setArguments($this->addQuotes($currentName) . ' ' . $this->addQuotes($newName));
16+
}
17+
}

src/Commands/SelectCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class SelectCommand extends Command
1717

1818
public function __construct(string $folder)
1919
{
20-
$this->setArguments($this->addQuotes($folder));
20+
$this->setArguments($folder);
2121
}
2222
}

src/Connection.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@
1212
use ReflectionMethod;
1313
use Sazanof\PhpImapSockets\Collections\MailboxCollection;
1414
use Sazanof\PhpImapSockets\Commands\Command;
15+
use Sazanof\PhpImapSockets\Commands\CreateCommand;
16+
use Sazanof\PhpImapSockets\Commands\DeleteCommand;
17+
use Sazanof\PhpImapSockets\Commands\ExamineCommand;
1518
use Sazanof\PhpImapSockets\Commands\ListCommand;
1619
use Sazanof\PhpImapSockets\Commands\LoginCommand;
1720
use Sazanof\PhpImapSockets\Commands\LogoutCommand;
21+
use Sazanof\PhpImapSockets\Commands\NoopCommand;
22+
use Sazanof\PhpImapSockets\Commands\RenameCommand;
1823
use Sazanof\PhpImapSockets\Commands\SelectCommand;
1924
use Sazanof\PhpImapSockets\Exceptions\ConnectionException;
25+
use Sazanof\PhpImapSockets\Exceptions\MailboxOperationException;
2026
use Sazanof\PhpImapSockets\Exceptions\LoginFailedException;
2127
use Sazanof\PhpImapSockets\Models\Mailbox;
28+
use Sazanof\PhpImapSockets\Response\ExamineResponse;
2229
use Sazanof\PhpImapSockets\Response\Response;
2330
use Sazanof\PhpImapSockets\Response\SelectResponse;
2431
use Sazanof\PhpImapSockets\Socket;
@@ -356,4 +363,64 @@ public function select($mailboxName): ?SelectResponse
356363
$response = $this->command(SelectCommand::class, [$mailboxName]);
357364
return $response->isOk() ? new SelectResponse($response) : null;
358365
}
366+
367+
/**
368+
* @return bool
369+
* @throws ReflectionException
370+
*/
371+
public function noop(): bool
372+
{
373+
return $this->command(NoopCommand::class)->isOk();
374+
}
375+
376+
public function examine(string $path)
377+
{
378+
return $this->command(ExamineCommand::class, [$path])->asCollection(ExamineResponse::class);
379+
}
380+
381+
/**
382+
* @param string $pathOrName
383+
* @return bool
384+
* @throws MailboxOperationException
385+
* @throws ReflectionException
386+
*/
387+
public function createMailbox(string $pathOrName): bool
388+
{
389+
$response = $this->command(CreateCommand::class, [$pathOrName]);
390+
if ($response->isNotOk()) {
391+
throw new MailboxOperationException($response->lastLine());
392+
}
393+
return $response->isOk();
394+
}
395+
396+
/**
397+
* @param string $pathOrName
398+
* @return bool
399+
* @throws MailboxOperationException
400+
* @throws ReflectionException
401+
*/
402+
public function deleteMailbox(string $pathOrName): bool
403+
{
404+
$response = $this->command(DeleteCommand::class, [$pathOrName]);
405+
if ($response->isNotOk()) {
406+
throw new MailboxOperationException($response->lastLine());
407+
}
408+
return $response->isOk();
409+
}
410+
411+
/**
412+
* @param string $currentName
413+
* @param string $newName
414+
* @return bool
415+
* @throws MailboxOperationException
416+
* @throws ReflectionException
417+
*/
418+
public function renameMailbox(string $currentName, string $newName): bool
419+
{
420+
$response = $this->command(RenameCommand::class, [$currentName, $newName]);
421+
if ($response->isNotOk()) {
422+
throw new MailboxOperationException($response->lastLine());
423+
}
424+
return $response->isOk();
425+
}
359426
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Sazanof\PhpImapSockets\Exceptions;
4+
5+
class MailboxOperationException extends \Exception
6+
{
7+
public function __construct(string $message = "Error creating folder", int $code = 0, ?\Throwable $previous = null)
8+
{
9+
if (preg_match('/^(\d+) (BAD|NO) (.+?)\./', $message, $m)) {
10+
$message = $m[3];
11+
}
12+
parent::__construct($message, $code, $previous);
13+
}
14+
}

src/Models/Mailbox.php

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77

88
namespace Sazanof\PhpImapSockets\Models;
99

10+
use ReflectionException;
1011
use Sazanof\PhpImapSockets\Collections\BodyStructureCollection;
1112
use Sazanof\PhpImapSockets\Collections\Collection;
1213
use Sazanof\PhpImapSockets\Collections\MailboxCollection;
1314
use Sazanof\PhpImapSockets\Commands\ExamineCommand;
1415
use Sazanof\PhpImapSockets\Commands\FetchCommand;
1516
use Sazanof\PhpImapSockets\Commands\SearchCommand;
1617
use Sazanof\PhpImapSockets\Connection;
18+
use Sazanof\PhpImapSockets\Exceptions\MailboxOperationException;
19+
use Sazanof\PhpImapSockets\Exceptions\NoResultsException;
1720
use Sazanof\PhpImapSockets\Query\FetchQuery;
1821
use Sazanof\PhpImapSockets\Query\SearchQuery;
1922
use Sazanof\PhpImapSockets\Response\ExamineResponse;
@@ -106,9 +109,7 @@ public function parseLine(string $line)
106109

107110
public function examine()
108111
{
109-
$response = new ExamineResponse(
110-
$this->getConnection()->command(ExamineCommand::class, [$this->getOriginalPath()])
111-
);
112+
$response = $this->getConnection()->examine($this->getOriginalPath());
112113
$this->uidvalidity = $response->uidvalidiry;
113114
$this->uidnext = $response->uidnext;
114115
$this->unseen = $response->unseen;
@@ -124,24 +125,82 @@ public function select()
124125
return $this;
125126
}
126127

127-
public function search(SearchQuery $query)
128+
/**
129+
* @param string $folder
130+
* @param bool $insideCurrent
131+
* @return bool
132+
* @throws MailboxOperationException
133+
* @throws ReflectionException
134+
*/
135+
public function create(string $folder, bool $insideCurrent = false): bool
136+
{
137+
$name = $insideCurrent ? $this->getPath() . $this->getDelimiter() . $folder : $folder;
138+
return $this->getConnection()->createMailbox($name);
139+
}
140+
141+
/**
142+
* @param string $folder
143+
* @param bool $insideCurrent
144+
* @return bool
145+
* @throws ReflectionException
146+
* @throws MailboxOperationException
147+
*/
148+
public function delete(string $folder, bool $insideCurrent = false): bool
149+
{
150+
$name = $insideCurrent ? $this->getPath() . $this->getDelimiter() . $folder : $folder;
151+
return $this->getConnection()->deleteMailbox($name);
152+
}
153+
154+
/**
155+
* @param string $currentName
156+
* @param string $newName
157+
* @return bool
158+
* @throws MailboxOperationException
159+
* @throws ReflectionException
160+
*/
161+
public function rename(string $currentName, string $newName): bool
162+
{
163+
return $this->getConnection()->renameMailbox($currentName, $newName);
164+
}
165+
166+
/**
167+
* @param SearchQuery $query
168+
* @return SearchResponse
169+
* @throws ReflectionException
170+
* @throws NoResultsException
171+
*/
172+
public function search(SearchQuery $query): SearchResponse
128173
{
129174
return new SearchResponse(
130175
$this->connection->command(SearchCommand::class, [$query])
131176
);
132177
}
133178

134-
public function fetch(array $nums, FetchQuery $query)
179+
/**
180+
* @param array $nums
181+
* @param FetchQuery $query
182+
* @return Response
183+
* @throws ReflectionException
184+
*/
185+
public function fetch(array $nums, FetchQuery $query): Response
135186
{
136187
return $this->connection->command(FetchCommand::class, [$nums, $query]);
137188
}
138189

190+
/**
191+
* @param array $nums
192+
* @return BodyStructureCollection
193+
* @throws ReflectionException
194+
*/
139195
public function getBodyStructure(array $nums): BodyStructureCollection
140196
{
141197
$query = new FetchQuery();
142198
return $this->connection->command(FetchCommand::class, [$nums, $query->bodystructure()])->asCollection(BodyStructureCollection::class);
143199
}
144200

201+
/**
202+
* @return bool
203+
*/
145204
public function hasChildren()
146205
{
147206
return !empty(array_intersect(self::SPECIAL_ATTRIBUTES['haschildren'], $this->attributes->toArray()));

0 commit comments

Comments
 (0)