@@ -519,6 +519,7 @@ public function getFolder(string $folder_name, ?string $delimiter = null, bool $
519519 /**
520520 * Get a folder instance by a folder name
521521 * @param $folder_name
522+ * @param bool $soft_fail If true, it will return null instead of throwing an exception
522523 *
523524 * @return Folder|null
524525 * @throws FolderFetchingException
@@ -529,14 +530,16 @@ public function getFolder(string $folder_name, ?string $delimiter = null, bool $
529530 * @throws RuntimeException
530531 * @throws ResponseException
531532 */
532- public function getFolderByName ($ folder_name ): ?Folder {
533- return $ this ->getFolders (false )->where ("name " , $ folder_name )->first ();
533+ public function getFolderByName ($ folder_name, bool $ soft_fail = false ): ?Folder {
534+ return $ this ->getFolders (false , null , $ soft_fail )->where ("name " , $ folder_name )->first ();
534535 }
535536
536537 /**
537538 * Get a folder instance by a folder path
538539 * @param $folder_path
539540 * @param bool $utf7
541+ * @param bool $soft_fail If true, it will return null instead of throwing an exception
542+ *
540543 * @return Folder|null
541544 * @throws AuthFailedException
542545 * @throws ConnectionFailedException
@@ -546,9 +549,9 @@ public function getFolderByName($folder_name): ?Folder {
546549 * @throws ResponseException
547550 * @throws RuntimeException
548551 */
549- public function getFolderByPath ($ folder_path , bool $ utf7 = false ): ?Folder {
552+ public function getFolderByPath ($ folder_path , bool $ utf7 = false , bool $ soft_fail = false ): ?Folder {
550553 if (!$ utf7 ) $ folder_path = EncodingAliases::convert ($ folder_path , "utf-8 " , "utf7-imap " );
551- return $ this ->getFolders (false )->where ("path " , $ folder_path )->first ();
554+ return $ this ->getFolders (false , null , $ soft_fail )->where ("path " , $ folder_path )->first ();
552555 }
553556
554557 /**
@@ -557,17 +560,18 @@ public function getFolderByPath($folder_path, bool $utf7 = false): ?Folder {
557560 *
558561 * @param boolean $hierarchical
559562 * @param string|null $parent_folder
563+ * @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
560564 *
561565 * @return FolderCollection
566+ * @throws AuthFailedException
562567 * @throws ConnectionFailedException
563568 * @throws FolderFetchingException
564- * @throws AuthFailedException
565569 * @throws ImapBadRequestException
566570 * @throws ImapServerErrorException
567- * @throws RuntimeException
568571 * @throws ResponseException
572+ * @throws RuntimeException
569573 */
570- public function getFolders (bool $ hierarchical = true , string $ parent_folder = null ): FolderCollection {
574+ public function getFolders (bool $ hierarchical = true , string $ parent_folder = null , bool $ soft_fail = false ): FolderCollection {
571575 $ this ->checkConnection ();
572576 $ folders = FolderCollection::make ([]);
573577
@@ -581,17 +585,19 @@ public function getFolders(bool $hierarchical = true, string $parent_folder = nu
581585 if ($ hierarchical && $ folder ->hasChildren ()) {
582586 $ pattern = $ folder ->full_name .$ folder ->delimiter .'% ' ;
583587
584- $ children = $ this ->getFolders (true , $ pattern );
588+ $ children = $ this ->getFolders (true , $ pattern, $ soft_fail );
585589 $ folder ->setChildren ($ children );
586590 }
587591
588592 $ folders ->push ($ folder );
589593 }
590594
591595 return $ folders ;
592- }else {
596+ }else if (! $ soft_fail ) {
593597 throw new FolderFetchingException ("failed to fetch any folders " );
594598 }
599+
600+ return $ folders ;
595601 }
596602
597603 /**
@@ -600,6 +606,7 @@ public function getFolders(bool $hierarchical = true, string $parent_folder = nu
600606 *
601607 * @param boolean $hierarchical
602608 * @param string|null $parent_folder
609+ * @param bool $soft_fail If true, it will return an empty collection instead of throwing an exception
603610 *
604611 * @return FolderCollection
605612 * @throws FolderFetchingException
@@ -610,7 +617,7 @@ public function getFolders(bool $hierarchical = true, string $parent_folder = nu
610617 * @throws RuntimeException
611618 * @throws ResponseException
612619 */
613- public function getFoldersWithStatus (bool $ hierarchical = true , string $ parent_folder = null ): FolderCollection {
620+ public function getFoldersWithStatus (bool $ hierarchical = true , string $ parent_folder = null , bool $ soft_fail = false ): FolderCollection {
614621 $ this ->checkConnection ();
615622 $ folders = FolderCollection::make ([]);
616623
@@ -624,7 +631,7 @@ public function getFoldersWithStatus(bool $hierarchical = true, string $parent_f
624631 if ($ hierarchical && $ folder ->hasChildren ()) {
625632 $ pattern = $ folder ->full_name .$ folder ->delimiter .'% ' ;
626633
627- $ children = $ this ->getFoldersWithStatus (true , $ pattern );
634+ $ children = $ this ->getFoldersWithStatus (true , $ pattern, $ soft_fail );
628635 $ folder ->setChildren ($ children );
629636 }
630637
@@ -633,9 +640,11 @@ public function getFoldersWithStatus(bool $hierarchical = true, string $parent_f
633640 }
634641
635642 return $ folders ;
636- }else {
643+ }else if (! $ soft_fail ) {
637644 throw new FolderFetchingException ("failed to fetch any folders " );
638645 }
646+
647+ return $ folders ;
639648 }
640649
641650 /**
0 commit comments