Skip to content

Commit 62dc8fd

Browse files
author
Mikhail Sazanov
committed
add lsub subscribe unsubscribe commands
1 parent edfca9c commit 62dc8fd

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

src/Commands/LsubCommand.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 LsubCommand extends ListCommand
6+
{
7+
protected string $name = 'LSUB';
8+
}

src/Commands/SubscribeCommand.php

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

src/Connection.php

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
use Sazanof\PhpImapSockets\Commands\ListCommand;
1919
use Sazanof\PhpImapSockets\Commands\LoginCommand;
2020
use Sazanof\PhpImapSockets\Commands\LogoutCommand;
21+
use Sazanof\PhpImapSockets\Commands\LsubCommand;
2122
use Sazanof\PhpImapSockets\Commands\NoopCommand;
2223
use Sazanof\PhpImapSockets\Commands\RenameCommand;
2324
use Sazanof\PhpImapSockets\Commands\SelectCommand;
25+
use Sazanof\PhpImapSockets\Commands\SubscribeCommand;
26+
use Sazanof\PhpImapSockets\Commands\UnsubscribeCommand;
2427
use Sazanof\PhpImapSockets\Exceptions\ConnectionException;
2528
use Sazanof\PhpImapSockets\Exceptions\MailboxOperationException;
2629
use Sazanof\PhpImapSockets\Exceptions\LoginFailedException;
@@ -322,6 +325,18 @@ public function listMailboxes(string $root = '', string $searchQuery = '*'): Mai
322325
return new MailboxCollection($this->command(ListCommand::class, [$root, $searchQuery]), $this);
323326
}
324327

328+
/**
329+
* @param string $root
330+
* @param string $searchQuery
331+
* @return MailboxCollection
332+
* @throws ConnectionException
333+
* @throws ReflectionException
334+
*/
335+
public function lsubMailboxes(string $root = '', string $searchQuery = '*'): MailboxCollection
336+
{
337+
return new MailboxCollection($this->command(LsubCommand::class, [$root, $searchQuery]), $this);
338+
}
339+
325340
/**
326341
* @param string $path
327342
* @return Mailbox|null
@@ -373,7 +388,12 @@ public function noop(): bool
373388
return $this->command(NoopCommand::class)->isOk();
374389
}
375390

376-
public function examine(string $path)
391+
/**
392+
* @param string $path
393+
* @return ExamineResponse
394+
* @throws ReflectionException
395+
*/
396+
public function examine(string $path): ExamineResponse
377397
{
378398
return $this->command(ExamineCommand::class, [$path])->asCollection(ExamineResponse::class);
379399
}
@@ -423,4 +443,34 @@ public function renameMailbox(string $currentName, string $newName): bool
423443
}
424444
return $response->isOk();
425445
}
446+
447+
/**
448+
* @param string $name
449+
* @return bool
450+
* @throws MailboxOperationException
451+
* @throws ReflectionException
452+
*/
453+
public function subscribeMailbox(string $name): bool
454+
{
455+
$response = $this->command(SubscribeCommand::class, [$name]);
456+
if ($response->isNotOk()) {
457+
throw new MailboxOperationException($response->lastLine());
458+
}
459+
return $response->isOk();
460+
}
461+
462+
/**
463+
* @param string $name
464+
* @return bool
465+
* @throws MailboxOperationException
466+
* @throws ReflectionException
467+
*/
468+
public function unsubscribeMailbox(string $name): bool
469+
{
470+
$response = $this->command(UnsubscribeCommand::class, [$name]);
471+
if ($response->isNotOk()) {
472+
throw new MailboxOperationException($response->lastLine());
473+
}
474+
return $response->isOk();
475+
}
426476
}

src/Models/Mailbox.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function create(string $folder, bool $insideCurrent = false): bool
147147
*/
148148
public function delete(string $folder, bool $insideCurrent = false): bool
149149
{
150-
$name = $insideCurrent ? $this->getPath() . $this->getDelimiter() . $folder : $folder;
150+
$name = $insideCurrent ? $this->getOriginalPath() . $this->getDelimiter() . $folder : $folder;
151151
return $this->getConnection()->deleteMailbox($name);
152152
}
153153

@@ -163,6 +163,24 @@ public function rename(string $currentName, string $newName): bool
163163
return $this->getConnection()->renameMailbox($currentName, $newName);
164164
}
165165

166+
/**
167+
* @param string|null $name
168+
* @return bool
169+
* @throws MailboxOperationException
170+
* @throws ReflectionException
171+
*/
172+
public function subscribe(string $name = null): bool
173+
{
174+
$name = is_null($name) ? $this->getOriginalPath() : $name;
175+
return $this->getConnection()->subscribeMailbox($name);
176+
}
177+
178+
public function unsubscribe(string $name = null): bool
179+
{
180+
$name = is_null($name) ? $this->getOriginalPath() : $name;
181+
return $this->getConnection()->unsubscribeMailbox($name);
182+
}
183+
166184
/**
167185
* @param SearchQuery $query
168186
* @return SearchResponse

0 commit comments

Comments
 (0)