15
15
use ErrorException ;
16
16
use Webklex \PHPIMAP \Connection \Protocols \ImapProtocol ;
17
17
use Webklex \PHPIMAP \Connection \Protocols \LegacyProtocol ;
18
- use Webklex \PHPIMAP \Connection \Protocols \Protocol ;
19
18
use Webklex \PHPIMAP \Connection \Protocols \ProtocolInterface ;
20
19
use Webklex \PHPIMAP \Exceptions \AuthFailedException ;
21
20
use Webklex \PHPIMAP \Exceptions \ConnectionFailedException ;
21
+ use Webklex \PHPIMAP \Exceptions \EventNotFoundException ;
22
22
use Webklex \PHPIMAP \Exceptions \FolderFetchingException ;
23
+ use Webklex \PHPIMAP \Exceptions \ImapBadRequestException ;
24
+ use Webklex \PHPIMAP \Exceptions \ImapServerErrorException ;
23
25
use Webklex \PHPIMAP \Exceptions \MaskNotFoundException ;
24
26
use Webklex \PHPIMAP \Exceptions \ProtocolNotSupportedException ;
27
+ use Webklex \PHPIMAP \Exceptions \ResponseException ;
28
+ use Webklex \PHPIMAP \Exceptions \RuntimeException ;
25
29
use Webklex \PHPIMAP \Support \FolderCollection ;
26
30
use Webklex \PHPIMAP \Support \Masks \AttachmentMask ;
27
31
use Webklex \PHPIMAP \Support \Masks \MessageMask ;
@@ -182,6 +186,10 @@ public function __construct(array $config = []) {
182
186
183
187
/**
184
188
* Client destructor
189
+ *
190
+ * @throws ImapBadRequestException
191
+ * @throws ImapServerErrorException
192
+ * @throws RuntimeException
185
193
*/
186
194
public function __destruct () {
187
195
$ this ->disconnect ();
@@ -335,8 +343,13 @@ protected function setMaskFromConfig($config): void {
335
343
/**
336
344
* Get the current imap resource
337
345
*
338
- * @return bool|Protocol| ProtocolInterface
346
+ * @return ProtocolInterface
339
347
* @throws ConnectionFailedException
348
+ * @throws AuthFailedException
349
+ * @throws ImapBadRequestException
350
+ * @throws ImapServerErrorException
351
+ * @throws RuntimeException
352
+ * @throws ResponseException
340
353
*/
341
354
public function getConnection (): ProtocolInterface {
342
355
$ this ->checkConnection ();
@@ -356,6 +369,11 @@ public function isConnected(): bool {
356
369
* Determine if connection was established and connect if not.
357
370
*
358
371
* @throws ConnectionFailedException
372
+ * @throws AuthFailedException
373
+ * @throws ImapBadRequestException
374
+ * @throws ImapServerErrorException
375
+ * @throws RuntimeException
376
+ * @throws ResponseException
359
377
*/
360
378
public function checkConnection () {
361
379
if (!$ this ->isConnected ()) {
@@ -367,6 +385,11 @@ public function checkConnection() {
367
385
* Force the connection to reconnect
368
386
*
369
387
* @throws ConnectionFailedException
388
+ * @throws AuthFailedException
389
+ * @throws ImapBadRequestException
390
+ * @throws ImapServerErrorException
391
+ * @throws RuntimeException
392
+ * @throws ResponseException
370
393
*/
371
394
public function reconnect (): void {
372
395
if ($ this ->isConnected ()) {
@@ -380,6 +403,11 @@ public function reconnect(): void {
380
403
*
381
404
* @return $this
382
405
* @throws ConnectionFailedException
406
+ * @throws AuthFailedException
407
+ * @throws ImapBadRequestException
408
+ * @throws ImapServerErrorException
409
+ * @throws RuntimeException
410
+ * @throws ResponseException
383
411
*/
384
412
public function connect (): Client {
385
413
$ this ->disconnect ();
@@ -421,29 +449,31 @@ public function connect(): Client {
421
449
/**
422
450
* Authenticate the current session
423
451
*
424
- * @throws ConnectionFailedException
452
+ * @throws AuthFailedException
453
+ * @throws ImapBadRequestException
454
+ * @throws ImapServerErrorException
455
+ * @throws ResponseException
425
456
*/
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 ()) {
433
460
throw new AuthFailedException ();
434
461
}
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 ( );
437
464
}
438
465
}
439
466
440
467
/**
441
468
* Disconnect from server.
442
469
*
443
470
* @return $this
471
+ * @throws ImapBadRequestException
472
+ * @throws ImapServerErrorException
473
+ * @throws RuntimeException
444
474
*/
445
475
public function disconnect (): Client {
446
- if ($ this ->isConnected () && $ this -> connection !== false ) {
476
+ if ($ this ->isConnected ()) {
447
477
$ this ->connection ->logout ();
448
478
}
449
479
$ this ->active_folder = null ;
@@ -457,9 +487,13 @@ public function disconnect(): Client {
457
487
* @param string|null $delimiter
458
488
*
459
489
* @return Folder|null
460
- * @throws ConnectionFailedException
461
490
* @throws FolderFetchingException
462
- * @throws Exceptions\RuntimeException
491
+ * @throws ConnectionFailedException
492
+ * @throws AuthFailedException
493
+ * @throws ImapBadRequestException
494
+ * @throws ImapServerErrorException
495
+ * @throws RuntimeException
496
+ * @throws ResponseException
463
497
*/
464
498
public function getFolder (string $ folder_name , ?string $ delimiter = null ): ?Folder {
465
499
// 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
476
510
* @param $folder_name
477
511
*
478
512
* @return Folder|null
479
- * @throws ConnectionFailedException
480
513
* @throws FolderFetchingException
481
- * @throws Exceptions\RuntimeException
514
+ * @throws ConnectionFailedException
515
+ * @throws AuthFailedException
516
+ * @throws ImapBadRequestException
517
+ * @throws ImapServerErrorException
518
+ * @throws RuntimeException
519
+ * @throws ResponseException
482
520
*/
483
521
public function getFolderByName ($ folder_name ): ?Folder {
484
522
return $ this ->getFolders (false )->where ("name " , $ folder_name )->first ();
@@ -489,9 +527,13 @@ public function getFolderByName($folder_name): ?Folder {
489
527
* @param $folder_path
490
528
*
491
529
* @return Folder|null
492
- * @throws ConnectionFailedException
493
530
* @throws FolderFetchingException
494
- * @throws Exceptions\RuntimeException
531
+ * @throws ConnectionFailedException
532
+ * @throws AuthFailedException
533
+ * @throws ImapBadRequestException
534
+ * @throws ImapServerErrorException
535
+ * @throws RuntimeException
536
+ * @throws ResponseException
495
537
*/
496
538
public function getFolderByPath ($ folder_path ): ?Folder {
497
539
return $ this ->getFolders (false )->where ("path " , $ folder_path )->first ();
@@ -507,16 +549,20 @@ public function getFolderByPath($folder_path): ?Folder {
507
549
* @return FolderCollection
508
550
* @throws ConnectionFailedException
509
551
* @throws FolderFetchingException
510
- * @throws Exceptions\RuntimeException
552
+ * @throws AuthFailedException
553
+ * @throws ImapBadRequestException
554
+ * @throws ImapServerErrorException
555
+ * @throws RuntimeException
556
+ * @throws ResponseException
511
557
*/
512
558
public function getFolders (bool $ hierarchical = true , string $ parent_folder = null ): FolderCollection {
513
559
$ this ->checkConnection ();
514
560
$ folders = FolderCollection::make ([]);
515
561
516
562
$ pattern = $ parent_folder .($ hierarchical ? '% ' : '* ' );
517
- $ items = $ this ->connection ->folders ('' , $ pattern );
563
+ $ items = $ this ->connection ->folders ('' , $ pattern )-> validatedData () ;
518
564
519
- if (is_array ($ items )){
565
+ if (! empty ($ items )){
520
566
foreach ($ items as $ folder_name => $ item ) {
521
567
$ folder = new Folder ($ this , $ folder_name , $ item ["delimiter " ], $ item ["flags " ]);
522
568
@@ -544,18 +590,22 @@ public function getFolders(bool $hierarchical = true, string $parent_folder = nu
544
590
* @param string|null $parent_folder
545
591
*
546
592
* @return FolderCollection
547
- * @throws ConnectionFailedException
548
593
* @throws FolderFetchingException
549
- * @throws Exceptions\RuntimeException
594
+ * @throws ConnectionFailedException
595
+ * @throws AuthFailedException
596
+ * @throws ImapBadRequestException
597
+ * @throws ImapServerErrorException
598
+ * @throws RuntimeException
599
+ * @throws ResponseException
550
600
*/
551
601
public function getFoldersWithStatus (bool $ hierarchical = true , string $ parent_folder = null ): FolderCollection {
552
602
$ this ->checkConnection ();
553
603
$ folders = FolderCollection::make ([]);
554
604
555
605
$ pattern = $ parent_folder .($ hierarchical ? '% ' : '* ' );
556
- $ items = $ this ->connection ->folders ('' , $ pattern );
606
+ $ items = $ this ->connection ->folders ('' , $ pattern )-> validatedData () ;
557
607
558
- if (is_array ($ items )){
608
+ if (! empty ($ items )){
559
609
foreach ($ items as $ folder_name => $ item ) {
560
610
$ folder = new Folder ($ this , $ folder_name , $ item ["delimiter " ], $ item ["flags " ]);
561
611
@@ -581,17 +631,21 @@ public function getFoldersWithStatus(bool $hierarchical = true, string $parent_f
581
631
* @param string $folder_path
582
632
* @param boolean $force_select
583
633
*
584
- * @return array|bool
634
+ * @return array
585
635
* @throws ConnectionFailedException
586
- * @throws Exceptions\RuntimeException
636
+ * @throws AuthFailedException
637
+ * @throws ImapBadRequestException
638
+ * @throws ImapServerErrorException
639
+ * @throws RuntimeException
640
+ * @throws ResponseException
587
641
*/
588
642
public function openFolder (string $ folder_path , bool $ force_select = false ): array {
589
643
if ($ this ->active_folder == $ folder_path && $ this ->isConnected () && $ force_select === false ) {
590
644
return [];
591
645
}
592
646
$ this ->checkConnection ();
593
647
$ this ->active_folder = $ folder_path ;
594
- return $ this ->connection ->selectFolder ($ folder_path );
648
+ return $ this ->connection ->selectFolder ($ folder_path )-> validatedData () ;
595
649
}
596
650
597
651
/**
@@ -601,17 +655,21 @@ public function openFolder(string $folder_path, bool $force_select = false): arr
601
655
*
602
656
* @return Folder
603
657
* @throws ConnectionFailedException
658
+ * @throws AuthFailedException
659
+ * @throws ImapBadRequestException
660
+ * @throws ImapServerErrorException
661
+ * @throws RuntimeException
662
+ * @throws EventNotFoundException
604
663
* @throws FolderFetchingException
605
- * @throws Exceptions\EventNotFoundException
606
- * @throws Exceptions\RuntimeException
664
+ * @throws ResponseException
607
665
*/
608
666
public function createFolder (string $ folder_path , bool $ expunge = true ): Folder {
609
667
$ this ->checkConnection ();
610
- $ status = $ this ->connection ->createFolder ($ folder );
668
+ $ status = $ this ->connection ->createFolder ($ folder_path )-> validatedData ( );
611
669
612
670
if ($ expunge ) $ this ->expunge ();
613
671
614
- $ folder = $ this ->getFolderByPath ($ folder );
672
+ $ folder = $ this ->getFolderByPath ($ folder_path );
615
673
if ($ status && $ folder ) {
616
674
$ event = $ this ->getEvent ("folder " , "new " );
617
675
$ event ::dispatch ($ folder );
@@ -626,11 +684,15 @@ public function createFolder(string $folder_path, bool $expunge = true): Folder
626
684
*
627
685
* @return array
628
686
* @throws ConnectionFailedException
629
- * @throws Exceptions\RuntimeException
687
+ * @throws AuthFailedException
688
+ * @throws ImapBadRequestException
689
+ * @throws ImapServerErrorException
690
+ * @throws RuntimeException
691
+ * @throws ResponseException
630
692
*/
631
693
public function checkFolder (string $ folder_path ): array {
632
694
$ this ->checkConnection ();
633
- return $ this ->connection ->examineFolder ($ folder );
695
+ return $ this ->connection ->examineFolder ($ folder_path )-> validatedData ( );
634
696
}
635
697
636
698
/**
@@ -650,23 +712,31 @@ public function getFolderPath(): string {
650
712
* @return array
651
713
*
652
714
* @throws ConnectionFailedException
653
- * @throws Exceptions\RuntimeException
715
+ * @throws AuthFailedException
716
+ * @throws ImapBadRequestException
717
+ * @throws ImapServerErrorException
718
+ * @throws RuntimeException
719
+ * @throws ResponseException
654
720
*/
655
721
public function Id (array $ ids = null ): array {
656
722
$ this ->checkConnection ();
657
- return $ this ->connection ->ID ($ ids );
723
+ return $ this ->connection ->ID ($ ids )-> validatedData () ;
658
724
}
659
725
660
726
/**
661
727
* Retrieve the quota level settings, and usage statics per mailbox
662
728
*
663
729
* @return array
664
730
* @throws ConnectionFailedException
665
- * @throws Exceptions\RuntimeException
731
+ * @throws AuthFailedException
732
+ * @throws ImapBadRequestException
733
+ * @throws ImapServerErrorException
734
+ * @throws RuntimeException
735
+ * @throws ResponseException
666
736
*/
667
737
public function getQuota (): array {
668
738
$ this ->checkConnection ();
669
- return $ this ->connection ->getQuota ($ this ->username );
739
+ return $ this ->connection ->getQuota ($ this ->username )-> validatedData () ;
670
740
}
671
741
672
742
/**
@@ -675,32 +745,46 @@ public function getQuota(): array {
675
745
*
676
746
* @return array
677
747
* @throws ConnectionFailedException
748
+ * @throws AuthFailedException
749
+ * @throws ImapBadRequestException
750
+ * @throws ImapServerErrorException
751
+ * @throws RuntimeException
752
+ * @throws ResponseException
678
753
*/
679
754
public function getQuotaRoot (string $ quota_root = 'INBOX ' ): array {
680
755
$ this ->checkConnection ();
681
- return $ this ->connection ->getQuotaRoot ($ quota_root );
756
+ return $ this ->connection ->getQuotaRoot ($ quota_root )-> validatedData () ;
682
757
}
683
758
684
759
/**
685
760
* Delete all messages marked for deletion
686
761
*
687
762
* @return array
688
763
* @throws ConnectionFailedException
689
- * @throws Exceptions\RuntimeException
764
+ * @throws ImapBadRequestException
765
+ * @throws ImapServerErrorException
766
+ * @throws RuntimeException
767
+ * @throws AuthFailedException
768
+ * @throws ResponseException
690
769
*/
691
770
public function expunge (): array {
692
771
$ this ->checkConnection ();
693
- return $ this ->connection ->expunge ();
772
+ return $ this ->connection ->expunge ()-> validatedData () ;
694
773
}
695
774
696
775
/**
697
776
* Set the connection timeout
698
777
* @param integer $timeout
699
778
*
700
- * @return Protocol
779
+ * @return ProtocolInterface
701
780
* @throws ConnectionFailedException
781
+ * @throws AuthFailedException
782
+ * @throws ImapBadRequestException
783
+ * @throws ImapServerErrorException
784
+ * @throws RuntimeException
785
+ * @throws ResponseException
702
786
*/
703
- public function setTimeout (int $ timeout ): Protocol {
787
+ public function setTimeout (int $ timeout ): ProtocolInterface {
704
788
$ this ->timeout = $ timeout ;
705
789
if ($ this ->isConnected ()) {
706
790
$ this ->connection ->setConnectionTimeout ($ timeout );
@@ -714,6 +798,11 @@ public function setTimeout(int $timeout): Protocol {
714
798
*
715
799
* @return int
716
800
* @throws ConnectionFailedException
801
+ * @throws AuthFailedException
802
+ * @throws ImapBadRequestException
803
+ * @throws ImapServerErrorException
804
+ * @throws RuntimeException
805
+ * @throws ResponseException
717
806
*/
718
807
public function getTimeout (): int {
719
808
$ this ->checkConnection ();
0 commit comments