From 670450387c2f6a1d603adf7b52287b7e34f565ee Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 23 Oct 2024 12:06:53 +1100 Subject: [PATCH] Improvements to handle rogue 2D adresses --- app/Models/Address.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/Models/Address.php b/app/Models/Address.php index 9876dd0..46c502c 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -254,19 +254,26 @@ class Address extends Model $ftn = self::parseFTN($address); $do = $ftn['d'] ? Domain::where('name',$ftn['d'])->single() : NULL; - if ($ftn['z'] === 0) - Log::alert(sprintf('%s:! newFTN was parsed an FTN [%s] with a zero zone',self::LOGKEY,$address)); + $o = new self; + $o->region_id = $ftn['r']; + $o->host_id = $ftn['n']; + $o->node_id = $ftn['f']; + $o->point_id = $ftn['p']; $zo = Zone::where('zone_id',$ftn['z']) ->when($do,fn($query)=>$query->where('domain_id',$do->id)) ->single(); - $o = new self; $o->zone_id = $zo?->id; - $o->region_id = $ftn['r']; - $o->host_id = $ftn['n']; - $o->node_id = $ftn['f']; - $o->point_id = $ftn['p']; + + if (($ftn['z'] === 0) || (! $zo)) { + Log::alert(sprintf('%s:! newFTN was parsed an FTN [%s] with a zero zone, adding empty zone in domain',self::LOGKEY,$address)); + + $zo = new Zone; + $zo->domain_id = $do?->id; + } + + $o->zone()->associate($zo); return $o; } @@ -761,6 +768,9 @@ class Address extends Model if (! $this->relationLoaded('zone')) $this->load(['zone:id,domain_id,zone_id']); + if (! $this->zone) + throw new InvalidFTNException(sprintf('Invalid Zone for FTN address [%d/%d.%d@%s]',$this->host_id ?: $this->region_id,$this->node_id,$this->point_id,$this->domain?->name)); + return sprintf('%d:%s',$this->zone->zone_id,$this->getFTN2DAttribute()); }