22
33namespace MongoDB \Laravel \Bus ;
44
5- use BadMethodCallException ;
65use Carbon \CarbonImmutable ;
76use Closure ;
87use DateTimeInterface ;
1211use Illuminate \Bus \PendingBatch ;
1312use Illuminate \Bus \PrunableBatchRepository ;
1413use Illuminate \Bus \UpdatedBatchJobCounts ;
15- use Illuminate \Database \Connection ;
1614use Illuminate \Support \Carbon ;
1715use MongoDB \BSON \ObjectId ;
1816use MongoDB \BSON \UTCDateTime ;
1917use MongoDB \Driver \ReadPreference ;
2018use MongoDB \Laravel \Collection ;
19+ use MongoDB \Laravel \Connection ;
2120use Override ;
2221
23- use function assert ;
2422use function date_default_timezone_get ;
2523use function is_string ;
2624
@@ -35,7 +33,6 @@ public function __construct(
3533 Connection $ connection ,
3634 string $ collection ,
3735 ) {
38- assert ($ connection instanceof \MongoDB \Laravel \Connection);
3936 $ this ->collection = $ connection ->getCollection ($ collection );
4037
4138 parent ::__construct ($ factory , $ connection , $ collection );
@@ -65,38 +62,43 @@ public function find(string $batchId): ?Batch
6562
6663 $ batch = $ this ->collection ->findOne (
6764 ['_id ' => $ batchId ],
68- ['readPreference ' => ReadPreference::PRIMARY ],
65+ [
66+ 'readPreference ' => new ReadPreference (ReadPreference::PRIMARY ),
67+ 'typeMap ' => ['root ' => 'array ' , 'array ' => 'array ' , 'document ' => 'array ' ],
68+ ],
6969 );
7070
7171 return $ this ->factory ->make (
7272 $ this ,
73- $ batch ['id ' ],
73+ $ batch ['_id ' ],
7474 $ batch ['name ' ],
7575 $ batch ['total_jobs ' ],
7676 $ batch ['pending_jobs ' ],
7777 $ batch ['failed_jobs ' ],
7878 $ batch ['failed_job_ids ' ],
7979 $ 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 ' ]) ,
8383 );
8484 }
8585
8686 #[Override]
8787 public function store (PendingBatch $ batch ): Batch
8888 {
89- $ this ->collection ->insertOne ([
89+ $ result = $ this ->collection ->insertOne ([
9090 'name ' => $ batch ->name ,
9191 'total_jobs ' => 0 ,
9292 'pending_jobs ' => 0 ,
9393 'failed_jobs ' => 0 ,
94- 'failed_job_ids ' => ' [] ' ,
95- 'options ' => $ this -> serialize ( $ batch ->options ) ,
94+ 'failed_job_ids ' => [] ,
95+ 'options ' => $ batch ->options ,
9696 'created_at ' => new UTCDateTime (Carbon::now ()),
9797 'cancelled_at ' => null ,
9898 'finished_at ' => null ,
9999 ]);
100+
101+ return $ this ->find ($ result ->getInsertedId ());
100102 }
101103
102104 #[Override]
@@ -195,19 +197,16 @@ public function delete(string $batchId): void
195197 #[Override]
196198 public function transaction (Closure $ callback ): mixed
197199 {
198- // Transactions are not necessary
199- return $ callback ();
200+ return $ this ->connection ->transaction (fn () => $ callback ());
200201 }
201202
202203 /**
203204 * Rollback the last database transaction for the connection.
204- *
205- * Not implemented.
206205 */
207206 #[Override]
208207 public function rollBack (): void
209208 {
210- throw new BadMethodCallException ( ' Not implemented ' );
209+ $ this -> connection -> rollBack ( );
211210 }
212211
213212 /** Mark the batch that has the given ID as finished. */
@@ -259,9 +258,19 @@ protected function toBatch($batch): Batch
259258 $ batch ->failed_jobs ,
260259 $ batch ->failed_job_ids ,
261260 $ 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 ) ,
265264 );
266265 }
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+ }
267276}
0 commit comments