Skip to content

Commit b666edd

Browse files
committed
use proper jobs for events
1 parent e897e20 commit b666edd

File tree

8 files changed

+70
-33
lines changed

8 files changed

+70
-33
lines changed

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
},
2424
"require-dev": {
2525
"driftingly/rector-laravel": "^2.0.2",
26-
"larastan/larastan": "^3.1",
27-
"laravel/pint": "^1.21.1",
28-
"nunomaduro/collision": "^8.6.1",
26+
"larastan/larastan": "^3.2",
27+
"laravel/pint": "^1.21.2",
28+
"nunomaduro/collision": "^8.7.0",
2929
"orchestra/testbench": "^9.11 || ^10.1",
3030
"phpstan/extension-installer": "^1.4.3",
31-
"phpstan/phpstan": "^2.1.8",
31+
"phpstan/phpstan": "^2.1.11",
3232
"phpstan/phpstan-deprecation-rules": "^2.0.1",
33-
"phpstan/phpstan-phpunit": "^2.0.4",
34-
"phpstan/phpstan-strict-rules": "^2.0.3",
35-
"phpunit/phpunit": "^11.5.12",
33+
"phpstan/phpstan-phpunit": "^2.0.6",
34+
"phpstan/phpstan-strict-rules": "^2.0.4",
35+
"phpunit/phpunit": "^11.5.15",
3636
"psr/log": "^3.0.2",
3737
"rector/rector": "^2.0.10",
3838
"sentry/sentry": "^4.10.0",

phpstan-baseline.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ parameters:
1212
count: 1
1313
path: src/Commands/IndexCommand.php
1414

15+
-
16+
message: '#^Parameter \#1 \$callback of method Illuminate\\Support\\Collection\<\(int\|string\),Limenet\\LaravelElasticaBridge\\Model\\ElasticsearchableInterface\>\:\:each\(\) expects callable\(Limenet\\LaravelElasticaBridge\\Model\\ElasticsearchableInterface, int\|string\)\: mixed, Closure\(Illuminate\\Database\\Eloquent\\Model\)\: Illuminate\\Foundation\\Bus\\PendingDispatch given\.$#'
17+
identifier: argument.type
18+
count: 1
19+
path: src/Events/EventHandler.php
20+
1521
-
1622
message: '#^Unable to resolve the template type TKey in call to function collect$#'
1723
identifier: argument.templateType

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parameters:
1111
tmpDir: build/phpstan
1212
checkOctaneCompatibility: true
1313
checkModelProperties: true
14+
treatPhpDocTypesAsCertain: false
1415

1516
ignoreErrors:
1617
-

src/Client/ElasticaClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ public function __construct()
2323
}
2424

2525
$client = new Client(
26-
[
27-
'host' => config('elastica-bridge.elasticsearch.host'),
28-
'port' => config('elastica-bridge.elasticsearch.port'),
29-
], logger: $logger);
26+
sprintf(
27+
'http://%s:%d',
28+
config('elastica-bridge.elasticsearch.host'),
29+
config('elastica-bridge.elasticsearch.port')
30+
),
31+
logger: $logger
32+
);
3033

3134
$this->client = $client;
3235
}

src/Commands/StatusCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function handle(): int
2929
$table
3030
->setHeaders(['Host', 'Port', 'Version'])
3131
->setRows([[
32-
$this->elastica->getClient()->getConfig('host'),
33-
$this->elastica->getClient()->getConfig('port'),
32+
$this->elastica->getClient()->getTransport()->getNodePool()->nextNode()->getUri()->getHost(),
33+
$this->elastica->getClient()->getTransport()->getNodePool()->nextNode()->getUri()->getPort(),
3434
$this->elastica->getClient()->getVersion(),
3535
]])
3636
->setHeaderTitle('Cluster');

src/Events/EventHandler.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,32 @@
55
use Illuminate\Contracts\Events\Dispatcher;
66
use Illuminate\Database\Eloquent\Model;
77
use Limenet\LaravelElasticaBridge\Client\ElasticaClient;
8-
use Limenet\LaravelElasticaBridge\Services\ModelEventListener;
8+
use Limenet\LaravelElasticaBridge\Jobs\ModelEvent;
9+
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
910

