From c8a42c32d007a0930c65ac6e6eee005624617cbf Mon Sep 17 00:00:00 2001 From: Leandro Guindani Gehlen Date: Tue, 21 May 2024 09:10:17 -0300 Subject: [PATCH] Refactor --- src/InteractsWithCompany.php | 11 ++++++----- src/Middleware/InitializeCompanyByRequestData.php | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/InteractsWithCompany.php b/src/InteractsWithCompany.php index 76f5e08..f6be6ca 100644 --- a/src/InteractsWithCompany.php +++ b/src/InteractsWithCompany.php @@ -42,14 +42,15 @@ public function company(): BelongsTo */ public function applyCompany() { - $company = company()->current(); $foreignKeyName = $this->company()->getForeignKeyName(); - if (! $company) { - throw new CompanyCouldNotBeIdentifiedException(); - } - if ($this->{$foreignKeyName} === null) { + $company = company()->current(); + + if (! $company) { + throw new CompanyCouldNotBeIdentifiedException(); + } + $this->{$foreignKeyName} = $company->id; } } diff --git a/src/Middleware/InitializeCompanyByRequestData.php b/src/Middleware/InitializeCompanyByRequestData.php index bb1efe0..4282637 100644 --- a/src/Middleware/InitializeCompanyByRequestData.php +++ b/src/Middleware/InitializeCompanyByRequestData.php @@ -51,13 +51,13 @@ public function handle($request, Closure $next) protected function getPayload(Request $request): ?string { - $tenant = null; + $company = null; if (static::$header && $request->hasHeader(static::$header)) { - $tenant = $request->header(static::$header); + $company = $request->header(static::$header); } elseif (static::$queryParameter && $request->has(static::$queryParameter)) { - $tenant = $request->get(static::$queryParameter); + $company = $request->get(static::$queryParameter); } - return $tenant; + return $company; } }