Skip to content

Commit

Permalink
default resolver to request
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed May 10, 2022
1 parent 2136a51 commit ab85feb
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/TenancyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,46 @@ class TenancyResolver
{
/**
* Method for resolving tenant
* @return
*
* @param boolean $throwError throw error if tenant not found
* @return string
*/
public static function resolve()
public static function resolve($throwError = false)
{
// @codeCoverageIgnoreStart
$resolver = config('laratt.resolver', '');

if (!empty($resolver)
&& (strpos($resolver, 'niiknow') === false)
&& is_callable($resolver)) {
$tenant = call_user_func($resolver);
} else {
$req = request();

// attempt resolve by request
$req = request();
$tenant = '';
if (isset($req)) {
$tenant = $req->header('x-tenant') ?? $req->query('x-tenant');
}

if (!isset($tenant)) {
$tenant = '';
// attempt resolve by resolver
if (!isset($tenant) || empty($tenant)) {
$resolver = config('laratt.resolver', '');

if (!empty($resolver)
&& (strpos($resolver, 'niiknow') === false)
&& is_callable($resolver)) {
$tenant = call_user_func($resolver);
}
}

// throw error if not found
if (!isset($tenant) || empty($tenant)) {
if ($throwError) {
throw new HttpException(403, 'x-tenant is required.', null, null);
}
}

// @codeCoverageIgnoreEnd

return self::slug($tenant);

// @codeCoverageIgnoreEnd

return self::slug($tenant);
}

/**
Expand Down

0 comments on commit ab85feb

Please sign in to comment.