2
2
3
3
namespace MongoDB \Laravel \Bus ;
4
4
5
- use BadMethodCallException ;
6
5
use Carbon \CarbonImmutable ;
7
6
use Closure ;
8
7
use DateTimeInterface ;
12
11
use Illuminate \Bus \PendingBatch ;
13
12
use Illuminate \Bus \PrunableBatchRepository ;
14
13
use Illuminate \Bus \UpdatedBatchJobCounts ;
15
- use Illuminate \Database \Connection ;
16
14
use Illuminate \Support \Carbon ;
17
15
use MongoDB \BSON \ObjectId ;
18
16
use MongoDB \BSON \UTCDateTime ;
19
17
use MongoDB \Driver \ReadPreference ;
20
18
use MongoDB \Laravel \Collection ;
19
+ use MongoDB \Laravel \Connection ;
21
20
use Override ;
22
21
23
- use function assert ;
24
22
use function date_default_timezone_get ;
25
23
use function is_string ;
26
24
@@ -35,7 +33,6 @@ public function __construct(
35
33
Connection $ connection ,
36
34
string $ collection ,
37
35
) {
38
- assert ($ connection instanceof \MongoDB \Laravel \Connection);
39
36
$ this ->collection = $ connection ->getCollection ($ collection );
40
37
41
38
parent ::__construct ($ factory , $ connection , $ collection );
@@ -65,38 +62,43 @@ public function find(string $batchId): ?Batch
65
62
66
63
$ batch = $ this ->collection ->findOne (
67
64
['_id ' => $ batchId ],
68
- ['readPreference ' => ReadPreference::PRIMARY ],
65
+ [
66
+ 'readPreference ' => new ReadPreference (ReadPreference::PRIMARY ),
67
+ 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ],
68
+ ],
69
69
);
70
70
71
71
return $ this ->factory ->make (
72
72
$ this ,
73
- $ batch ['id ' ],
73
+ $ batch ['_id ' ],
74
74
$ batch ['name ' ],
75
75
$ batch ['total_jobs ' ],
76
76
$ batch ['pending_jobs ' ],
77
77
$ batch ['failed_jobs ' ],
78
78
$ batch ['failed_job_ids ' ],
79
79
$ batch ['options ' ],
80
- CarbonImmutable:: createFromTimestamp ($ batch ['created_at ' ]-> getTimestamp (), date_default_timezone_get () ),
81
- $ batch [ ' cancelled_at ' ] ? CarbonImmutable:: createFromTimestamp ($ batch ['cancelled_at ' ]-> getTimestamp (), date_default_timezone_get ()) : null ,
82
- $ batch [ ' finished_at ' ] ? CarbonImmutable:: createFromTimestamp ($ batch ['finished_at ' ]-> getTimestamp (), date_default_timezone_get ()) : null ,
80
+ $ this -> toCarbon ($ batch ['created_at ' ]),
81
+ $ this -> toCarbon ($ batch ['cancelled_at ' ]) ,
82
+ $ this -> toCarbon ($ batch ['finished_at ' ]) ,
83
83
);
84
84
}
85
85
86
86
#[Override]
87
87
public function store (PendingBatch $ batch ): Batch
88
88
{
89
- $ this ->collection ->insertOne ([
89
+ $ result = $ this ->collection ->insertOne ([
90
90
'name ' => $ batch ->name ,
91
91
'total_jobs ' => 0 ,
92
92
'pending_jobs ' => 0 ,
93
93
'failed_jobs ' => 0 ,
94
- 'failed_job_ids ' => ' [] ' ,
95
- 'options ' => $ this -> serialize ( $ batch ->options ) ,
94
+ 'failed_job_ids ' => [] ,
95
+ 'options ' => $ batch ->options ,
96
96
'created_at ' => new UTCDateTime (Carbon::now ()),
97
97
'cancelled_at ' => null ,
98
98
'finished_at ' => null ,
99
99
]);
100
+
101
+ return $ this ->find ($ result ->getInsertedId ());
100
102
}
101
103
102
104
#[Override]
@@ -195,19 +197,16 @@ public function delete(string $batchId): void
195
197
#[Override]
196
198
public function transaction (Closure $ callback ): mixed
197
199
{
198
- // Transactions are not necessary
199
- return $ callback ();
200
+ return $ this ->connection ->transaction (fn () => $ callback ());
200
201
}
201
202
202
203
/**
203
204
* Rollback the last database transaction for the connection.
204
- *
205
- * Not implemented.
206
205
*/
207
206
#[Override]
208
207
public function rollBack (): void
209
208
{
210
- throw new BadMethodCallException ( ' Not implemented ' );
209
+ $ this -> connection -> rollBack ( );
211
210
}
212
211
213
212
/** Mark the batch that has the given ID as finished. */
@@ -259,9 +258,19 @@ protected function toBatch($batch): Batch
259
258
$ batch ->failed_jobs ,
260
259
$ batch ->failed_job_ids ,
261
260
$ batch ->options ,
262
- CarbonImmutable:: createFromTimestamp ($ batch ->created_at -> getTimestamp (), date_default_timezone_get () ),
263
- $ batch -> cancelled_at ? CarbonImmutable:: createFromTimestamp ($ batch ->cancelled_at -> getTimestamp (), date_default_timezone_get ()) : null ,
264
- $ batch -> finished_at ? CarbonImmutable:: createFromTimestamp ($ batch ->finished_at -> getTimestamp (), date_default_timezone_get ()) : null ,
261
+ $ this -> toCarbon ($ batch ->created_at ),
262
+ $ this -> toCarbon ($ batch ->cancelled_at ) ,
263
+ $ this -> toCarbon ($ batch ->finished_at ) ,
265
264
);
266
265
}
266
+
267
+ /** @return ($date is null ? null : CarbonImmutable) */
268
+ private function toCarbon (?UTCDateTime $ date ): ?CarbonImmutable
269
+ {
270
+ if ($ date === null ) {
271
+ return null ;
272
+ }
273
+
274
+ return CarbonImmutable::createFromTimestamp ((string ) $ date , date_default_timezone_get ());
275
+ }
267
276
}
0 commit comments