Skip to content

Commit 5a2eb39

Browse files
committed
add a get method to batch repo
1 parent 737fe98 commit 5a2eb39

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

src/Illuminate/Bus/BatchRepository.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
interface BatchRepository
88
{
9+
/**
10+
* Get available batches.
11+
*
12+
* @param mixed $before
13+
* @param int $limit
14+
* @return [\Illuminate\Bus\Batch]
15+
*/
16+
public function get($before, $limit);
17+
918
/**
1019
* Store a new pending batch.
1120
*

src/Illuminate/Bus/DatabaseBatchRepository.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ public function __construct(BatchFactory $factory, Connection $connection, strin
4545
$this->table = $table;
4646
}
4747

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+
4870
/**
4971
* Retrieve information about an existing batch.
5072
*
@@ -57,22 +79,9 @@ public function find(string $batchId)
5779
->where('id', $batchId)
5880
->first();
5981

60-
if (! $batch) {
61-
return;
82+
if ($batch) {
83+
return $this->toBatch($batch);
6284
}
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-
);
7685
}
7786

7887
/**
@@ -232,4 +241,26 @@ public function transaction(Closure $callback)
232241
return $callback();
233242
});
234243
}
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+
}
235266
}

src/Illuminate/Support/Testing/Fakes/BatchRepositoryFake.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212

1313
class BatchRepositoryFake implements BatchRepository
1414
{
15+
/**
16+
* Get available batches.
17+
*
18+
* @param mixed $before
19+
* @param int $limit
20+
* @return [\Illuminate\Bus\Batch]
21+
*/
22+
public function get($before, $limit)
23+
{
24+
}
25+
1526
/**
1627
* Retrieve information about an existing batch.
1728
*

0 commit comments

Comments
 (0)