1011
class EventHandler
1112
{
12-
public function __construct(
13-
private readonly ModelEventListener $modelEventListener
14-
) {}
15-
1613
public function subscribe(Dispatcher $events): void
1714
{
1815
$events->listen(
19-
collect(ModelEventListener::EVENTS)
16+
collect(ModelEvent::EVENTS)
2017
->map(fn (string $name): string => sprintf('eloquent.%s:*', $name))
2118
->toArray(),
2219
function ($event, $models): void {
2320
if (! ElasticaClient::listensToEvents()) {
2421
return;
2522
}
2623

27-
dispatch(function () use ($event, $models): void {
28-
$name = (str($event)->before(':')->after('.'));
29-
if (! ElasticaClient::listensToEvents()) {
30-
return;
31-
}
24+
$name = str($event)->before(':')->after('.')->toString();
3225

33-
collect($models)->each(fn (Model $model) => $this->modelEventListener->handle($name, $model));
34-
})->onConnection(config('elastica-bridge.connection'));
26+
collect($models)
27+
->filter(fn (Model $model): bool => $model instanceof ElasticsearchableInterface)
28+
->each(fn (Model $model) => ModelEvent::dispatch(
29+
$name,
30+
$model::class,
31+
$model->getKeyName(),
32+
$model->getKey()
33+
));
3534
});
3635
}
3736
}

src/Services/ModelEventListener.php renamed to src/Jobs/ModelEvent.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@
22

33
declare(strict_types=1);
44

5-
namespace Limenet\LaravelElasticaBridge\Services;
5+
namespace Limenet\LaravelElasticaBridge\Jobs;
66

77
use Elastic\Elasticsearch\Exception\ClientResponseException;
8+
use Illuminate\Bus\Batchable;
9+
use Illuminate\Bus\Queueable;
10+
use Illuminate\Contracts\Queue\ShouldQueue;
811
use Illuminate\Database\Eloquent\Model;
12+
use Illuminate\Foundation\Bus\Dispatchable;
13+
use Illuminate\Queue\InteractsWithQueue;
14+
use Illuminate\Queue\SerializesModels;
915
use Limenet\LaravelElasticaBridge\Index\IndexInterface;
1016
use Limenet\LaravelElasticaBridge\Model\ElasticsearchableInterface;
1117
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
1218

13-
class ModelEventListener
19+
class ModelEvent implements ShouldQueue
1420
{
21+
use Batchable;
22+
use Dispatchable;
23+
use InteractsWithQueue;
24+
use Queueable;
25+
use SerializesModels;
26+
1527
private const EVENT_CREATED = 'created';
1628

1729
private const EVENT_UPDATED = 'updated';
@@ -30,9 +42,27 @@ class ModelEventListener
3042
self::EVENT_DELETED,
3143
];
3244

33-
public function handle(string $event, Model $model): void
45+
/**
46+
* @template TModel of ElasticsearchableInterface&Model
47+
*
48+
* @param class-string<TModel> $modelClass
49+
* @param model-property<TModel> $keyName
50+
* @return void
51+
*/
52+
public function __construct(
53+
private readonly string $event,
54+
private readonly string $modelClass,
55+
private readonly string $keyName,
56+
private readonly mixed $keyValue
57+
) {
58+
$this->onConnection(config('elastica-bridge.connection'));
59+
}
60+
61+
public function handle(): void
3462
{
35-
if (! $model instanceof ElasticsearchableInterface) {
63+
$model = $this->modelClass::query()->where($this->keyName, $this->keyValue)->first();
64+
65+
if (! $model instanceof ElasticsearchableInterface || ! $model instanceof Model) {
3666
return;
3767
}
3868

@@ -43,7 +73,7 @@ public function handle(string $event, Model $model): void
4373

4474
$shouldBePresent = true;
4575

46-
if (! $model->shouldIndex($index) || $event === self::EVENT_DELETED) {
76+
if (! $model->shouldIndex($index) || $this->event === self::EVENT_DELETED) {
4777
$shouldBePresent = false;
4878
}
4979

src/LaravelElasticaBridgeServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Limenet\LaravelElasticaBridge\Commands\StatusCommand;
1111
use Limenet\LaravelElasticaBridge\Events\EventHandler;
1212
use Limenet\LaravelElasticaBridge\Repository\IndexRepository;
13-
use Limenet\LaravelElasticaBridge\Services\ModelEventListener;
1413
use Spatie\LaravelPackageTools\Package;
1514
use Spatie\LaravelPackageTools\PackageServiceProvider;
1615

@@ -32,7 +31,6 @@ public function configurePackage(Package $package): void
3231
public function packageRegistered(): void
3332
{
3433
$this->app->singleton(ElasticaClient::class);
35-
$this->app->bind(ModelEventListener::class);
3634
$this->app->tag(config('elastica-bridge.indices'), 'elasticaBridgeIndices');
3735

3836
$this->app->when(IndexRepository::class)

0 commit comments

Comments
 (0)