From 743de4dddbe57fe92bddab474b57af9d4c074644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 11 Jan 2025 11:47:57 +0100 Subject: [PATCH] phpstan fixes, clarify previousTenant use --- .../PersistentQueueTenancyBootstrapper.php | 23 +++++++++++-------- .../QueueTenancyBootstrapper.php | 6 +++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Bootstrappers/PersistentQueueTenancyBootstrapper.php b/src/Bootstrappers/PersistentQueueTenancyBootstrapper.php index 3258769a..90bb3d08 100644 --- a/src/Bootstrappers/PersistentQueueTenancyBootstrapper.php +++ b/src/Bootstrappers/PersistentQueueTenancyBootstrapper.php @@ -29,7 +29,7 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper * However, we're registering a hook to initialize tenancy. Therefore, * we need to register the hook at service provider execution time. */ - public static function __constructStatic(Application $app) + public static function __constructStatic(Application $app): void { static::setUpJobListener($app->make(Dispatcher::class), $app->runningUnitTests()); } @@ -42,7 +42,7 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper $this->setUpPayloadGenerator(); } - protected static function setUpJobListener($dispatcher, $runningTests) + protected static function setUpJobListener(Dispatcher $dispatcher, bool $runningTests): void { $previousTenant = null; @@ -61,7 +61,9 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper // If we're running tests, we make sure to clean up after any artisan('queue:work') calls $revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) { if ($runningTests) { - static::revertToPreviousState($event, $previousTenant); + static::revertToPreviousState($event->job->payload()['tenant_id'] ?? null, $previousTenant); + + // We don't need to reset $previousTenant since the value will be set again when a job is processed. } // If we're not running tests, we remain in the tenant's context. This makes other JobProcessed @@ -73,7 +75,7 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper $dispatcher->listen(JobFailed::class, $revertToPreviousState); // artisan('queue:work') which fails } - protected static function initializeTenancyForQueue($tenantId) + protected static function initializeTenancyForQueue(string|int|null $tenantId): void { if (! $tenantId) { // The job is not tenant-aware @@ -89,13 +91,14 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper // so that we don't work with an outdated tenant() instance in case it // was updated outside the queue worker. tenancy()->end(); - tenancy()->initialize(tenancy()->find($tenantId)); + + /** @var Tenant $tenant */ + $tenant = tenancy()->find($tenantId); + tenancy()->initialize($tenant); } - protected static function revertToPreviousState($event, &$previousTenant) + protected static function revertToPreviousState(string|int|null $tenantId, ?Tenant $previousTenant): void { - $tenantId = $event->job->payload()['tenant_id'] ?? null; - // The job was not tenant-aware if (! $tenantId) { return; @@ -112,7 +115,7 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper } } - protected function setUpPayloadGenerator() + protected function setUpPayloadGenerator(): void { $bootstrapper = &$this; @@ -123,7 +126,7 @@ class PersistentQueueTenancyBootstrapper implements TenancyBootstrapper } } - public function getPayload(string $connection) + public function getPayload(string $connection): array { if (! tenancy()->initialized) { return []; diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 06fdb6a7..2ed6b7f1 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -59,9 +59,11 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper }); $revertToPreviousState = function ($event) use (&$previousTenant) { + // In queue worker context, this reverts to the central context. + // In dispatchSync context, this reverts to the previous tenant's context. + // There's no need to reset $previousTenant here since it's always first + // set in the above listeners and the app is reverted back to that context. static::revertToPreviousState($event->job->payload()['tenant_id'] ?? null, $previousTenant); - - $previousTenant = null; }; $dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds