@@ -37,19 +37,14 @@ public function get( $limit = 50, $before = null ): array
37
37
// Determine the starting index
38
38
$ startIndex = 0 ;
39
39
if ( $ before !== null ) {
40
- // Fetch a range of batch IDs surrounding the 'before' value
41
- // Adjust the range size as needed for efficiency
42
40
$ rangeSize = 100 ;
43
41
$ allBatchIds = Redis::lrange ( 'batches_list ' , 0 , $ rangeSize - 1 );
44
42
$ beforeIndex = array_search ( $ before , $ allBatchIds );
45
43
46
44
if ( $ beforeIndex !== false ) {
47
45
$ startIndex = $ beforeIndex + 1 ;
48
- } else {
49
- // TODO:
50
- // Handle case where 'before' is not found in the initial range
51
- // Consider fetching additional ranges or handling this scenario differently
52
46
}
47
+ // Handle cases where 'before' is not found or other scenarios
53
48
}
54
49
55
50
// Calculate the range to fetch
@@ -65,12 +60,16 @@ public function get( $limit = 50, $before = null ): array
65
60
}
66
61
} );
67
62
68
- // Filter, unserialize, and map to Batch objects
69
- $ batches = array_map ( function ( $ response ) {
70
- return $ response ? $ this ->toBatch ( json_decode ( $ response , true ) ) : null ;
71
- }, $ responses );
63
+ // Filter, decode, and sort raw batch data
64
+ $ batchesRaw = array_map ( fn ( $ response ) => json_decode ( $ response , true ), array_filter ( $ responses ) );
65
+ uasort ( $ batchesRaw , function ( $ a , $ b ) {
66
+ return $ b ['created_at ' ] <=> $ a ['created_at ' ];
67
+ } );
68
+
69
+ // Map sorted data to Batch objects and convert to a sequential array
70
+ $ batches = array_values ( array_map ( fn ( $ data ) => $ this ->toBatch ( $ data ), $ batchesRaw ) );
72
71
73
- return array_filter ( $ batches ) ;
72
+ return $ batches ;
74
73
}
75
74
76
75
public function find ( string $ batchId )
@@ -327,4 +326,4 @@ protected function pruneBatches( DateTimeInterface $before, $isFinished = null,
327
326
328
327
return $ totalDeleted ;
329
328
}
330
- }
329
+ }
0 commit comments