Skip to content

Commit 9aa3246

Browse files
authored
Merge pull request #85 from DirectoryTree/connection-exception-handling
Handle connection exceptions during destruct and disconnect
2 parents 44a211d + 0a19905 commit 9aa3246

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/Connection/ImapConnection.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
use DirectoryTree\ImapEngine\Connection\Streams\StreamInterface;
1515
use DirectoryTree\ImapEngine\Connection\Tokens\Token;
1616
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier;
17-
use DirectoryTree\ImapEngine\Exceptions\Exception;
1817
use DirectoryTree\ImapEngine\Exceptions\ImapCommandException;
1918
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionClosedException;
2019
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionFailedException;
2120
use DirectoryTree\ImapEngine\Exceptions\ImapConnectionTimedOutException;
2221
use DirectoryTree\ImapEngine\Exceptions\ImapResponseException;
2322
use DirectoryTree\ImapEngine\Exceptions\ImapStreamException;
2423
use DirectoryTree\ImapEngine\Support\Str;
24+
use Exception;
2525
use Generator;
2626
use LogicException;
2727

@@ -69,8 +69,14 @@ public static function fake(array $responses = []): static
6969
*/
7070
public function __destruct()
7171
{
72-
if ($this->connected()) {
73-
$this->logout();
72+
if (! $this->connected()) {
73+
return;
74+
}
75+
76+
try {
77+
@$this->logout();
78+
} catch (Exception $e) {
79+
// Do nothing.
7480
}
7581
}
7682

@@ -182,14 +188,7 @@ public function login(string $user, string $password): TaggedResponse
182188
*/
183189
public function logout(): void
184190
{
185-
try {
186-
// It's generally acceptable to send a logout command to an IMAP server
187-
// and not wait for a response. If the server encounters an error
188-
// processing the request, we will have to reconnect anyway.
189-
$this->send('LOGOUT', tag: $tag);
190-
} catch (Exception) {
191-
// Do nothing.
192-
}
191+
$this->send('LOGOUT', tag: $tag);
193192
}
194193

195194
/**

src/Mailbox.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DirectoryTree\ImapEngine\Connection\Loggers\FileLogger;
99
use DirectoryTree\ImapEngine\Connection\Streams\ImapStream;
1010
use DirectoryTree\ImapEngine\Connection\Tokens\Atom;
11+
use Exception;
1112

1213
class Mailbox implements MailboxInterface
1314
{
@@ -167,12 +168,14 @@ protected function authenticate(): void
167168
*/
168169
public function disconnect(): void
169170
{
170-
if ($this->connected()) {
171-
$this->connection->logout();
172-
$this->connection->disconnect();
171+
try {
172+
$this->connection?->logout();
173+
$this->connection?->disconnect();
174+
} catch (Exception) {
175+
// Do nothing.
176+
} finally {
177+
$this->connection = null;
173178
}
174-
175-
$this->connection = null;
176179
}
177180

178181
/**

0 commit comments

Comments
 (0)