Skip to content

Commit 0c53c6a

Browse files
committed
Protocol Response class introduced to handle and unify all protocol requests
1 parent 021402a commit 0c53c6a

File tree

9 files changed

+1623
-657
lines changed

9 files changed

+1623
-657
lines changed

src/Client.php

Lines changed: 133 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
use ErrorException;
1616
use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol;
1717
use Webklex\PHPIMAP\Connection\Protocols\LegacyProtocol;
18-
use Webklex\PHPIMAP\Connection\Protocols\Protocol;
1918
use Webklex\PHPIMAP\Connection\Protocols\ProtocolInterface;
2019
use Webklex\PHPIMAP\Exceptions\AuthFailedException;
2120
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
21+
use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
2222
use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
23+
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
24+
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
2325
use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
2426
use Webklex\PHPIMAP\Exceptions\ProtocolNotSupportedException;
27+
use Webklex\PHPIMAP\Exceptions\ResponseException;
28+
use Webklex\PHPIMAP\Exceptions\RuntimeException;
2529
use Webklex\PHPIMAP\Support\FolderCollection;
2630
use Webklex\PHPIMAP\Support\Masks\AttachmentMask;
2731
use Webklex\PHPIMAP\Support\Masks\MessageMask;
@@ -182,6 +186,10 @@ public function __construct(array $config = []) {
182186

183187
/**
184188
* Client destructor
189+
*
190+
* @throws ImapBadRequestException
191+
* @throws ImapServerErrorException
192+
* @throws RuntimeException
185193
*/
186194
public function __destruct() {
187195
$this->disconnect();
@@ -335,8 +343,13 @@ protected function setMaskFromConfig($config): void {
335343
/**
336344
* Get the current imap resource
337345
*
338-
* @return bool|Protocol|ProtocolInterface
346+
* @return ProtocolInterface
339347
* @throws ConnectionFailedException
348+
* @throws AuthFailedException
349+
* @throws ImapBadRequestException
350+
* @throws ImapServerErrorException
351+
* @throws RuntimeException
352+
* @throws ResponseException
340353
*/
341354
public function getConnection(): ProtocolInterface {
342355
$this->checkConnection();
@@ -356,6 +369,11 @@ public function isConnected(): bool {
356369
* Determine if connection was established and connect if not.
357370
*
358371
* @throws ConnectionFailedException
372+
* @throws AuthFailedException
373+
* @throws ImapBadRequestException
374+
* @throws ImapServerErrorException
375+
* @throws RuntimeException
376+
* @throws ResponseException
359377
*/
360378
public function checkConnection() {
361379
if (!$this->isConnected()) {
@@ -367,6 +385,11 @@ public function checkConnection() {
367385
* Force the connection to reconnect
368386
*
369387
* @throws ConnectionFailedException
388+
* @throws AuthFailedException
389+
* @throws ImapBadRequestException
390+
* @throws ImapServerErrorException
391+
* @throws RuntimeException
392+
* @throws ResponseException
370393
*/
371394
public function reconnect(): void {
372395
if ($this->isConnected()) {
@@ -380,6 +403,11 @@ public function reconnect(): void {
380403
*
381404
* @return $this
382405
* @throws ConnectionFailedException
406+
* @throws AuthFailedException
407+
* @throws ImapBadRequestException
408+
* @throws ImapServerErrorException
409+
* @throws RuntimeException
410+
* @throws ResponseException
383411
*/
384412
public function connect(): Client {
385413
$this->disconnect();
@@ -421,29 +449,31 @@ public function connect(): Client {
421449
/**
422450
* Authenticate the current session
423451
*
424-
* @throws ConnectionFailedException
452+
* @throws AuthFailedException
453+
* @throws ImapBadRequestException
454+
* @throws ImapServerErrorException
455+
* @throws ResponseException
425456
*/
426-
protected function authenticate() {
427-
try {
428-
if ($this->authentication == "oauth") {
429-
if (!$this->connection->authenticate($this->username, $this->password)) {
430-
throw new AuthFailedException();
431-
}
432-
} elseif (!$this->connection->login($this->username, $this->password)) {
457+
protected function authenticate(): void {
458+
if ($this->authentication == "oauth") {
459+
if (!$this->connection->authenticate($this->username, $this->password)->validatedData()) {
433460
throw new AuthFailedException();
434461
}
435-
} catch (AuthFailedException $e) {
436-
throw new ConnectionFailedException("connection setup failed", 0, $e);
462+
} elseif (!$this->connection->login($this->username, $this->password)->validatedData()) {
463+
throw new AuthFailedException();
437464
}
438465
}
439466

440467
/**
441468
* Disconnect from server.
442469
*
443470
* @return $this
471+
* @throws ImapBadRequestException
472+
* @throws ImapServerErrorException
473+
* @throws RuntimeException
444474
*/
445475
public function disconnect(): Client {
446-
if ($this->isConnected() && $this->connection !== false) {
476+
if ($this->isConnected()) {
447477
$this->connection->logout();
448478
}
449479
$this->active_folder = null;
@@ -457,9 +487,13 @@ public function disconnect(): Client {
457487
* @param string|null $delimiter
458488
*
459489
* @return Folder|null
460-
* @throws ConnectionFailedException
461490
* @throws FolderFetchingException
462-
* @throws Exceptions\RuntimeException
491+
* @throws ConnectionFailedException
492+
* @throws AuthFailedException
493+
* @throws ImapBadRequestException
494+
* @throws ImapServerErrorException
495+
* @throws RuntimeException
496+
* @throws ResponseException
463497
*/
464498
public function getFolder(string $folder_name, ?string $delimiter = null): ?Folder {
465499
// Set delimiter to false to force selection via getFolderByName (maybe useful for uncommon folder names)
@@ -476,9 +510,13 @@ public function getFolder(string $folder_name, ?string $delimiter = null): ?Fold
476510
* @param $folder_name
477511
*
478512
* @return Folder|null
479-
* @throws ConnectionFailedException
480513
* @throws FolderFetchingException
481-
* @throws Exceptions\RuntimeException
514+
* @throws ConnectionFailedException
515+
* @throws AuthFailedException
516+
* @throws ImapBadRequestException
517+
* @throws ImapServerErrorException
518+
* @throws RuntimeException
519+
* @throws ResponseException
482520
*/
483521
public function getFolderByName($folder_name): ?Folder {
484522
return $this->getFolders(false)->where("name", $folder_name)->first();
@@ -489,9 +527,13 @@ public function getFolderByName($folder_name): ?Folder {
489527
* @param $folder_path
490528
*
491529
* @return Folder|null
492-
* @throws ConnectionFailedException
493530
* @throws FolderFetchingException
494-
* @throws Exceptions\RuntimeException
531+
* @throws ConnectionFailedException
532+
* @throws AuthFailedException
533+
* @throws ImapBadRequestException
534+
* @throws ImapServerErrorException
535+
* @throws RuntimeException
536+
* @throws ResponseException
495537
*/
496538
public function getFolderByPath($folder_path): ?Folder {
497539
return $this->getFolders(false)->where("path", $folder_path)->first();
@@ -507,16 +549,20 @@ public function getFolderByPath($folder_path): ?Folder {
507549
* @return FolderCollection
508550
* @throws ConnectionFailedException
509551
* @throws FolderFetchingException
510-
* @throws Exceptions\RuntimeException
552+
* @throws AuthFailedException
553+
* @throws ImapBadRequestException
554+
* @throws ImapServerErrorException
555+
* @throws RuntimeException
556+
* @throws ResponseException
511557
*/
512558
public function getFolders(bool $hierarchical = true, string $parent_folder = null): FolderCollection {
513559
$this->checkConnection();
514560
$folders = FolderCollection::make([]);
515561

516562
$pattern = $parent_folder.($hierarchical ? '%' : '*');
517-
$items = $this->connection->folders('', $pattern);
563+
$items = $this->connection->folders('', $pattern)->validatedData();
518564

519-
if(is_array($items)){
565+
if(!empty($items)){
520566
foreach ($items as $folder_name => $item) {
521567
$folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);
522568

@@ -544,18 +590,22 @@ public function getFolders(bool $hierarchical = true, string $parent_folder = nu
544590
* @param string|null $parent_folder
545591
*
546592
* @return FolderCollection
547-
* @throws ConnectionFailedException
548593
* @throws FolderFetchingException
549-
* @throws Exceptions\RuntimeException
594+
* @throws ConnectionFailedException
595+
* @throws AuthFailedException
596+
* @throws ImapBadRequestException
597+
* @throws ImapServerErrorException
598+
* @throws RuntimeException
599+
* @throws ResponseException
550600
*/
551601
public function getFoldersWithStatus(bool $hierarchical = true, string $parent_folder = null): FolderCollection {
552602
$this->checkConnection();
553603
$folders = FolderCollection::make([]);
554604

555605
$pattern = $parent_folder.($hierarchical ? '%' : '*');
556-
$items = $this->connection->folders('', $pattern);
606+
$items = $this->connection->folders('', $pattern)->validatedData();
557607

558-
if(is_array($items)){
608+
if(!empty($items)){
559609
foreach ($items as $folder_name => $item) {
560610
$folder = new Folder($this, $folder_name, $item["delimiter"], $item["flags"]);
561611

@@ -581,17 +631,21 @@ public function getFoldersWithStatus(bool $hierarchical = true, string $parent_f
581631
* @param string $folder_path
582632
* @param boolean $force_select
583633
*
584-
* @return array|bool
634+
* @return array
585635
* @throws ConnectionFailedException
586-
* @throws Exceptions\RuntimeException
636+
* @throws AuthFailedException
637+
* @throws ImapBadRequestException
638+
* @throws ImapServerErrorException
639+
* @throws RuntimeException
640+
* @throws ResponseException
587641
*/
588642
public function openFolder(string $folder_path, bool $force_select = false): array {
589643
if ($this->active_folder == $folder_path && $this->isConnected() && $force_select === false) {
590644
return [];
591645
}
592646
$this->checkConnection();
593647
$this->active_folder = $folder_path;
594-
return $this->connection->selectFolder($folder_path);
648+
return $this->connection->selectFolder($folder_path)->validatedData();
595649
}
596650

597651
/**
@@ -601,17 +655,21 @@ public function openFolder(string $folder_path, bool $force_select = false): arr
601655
*
602656
* @return Folder
603657
* @throws ConnectionFailedException
658+
* @throws AuthFailedException
659+
* @throws ImapBadRequestException
660+
* @throws ImapServerErrorException
661+
* @throws RuntimeException
662+
* @throws EventNotFoundException
604663
* @throws FolderFetchingException
605-
* @throws Exceptions\EventNotFoundException
606-
* @throws Exceptions\RuntimeException
664+
* @throws ResponseException
607665
*/
608666
public function createFolder(string $folder_path, bool $expunge = true): Folder {
609667
$this->checkConnection();
610-
$status = $this->connection->createFolder($folder);
668+
$status = $this->connection->createFolder($folder_path)->validatedData();
611669

612670
if($expunge) $this->expunge();
613671

614-
$folder = $this->getFolderByPath($folder);
672+
$folder = $this->getFolderByPath($folder_path);
615673
if($status && $folder) {
616674
$event = $this->getEvent("folder", "new");
617675
$event::dispatch($folder);
@@ -626,11 +684,15 @@ public function createFolder(string $folder_path, bool $expunge = true): Folder
626684
*
627685
* @return array
628686
* @throws ConnectionFailedException
629-
* @throws Exceptions\RuntimeException
687+
* @throws AuthFailedException
688+
* @throws ImapBadRequestException
689+
* @throws ImapServerErrorException
690+
* @throws RuntimeException
691+
* @throws ResponseException
630692
*/
631693
public function checkFolder(string $folder_path): array {
632694
$this->checkConnection();
633-
return $this->connection->examineFolder($folder);
695+
return $this->connection->examineFolder($folder_path)->validatedData();
634696
}
635697

636698
/**
@@ -650,23 +712,31 @@ public function getFolderPath(): string {
650712
* @return array
651713
*
652714
* @throws ConnectionFailedException
653-
* @throws Exceptions\RuntimeException
715+
* @throws AuthFailedException
716+
* @throws ImapBadRequestException
717+
* @throws ImapServerErrorException
718+
* @throws RuntimeException
719+
* @throws ResponseException
654720
*/
655721
public function Id(array $ids = null): array {
656722
$this->checkConnection();
657-
return $this->connection->ID($ids);
723+
return $this->connection->ID($ids)->validatedData();
658724
}
659725

660726
/**
661727
* Retrieve the quota level settings, and usage statics per mailbox
662728
*
663729
* @return array
664730
* @throws ConnectionFailedException
665-
* @throws Exceptions\RuntimeException
731+
* @throws AuthFailedException
732+
* @throws ImapBadRequestException
733+
* @throws ImapServerErrorException
734+
* @throws RuntimeException
735+
* @throws ResponseException
666736
*/
667737
public function getQuota(): array {
668738
$this->checkConnection();
669-
return $this->connection->getQuota($this->username);
739+
return $this->connection->getQuota($this->username)->validatedData();
670740
}
671741

672742
/**
@@ -675,32 +745,46 @@ public function getQuota(): array {
675745
*
676746
* @return array
677747
* @throws ConnectionFailedException
748+
* @throws AuthFailedException
749+
* @throws ImapBadRequestException
750+
* @throws ImapServerErrorException
751+
* @throws RuntimeException
752+
* @throws ResponseException
678753
*/
679754
public function getQuotaRoot(string $quota_root = 'INBOX'): array {
680755
$this->checkConnection();
681-
return $this->connection->getQuotaRoot($quota_root);
756+
return $this->connection->getQuotaRoot($quota_root)->validatedData();
682757
}
683758

684759
/**
685760
* Delete all messages marked for deletion
686761
*
687762
* @return array
688763
* @throws ConnectionFailedException
689-
* @throws Exceptions\RuntimeException
764+
* @throws ImapBadRequestException
765+
* @throws ImapServerErrorException
766+
* @throws RuntimeException
767+
* @throws AuthFailedException
768+
* @throws ResponseException
690769
*/
691770
public function expunge(): array {
692771
$this->checkConnection();
693-
return $this->connection->expunge();
772+
return $this->connection->expunge()->validatedData();
694773
}
695774

696775
/**
697776
* Set the connection timeout
698777
* @param integer $timeout
699778
*
700-
* @return Protocol
779+
* @return ProtocolInterface
701780
* @throws ConnectionFailedException
781+
* @throws AuthFailedException
782+
* @throws ImapBadRequestException
783+
* @throws ImapServerErrorException
784+
* @throws RuntimeException
785+
* @throws ResponseException
702786
*/
703-
public function setTimeout(int $timeout): Protocol {
787+
public function setTimeout(int $timeout): ProtocolInterface {
704788
$this->timeout = $timeout;
705789
if ($this->isConnected()) {
706790
$this->connection->setConnectionTimeout($timeout);
@@ -714,6 +798,11 @@ public function setTimeout(int $timeout): Protocol {
714798
*
715799
* @return int
716800
* @throws ConnectionFailedException
801+
* @throws AuthFailedException
802+
* @throws ImapBadRequestException
803+
* @throws ImapServerErrorException
804+
* @throws RuntimeException
805+
* @throws ResponseException
717806
*/
718807
public function getTimeout(): int {
719808
$this->checkConnection();

0 commit comments

Comments
 (0)