@@ -621,7 +621,13 @@ public function getQuery(): string
621621 */
622622 protected function search (): Collection
623623 {
624- $ response = $ this ->connection ()->search ([$ this ->getQuery ()]);
624+ if (empty ($ this ->conditions )) {
625+ $ this ->all ();
626+ }
627+
628+ $ response = $ this ->connection ()->search ([
629+ $ this ->getQuery (),
630+ ]);
625631
626632 return new Collection (array_map (
627633 fn (Atom $ token ) => $ token ->value ,
@@ -752,10 +758,6 @@ public function first(): ?Message
752758 */
753759 public function get (): MessageCollection
754760 {
755- if (empty ($ this ->conditions )) {
756- $ this ->all ();
757- }
758-
759761 return $ this ->process ($ this ->search ());
760762 }
761763
@@ -764,45 +766,48 @@ public function get(): MessageCollection
764766 */
765767 public function chunk (callable $ callback , int $ chunkSize = 10 , int $ startChunk = 1 ): void
766768 {
767- $ messages = $ this ->search ();
768-
769769 $ startChunk = max ($ startChunk , 1 );
770770 $ chunkSize = max ($ chunkSize , 1 );
771771
772- $ count = max ($ messages ->count () - ($ chunkSize * ($ startChunk - 1 )), 0 );
772+ // Get all search result tokens once.
773+ $ messages = $ this ->search ();
774+
775+ // Calculate how many chunks there are
776+ $ totalChunks = (int ) ceil ($ messages ->count () / $ chunkSize );
773777
774- if (! $ count ) {
778+ // If startChunk is beyond our total chunks, return early.
779+ if ($ startChunk > $ totalChunks ) {
775780 return ;
776781 }
777782
783+ // Save previous state to restore later.
778784 $ previousLimit = $ this ->limit ;
779785 $ previousPage = $ this ->page ;
780786
781787 $ this ->limit = $ chunkSize ;
782- $ this ->page = $ startChunk ;
783788
784- $ handledMessagesCount = 0 ;
789+ // Iterate from the starting chunk to the last chunk.
790+ for ($ page = $ startChunk ; $ page <= $ totalChunks ; $ page ++) {
791+ $ this ->page = $ page ;
785792
786- do {
793+ // populate() will use $this->page to slice the results.
787794 $ hydrated = $ this ->populate ($ messages );
788795
789- $ handledMessagesCount += $ hydrated ->count ();
790-
791- $ callback ($ hydrated , $ this ->page );
796+ // If no messages are returned, break out to prevent infinite loop.
797+ if ($ hydrated ->isEmpty ()) {
798+ break ;
799+ }
792800
793- $ this -> page ++ ;
794- } while ( $ handledMessagesCount < $ count );
801+ $ callback ( $ hydrated , $ page) ;
802+ }
795803
804+ // Restore the original state.
796805 $ this ->limit = $ previousLimit ;
797806 $ this ->page = $ previousPage ;
798807 }
799808
800809 /**
801810 * Paginate the current query.
802- *
803- * @param int $perPage Results you which to receive per page
804- * @param null $page The current page you are on (e.g. 0, 1, 2, ...) use `null` to enable auto mode
805- * @param string $pageName The page name / uri parameter used for the generated links and the auto mode
806811 */
807812 public function paginate (int $ perPage = 5 , $ page = null , string $ pageName = 'page ' ): LengthAwarePaginator
808813 {
0 commit comments