1212use DirectoryTree \ImapEngine \Connection \Streams \StreamInterface ;
1313use DirectoryTree \ImapEngine \Connection \Tokens \Token ;
1414use DirectoryTree \ImapEngine \Enums \ImapFetchIdentifier ;
15- use DirectoryTree \ImapEngine \Exceptions \CommandFailedException ;
16- use DirectoryTree \ImapEngine \Exceptions \ConnectionClosedException ;
17- use DirectoryTree \ImapEngine \Exceptions \ConnectionFailedException ;
18- use DirectoryTree \ImapEngine \Exceptions \ConnectionTimedOutException ;
15+ use DirectoryTree \ImapEngine \Exceptions \ImapCommandException ;
16+ use DirectoryTree \ImapEngine \Exceptions \ImapConnectionClosedException ;
17+ use DirectoryTree \ImapEngine \Exceptions \ImapConnectionException ;
18+ use DirectoryTree \ImapEngine \Exceptions \ImapConnectionFailedException ;
19+ use DirectoryTree \ImapEngine \Exceptions \ImapConnectionTimedOutException ;
1920use DirectoryTree \ImapEngine \Exceptions \Exception ;
21+ use DirectoryTree \ImapEngine \Exceptions \ImapResponseException ;
22+ use DirectoryTree \ImapEngine \Exceptions \ImapStreamException ;
2023use DirectoryTree \ImapEngine \Exceptions \RuntimeException ;
2124use DirectoryTree \ImapEngine \Support \Str ;
2225use Generator ;
26+ use LogicException ;
2327
2428class ImapConnection implements ConnectionInterface
2529{
@@ -88,7 +92,7 @@ public function connect(string $host, ?int $port = null, array $options = []): v
8892 $ this ->assertNextResponse (
8993 fn (Response $ response ) => $ response instanceof UntaggedResponse,
9094 fn (UntaggedResponse $ response ) => $ response ->type ()->is ('OK ' ),
91- fn () => new ConnectionFailedException ("Connection to $ host: $ port failed " )
95+ fn () => new ImapConnectionFailedException ("Connection to $ host: $ port failed " )
9296 );
9397
9498 if ($ transport === 'starttls ' ) {
@@ -150,7 +154,7 @@ public function login(string $user, string $password): TaggedResponse
150154 $ this ->send ('LOGIN ' , Str::literal ([$ user , $ password ]), $ tag );
151155
152156 return $ this ->assertTaggedResponse ($ tag , fn (TaggedResponse $ response ) => (
153- CommandFailedException ::make ($ this ->result ->command ()->redacted (), $ response )
157+ ImapCommandException ::make ($ this ->result ->command ()->redacted (), $ response )
154158 ));
155159 }
156160
@@ -177,7 +181,7 @@ public function authenticate(string $user, string $token): TaggedResponse
177181 $ this ->send ('AUTHENTICATE ' , ['XOAUTH2 ' , Str::credentials ($ user , $ token )], $ tag );
178182
179183 return $ this ->assertTaggedResponse ($ tag , fn (TaggedResponse $ response ) => (
180- CommandFailedException ::make ($ this ->result ->command ()->redacted (), $ response )
184+ ImapCommandException ::make ($ this ->result ->command ()->redacted (), $ response )
181185 ));
182186 }
183187
@@ -190,11 +194,7 @@ public function startTls(): void
190194
191195 $ this ->assertTaggedResponse ($ tag );
192196
193- $ this ->stream ->setSocketSetCrypto (true , match (true ) {
194- defined ('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT ' ) => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
195- defined ('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT ' ) => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT,
196- default => STREAM_CRYPTO_METHOD_TLS_CLIENT ,
197- });
197+ $ this ->stream ->setSocketSetCrypto (true , STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT);
198198 }
199199
200200 /**
@@ -522,7 +522,7 @@ public function idle(int $timeout): Generator
522522 $ this ->assertNextResponse (
523523 fn (Response $ response ) => $ response instanceof ContinuationResponse,
524524 fn (ContinuationResponse $ response ) => true ,
525- fn (ContinuationResponse $ response ) => CommandFailedException ::make (new ImapCommand ('' , 'IDLE ' ), $ response ),
525+ fn (ContinuationResponse $ response ) => ImapCommandException ::make (new ImapCommand ('' , 'IDLE ' ), $ response ),
526526 );
527527
528528 while ($ response = $ this ->nextReply ()) {
@@ -543,7 +543,7 @@ public function done(): void
543543 $ this ->assertNextResponse (
544544 fn (Response $ response ) => $ response instanceof TaggedResponse,
545545 fn (TaggedResponse $ response ) => $ response ->successful (),
546- fn (TaggedResponse $ response ) => CommandFailedException ::make (new ImapCommand ('' , 'DONE ' ), $ response ),
546+ fn (TaggedResponse $ response ) => ImapCommandException ::make (new ImapCommand ('' , 'DONE ' ), $ response ),
547547 );
548548 }
549549
@@ -575,7 +575,7 @@ public function send(string $name, array $tokens = [], ?string &$tag = null): vo
575575 protected function write (string $ data ): void
576576 {
577577 if ($ this ->stream ->fwrite ($ data ."\r\n" ) === false ) {
578- throw new RuntimeException ('Failed to write data to stream ' );
578+ throw new ImapStreamException ('Failed to write data to stream ' );
579579 }
580580
581581 $ this ->logger ?->sent($ data );
@@ -658,7 +658,7 @@ protected function assertTaggedResponse(string $tag, ?callable $exception = null
658658 $ response ->successful ()
659659 ),
660660 $ exception ?? fn (TaggedResponse $ response ) => (
661- CommandFailedException ::make ($ this ->result ->command (), $ response )
661+ ImapCommandException ::make ($ this ->result ->command (), $ response )
662662 ),
663663 );
664664 }
@@ -681,7 +681,7 @@ protected function assertNextResponse(callable $filter, callable $assertion, cal
681681 throw $ exception ($ response );
682682 }
683683
684- throw new RuntimeException ('No matching response found ' );
684+ throw new ImapResponseException ('No matching response found ' );
685685 }
686686
687687 /**
@@ -695,7 +695,7 @@ protected function assertNextResponse(callable $filter, callable $assertion, cal
695695 protected function nextResponse (callable $ filter ): Response
696696 {
697697 if (! $ this ->parser ) {
698- throw new RuntimeException ('No parser instance set ' );
698+ throw new LogicException ('No parser instance set ' );
699699 }
700700
701701 while ($ response = $ this ->nextReply ()) {
@@ -710,7 +710,7 @@ protected function nextResponse(callable $filter): Response
710710 }
711711 }
712712
713- throw new RuntimeException ('No matching response found ' );
713+ throw new ImapResponseException ('No matching response found ' );
714714 }
715715
716716 /**
@@ -722,9 +722,9 @@ protected function nextReply(): Data|Token|Response|null
722722 $ meta = $ this ->stream ->meta ();
723723
724724 throw match (true ) {
725- $ meta ['timed_out ' ] ?? false => new ConnectionTimedOutException ('Stream timed out, no response ' ),
726- $ meta ['eof ' ] ?? false => new ConnectionClosedException ('Server closed the connection (EOF) ' ),
727- default => new RuntimeException ('Unknown read error, no response : ' .json_encode ($ meta )),
725+ $ meta ['timed_out ' ] ?? false => new ImapConnectionTimedOutException ('Stream timed out, no response ' ),
726+ $ meta ['eof ' ] ?? false => new ImapConnectionClosedException ('Server closed the connection (EOF) ' ),
727+ default => new ImapConnectionException ('Unknown stream error. Metadata : ' .json_encode ($ meta )),
728728 };
729729 }
730730
0 commit comments