|
3 | 3 | namespace Lomkit\Rest\Actions; |
4 | 4 |
|
5 | 5 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 6 | +use Illuminate\Database\Eloquent\Builder; |
6 | 7 | use Illuminate\Support\Collection; |
7 | 8 | use Illuminate\Support\Facades\Bus; |
8 | 9 | use Illuminate\Support\Facades\DB; |
@@ -109,32 +110,50 @@ public function dispatch($chunkCount) |
109 | 110 | } |
110 | 111 |
|
111 | 112 | /** |
112 | | - * Dispatch the given action. |
| 113 | + * Processes models in chunks using classic mode and dispatches an action for each set. |
113 | 114 | * |
114 | | - * @param int $chunkCount |
| 115 | + * The method builds a search query for the resource associated with the current request, applying |
| 116 | + * search criteria from the request input and removing any default result limits. It then processes |
| 117 | + * the query results in chunks (of size $chunkCount) by invoking the forModels method on each chunk. |
| 118 | + * Finally, it returns the query limit if one is set; otherwise, it returns the total count of models. |
115 | 119 | * |
116 | | - * @return int |
| 120 | + * @param int $chunkCount The number of models to process per chunk. |
| 121 | + * |
| 122 | + * @return int The effective result limit if set, or the total count of models. |
117 | 123 | */ |
118 | 124 | public function handleClassic(int $chunkCount) |
119 | 125 | { |
| 126 | + /** |
| 127 | + * @var Builder $searchQuery |
| 128 | + */ |
120 | 129 | $searchQuery = |
121 | 130 | app()->make(QueryBuilder::class, ['resource' => $this->request->resource, 'query' => null]) |
| 131 | + ->disableDefaultLimit() |
122 | 132 | ->search($this->request->input('search', [])); |
123 | 133 |
|
| 134 | + $limit = $searchQuery->toBase()->limit; |
| 135 | + |
124 | 136 | $searchQuery |
125 | 137 | ->clone() |
126 | 138 | ->chunk( |
127 | 139 | $chunkCount, |
128 | | - function ($chunk) { |
129 | | - return $this->forModels( |
130 | | - \Illuminate\Database\Eloquent\Collection::make( |
131 | | - $chunk |
132 | | - ) |
133 | | - ); |
| 140 | + function ($chunk, $page) use ($limit, $chunkCount) { |
| 141 | + $collection = \Illuminate\Database\Eloquent\Collection::make($chunk); |
| 142 | + |
| 143 | + // This is to remove for Laravel 12, chunking with limit does not work |
| 144 | + // in Laravel 11 |
| 145 | + if (!is_null($limit) && $page * $chunkCount >= $limit) { |
| 146 | + $collection = $collection->take($limit - ($page - 1) * $chunkCount); |
| 147 | + $this->forModels($collection); |
| 148 | + |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + return $this->forModels($collection); |
134 | 153 | } |
135 | 154 | ); |
136 | 155 |
|
137 | | - return $searchQuery->count(); |
| 156 | + return $limit ?? $searchQuery->count(); |
138 | 157 | } |
139 | 158 |
|
140 | 159 | /** |
@@ -202,7 +221,7 @@ protected function dispatchSynchronouslyForCollection(Collection $models) |
202 | 221 | * |
203 | 222 | * @return $this |
204 | 223 | */ |
205 | | - protected function addQueuedActionJob(Collection $models) |
| 224 | + protected function addQueuedActionJob(Collection $models): self |
206 | 225 | { |
207 | 226 | $job = new CallRestApiAction($this->action, $this->fields, $models); |
208 | 227 |
|
|
0 commit comments