From 7a86b1e7f52cbf45b3a8b8b4dba7b51c003bba66 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Wed, 2 Aug 2023 13:48:22 +0200 Subject: [PATCH] Fix for code incorrectly assuming you have at least 2x pageSize amount of reviews (#6) --- src/Actions/HarvestReviews.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Actions/HarvestReviews.php b/src/Actions/HarvestReviews.php index ed24f84..5a35c97 100644 --- a/src/Actions/HarvestReviews.php +++ b/src/Actions/HarvestReviews.php @@ -81,17 +81,20 @@ public function harvest(): void $total = $firstResponse->json('count')['total']; - $responses = Http::pool(function (Pool $pool) use ($total, $pageSize, $auth) { - foreach (range($pageSize, $total, $pageSize) as $page) { - $pools[] = $pool->withHeaders($auth)->get(config('feedback-company.api_url') . '/review', [ - 'limit' => $pageSize, - 'start' => $page, - ]); - } - return $pools; - }); + if($total > $pageSize) { + $responses = Http::pool(function (Pool $pool) use ($total, $pageSize, $auth) { + $pageRange = $total > $pageSize * 2 ? range($pageSize, $total, $pageSize) : [ $pageSize ]; + foreach ($pageRange as $page) { + $pools[] = $pool->withHeaders($auth)->get(config('feedback-company.api_url') . '/review', [ + 'limit' => $pageSize, + 'start' => $page, + ]); + } + return $pools; + }); + } - foreach(array_merge([$firstResponse], $responses) as $response) { + foreach(array_merge([$firstResponse], $responses ?? []) as $response) { $this->saveReviews($response->json('reviews')); }