Skip to content

Commit 767dc65

Browse files
committed
pass index config to model methods
1 parent ce29649 commit 767dc65

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

src/Jobs/PopulateBatchIndex.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Foundation\Bus\Dispatchable;
1212
use Illuminate\Queue\InteractsWithQueue;
1313
use Illuminate\Queue\SerializesModels;
14+
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
1415
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
1516

1617
class PopulateBatchIndex implements ShouldQueue
@@ -26,7 +27,7 @@ class PopulateBatchIndex implements ShouldQueue
2627
*
2728
* @return void
2829
*/
29-
public function __construct(protected Index $index, protected string $indexDocument, protected int $limit, protected int $offset)
30+
public function __construct(protected Index $index, protected IndexInterface $indexConfig, protected string $indexDocument, protected int $limit, protected int $offset)
3031
{
3132
//
3233
}
@@ -45,11 +46,11 @@ public function handle(): void
4546
/** @var ElasticsearchableInterface[] $records */
4647
$records = $this->indexDocument::offset($this->offset)->limit($this->limit)->get();
4748
foreach ($records as $record) {
48-
if (! $record->shouldIndex()) {
49+
if (! $record->shouldIndex($this->indexConfig)) {
4950
continue;
5051
}
5152

52-
$esDocuments[] = $record->toElasticaDocument();
53+
$esDocuments[] = $record->toElasticaDocument($this->indexConfig);
5354
}
5455

5556
if (count($esDocuments) === 0) {

src/Jobs/PopulateIndex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function handle(): void
4141
$modelCount = $indexDocument::count();
4242

4343
for ($batchNumber = 0; $batchNumber < ceil($modelCount / $this->indexConfig->getBatchSize()); $batchNumber++) {
44-
$jobs[] = new PopulateBatchIndex($index, $indexDocument, $this->indexConfig->getBatchSize(), $batchNumber * $this->indexConfig->getBatchSize());
44+
$jobs[] = new PopulateBatchIndex($index, $this->indexConfig, $indexDocument, $this->indexConfig->getBatchSize(), $batchNumber * $this->indexConfig->getBatchSize());
4545
}
4646
}
4747

src/Model/ElasticsearchableInterface.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Limenet\LaravelElasticaBridge\Model;
66

77
use Elastica\Document;
8+
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
89

910
interface ElasticsearchableInterface
1011
{
@@ -15,9 +16,9 @@ public function getModel(): string;
1516
*/
1617
public function getElasticsearchId(): string;
1718

18-
public function toElasticsearch(): array;
19+
public function toElasticsearch(IndexInterface $indexConfig): array;
1920

20-
public function shouldIndex(): bool;
21+
public function shouldIndex(IndexInterface $indexConfig): bool;
2122

22-
public function toElasticaDocument(): Document;
23+
public function toElasticaDocument(IndexInterface $indexConfig): Document;
2324
}

src/Model/ElasticsearchableTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final public function getElasticsearchId(): string
1919
return $this::class.'|'.$this->id;
2020
}
2121

22-
public function toElasticaDocument(): Document
22+
public function toElasticaDocument(IndexInterface $indexConfig): Document
2323
{
2424
return new Document(
2525
$this->getElasticsearchId(),
2626
array_merge(
27-
$this->toElasticsearch(),
27+
$this->toElasticsearch($indexConfig),
2828
[
2929
IndexInterface::DOCUMENT_MODEL_CLASS => $this::class,
3030
IndexInterface::DOCUMENT_MODEL_ID => $this->id,

0 commit comments

Comments
 (0)