@@ -45,6 +45,28 @@ public function __construct(BatchFactory $factory, Connection $connection, strin
45
45
$ this ->table = $ table ;
46
46
}
47
47
48
+ /**
49
+ * Get available batches.
50
+ *
51
+ * @param mixed $before
52
+ * @param int $limit
53
+ * @return [\Illuminate\Bus\Batch]
54
+ */
55
+ public function get ($ before = null , $ limit = 50 )
56
+ {
57
+ return $ this ->connection ->table ($ this ->table )
58
+ ->orderByDesc ('id ' )
59
+ ->take ($ limit )
60
+ ->when ($ before , function ($ q ) use ($ before ) {
61
+ return $ q ->where ('id ' , '< ' , $ before );
62
+ })
63
+ ->get ()
64
+ ->map (function ($ batch ) {
65
+ return $ this ->toBatch ($ batch );
66
+ })
67
+ ->all ();
68
+ }
69
+
48
70
/**
49
71
* Retrieve information about an existing batch.
50
72
*
@@ -57,22 +79,9 @@ public function find(string $batchId)
57
79
->where ('id ' , $ batchId )
58
80
->first ();
59
81
60
- if (! $ batch ) {
61
- return ;
82
+ if ($ batch ) {
83
+ return $ this -> toBatch ( $ batch ) ;
62
84
}
63
-
64
- return $ this ->factory ->make (
65
- $ this ,
66
- $ batch ->id ,
67
- (int ) $ batch ->total_jobs ,
68
- (int ) $ batch ->pending_jobs ,
69
- (int ) $ batch ->failed_jobs ,
70
- json_decode ($ batch ->failed_job_ids , true ),
71
- unserialize ($ batch ->options ),
72
- CarbonImmutable::createFromTimestamp ($ batch ->created_at ),
73
- $ batch ->cancelled_at ? CarbonImmutable::createFromTimestamp ($ batch ->cancelled_at ) : $ batch ->cancelled_at ,
74
- $ batch ->finished_at ? CarbonImmutable::createFromTimestamp ($ batch ->finished_at ) : $ batch ->finished_at
75
- );
76
85
}
77
86
78
87
/**
@@ -232,4 +241,26 @@ public function transaction(Closure $callback)
232
241
return $ callback ();
233
242
});
234
243
}
244
+
245
+ /**
246
+ * Convert the given raw batch to an object.
247
+ *
248
+ * @param object $batch
249
+ * @@return \Illuminate\Bus\Batch
250
+ */
251
+ protected function toBatch ($ batch )
252
+ {
253
+ return $ this ->factory ->make (
254
+ $ this ,
255
+ $ batch ->id ,
256
+ (int ) $ batch ->total_jobs ,
257
+ (int ) $ batch ->pending_jobs ,
258
+ (int ) $ batch ->failed_jobs ,
259
+ json_decode ($ batch ->failed_job_ids , true ),
260
+ unserialize ($ batch ->options ),
261
+ CarbonImmutable::createFromTimestamp ($ batch ->created_at ),
262
+ $ batch ->cancelled_at ? CarbonImmutable::createFromTimestamp ($ batch ->cancelled_at ) : $ batch ->cancelled_at ,
263
+ $ batch ->finished_at ? CarbonImmutable::createFromTimestamp ($ batch ->finished_at ) : $ batch ->finished_at
264
+ );
265
+ }
235
266
}
0 commit comments