44
55namespace Limenet \LaravelElasticaBridge \Jobs ;
66
7- use Elastica \Index ;
87use Illuminate \Bus \Batchable ;
98use Illuminate \Bus \Queueable ;
109use Illuminate \Contracts \Queue \ShouldQueue ;
1312use Illuminate \Queue \SerializesModels ;
1413use Limenet \LaravelElasticaBridge \Index \IndexInterface ;
1514use Limenet \LaravelElasticaBridge \Model \ElasticsearchableInterface ;
15+ use Limenet \LaravelElasticaBridge \Repository \IndexRepository ;
1616use Throwable ;
1717
1818class PopulateBatchIndex implements ShouldQueue
@@ -23,16 +23,19 @@ class PopulateBatchIndex implements ShouldQueue
2323 use Queueable;
2424 use SerializesModels;
2525
26+ /**
27+ * @param class-string<IndexInterface> $indexConfigKey
28+ */
2629 public function __construct (
27- private readonly Index $ index ,
28- private readonly IndexInterface $ indexConfig ,
30+ protected string $ indexConfigKey ,
2931 private readonly string $ indexDocument ,
3032 private readonly int $ limit ,
3133 private readonly int $ offset
3234 ) {}
3335
34- public function handle (): void
36+ public function handle (IndexRepository $ indexRepository ): void
3537 {
38+ $ indexConfig = $ indexRepository ->get ($ this ->indexConfigKey );
3639 if ($ this ->batch ()?->cancelled() === true ) {
3740 return ;
3841 }
@@ -42,21 +45,21 @@ public function handle(): void
4245 /** @var ElasticsearchableInterface[] $records */
4346 $ records = $ this ->indexDocument ::offset ($ this ->offset )->limit ($ this ->limit )->get ();
4447 foreach ($ records as $ record ) {
45- if (! $ record ->shouldIndex ($ this -> indexConfig )) {
48+ if (! $ record ->shouldIndex ($ indexConfig )) {
4649 continue ;
4750 }
4851
49- $ esDocuments [] = $ record ->toElasticaDocument ($ this -> indexConfig );
52+ $ esDocuments [] = $ record ->toElasticaDocument ($ indexConfig );
5053 }
5154
5255 if ($ esDocuments === []) {
5356 return ;
5457 }
5558
5659 try {
57- $ this -> index ->addDocuments ($ esDocuments );
60+ $ indexConfig -> getBlueGreenInactiveElasticaIndex () ->addDocuments ($ esDocuments );
5861 } catch (Throwable $ throwable ) {
59- if (! $ this -> indexConfig ->ingoreIndexingErrors ()) {
62+ if (! $ indexConfig ->ingoreIndexingErrors ()) {
6063 throw $ throwable ;
6164 }
6265 }
0 commit comments