Code improvement to our_address(), reducing arguments

This commit is contained in:
2024-04-21 21:40:55 +10:00
parent 1c270025cf
commit bba6f93fbc
9 changed files with 39 additions and 20 deletions

View File

@@ -3,7 +3,7 @@
use Carbon\Carbon;
use Illuminate\Support\Collection;
use App\Models\{Address,Domain,Setup,Zone};
use App\Models\{Address,Domain,Setup};
/**
* Calculate CCITT-CRC16 checksum
@@ -83,13 +83,14 @@ if (! function_exists('hexstr')) {
/**
* Return our addresses.
* If zone provided, limit the list to those within the zone
* If domain provided, limit the list to those within the domain - returning a Collection::class
* If address provided, return our address that would be used for the provided address - return Address::class
*
* @param Domain|NULL $do Limit the addresses for the specific domain
* @param Address|null $ao If address is presented, show the address we use when talking to that address
* @param Domain|Address|null $o - Domain or Address
* @return Collection|Address|NULL
* @throws Exception
*/
function our_address(Domain $do=NULL,Address $ao=NULL): Collection|Address|NULL
function our_address(Domain|Address $o=NULL): Collection|Address|NULL
{
static $so = NULL;
static $our = NULL;
@@ -102,15 +103,32 @@ function our_address(Domain $do=NULL,Address $ao=NULL): Collection|Address|NULL
$our = $so->system->akas;
}
$filter = $our;
if ($do)
$filter = $our->filter(function($item) use ($do) { return $item->zone->domain_id === $do->id; })->sortBy('role');
// If we dont have any addresses
if ($our->count() === 0)
return NULL;
// If we are looking for a specific address, and there is only 1 result, return it, otherwise return what we have
if ($ao && config('fido.strict') && ($x=$filter->filter(function($item) use ($ao) { return $item->role <= $ao->role; })->sortBy('role'))->count())
$filter = $x;
// We havent asked for an address/domain, so we'll return them all.
if (is_null($o))
return $our;
return $ao ? $filter->last() : ($do ? $filter : $our);
// We are requesting a list of addresses for a Domain, or a specific Address, and we have more than 1
switch (get_class($o)) {
case Address::class:
$filter = $our->filter(function($item) use ($o) { return $item->zone->domain_id === $o->zone->domain_id; })->sortBy('role');
// If we are looking for a specific address, and there is only 1 result, return it, otherwise return what we have
if (config('fido.strict') && ($x=$filter->filter(function($item) use ($o) { return $item->role <= $o->role; })->sortBy('role'))->count())
$filter = $x;
return $filter->last();
case Domain::class:
return $our->filter(function($item) use ($o) { return $item->zone->domain_id === $o->id; })->sortBy('role');
// We shouldnt get here
default:
throw new Exception('Unhandled class: '.get_class($o));
}
}
/**