From 7352a74a12d1148a844582c520912a8df28c5098 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 8 Nov 2024 23:31:21 +1100 Subject: [PATCH] Deprecate singleOrFail() in favour of sole() --- app/Classes/FTN/Packet.php | 2 +- app/Classes/Protocol/DNS.php | 4 ++-- app/Console/Commands/AddressIdle.php | 2 +- app/Console/Commands/CommBinkpSend.php | 4 +--- app/Console/Commands/CommEMSISend.php | 4 +--- app/Console/Commands/Debug/ZoneCheck.php | 2 +- app/Console/Commands/EchoareaImport.php | 3 ++- app/Console/Commands/FileareaImport.php | 3 ++- app/Console/Commands/Filefix/Rescan.php | 2 +- app/Console/Commands/NodesNew.php | 2 +- app/Console/Commands/UserCodeSend.php | 2 +- app/Http/Controllers/ZoneController.php | 2 +- app/Jobs/NodelistImport.php | 4 ++-- app/Jobs/TicProcess.php | 2 +- app/Providers/AppServiceProvider.php | 6 +++--- app/Traits/{SingleOrFail.php => Single.php} | 20 +++----------------- database/seeders/TestNodeHierarchy.php | 4 ++-- tests/Feature/RoutingTest.php | 2 +- 18 files changed, 27 insertions(+), 43 deletions(-) rename app/Traits/{SingleOrFail.php => Single.php} (51%) diff --git a/app/Classes/FTN/Packet.php b/app/Classes/FTN/Packet.php index 5a8c910..8e24d25 100644 --- a/app/Classes/FTN/Packet.php +++ b/app/Classes/FTN/Packet.php @@ -160,7 +160,7 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable try { $o->zone = Zone::where('zone_id',$o->fz) ->where('default',TRUE) - ->singleOrFail(); + ->sole(); } catch (ModelNotFoundException $e) { throw new InvalidPacketException(sprintf('%s:! We couldnt work out the packet zone, and there isnt a default for[%d]',self::LOGKEY,$o->fz)); diff --git a/app/Classes/Protocol/DNS.php b/app/Classes/Protocol/DNS.php index 6f5f215..87d7786 100644 --- a/app/Classes/Protocol/DNS.php +++ b/app/Classes/Protocol/DNS.php @@ -179,11 +179,11 @@ final class DNS extends BaseProtocol switch ($labels->first()) { case '_binkp': - $mailer = Mailer::where('name','BINKP')->singleOrFail(); + $mailer = Mailer::where('name','BINKP')->sole(); break; case '_ifcico': - $mailer = Mailer::where('name','EMSI')->singleOrFail(); + $mailer = Mailer::where('name','EMSI')->sole(); break; default: diff --git a/app/Console/Commands/AddressIdle.php b/app/Console/Commands/AddressIdle.php index bed54e7..150ec76 100644 --- a/app/Console/Commands/AddressIdle.php +++ b/app/Console/Commands/AddressIdle.php @@ -30,7 +30,7 @@ class AddressIdle extends Command */ public function handle(): int { - $do = Domain::where('name',$this->argument('domain'))->singleOrFail(); + $do = Domain::where('name',$this->argument('domain'))->sole(); return Job::dispatchSync($do,$this->option('ftn') ? Address::findFTN($this->option('ftn')) : NULL); } diff --git a/app/Console/Commands/CommBinkpSend.php b/app/Console/Commands/CommBinkpSend.php index bb06df5..39ae9e4 100644 --- a/app/Console/Commands/CommBinkpSend.php +++ b/app/Console/Commands/CommBinkpSend.php @@ -27,8 +27,6 @@ class CommBinkpSend extends Command */ protected $description = 'BINKP send'; - private const ID = 'BINKP'; - /** * Execute the console command. * @@ -42,7 +40,7 @@ class CommBinkpSend extends Command Log::info(sprintf('CBS:- Call BINKP send for %s',$ao->ftn)); - $mo = Mailer::where('name',self::ID)->singleOrFail(); + $mo = Mailer::where('name','BINKP')->sole(); if ($this->option('now')) Job::dispatchSync($ao,$mo); diff --git a/app/Console/Commands/CommEMSISend.php b/app/Console/Commands/CommEMSISend.php index e12ffc5..975cd39 100644 --- a/app/Console/Commands/CommEMSISend.php +++ b/app/Console/Commands/CommEMSISend.php @@ -27,8 +27,6 @@ class CommEMSISend extends Command */ protected $description = 'EMSI send'; - private const ID = 'EMSI'; - /** * Execute the console command. * @@ -42,7 +40,7 @@ class CommEMSISend extends Command Log::info(sprintf('CES:- Call EMSI send for %s',$ao->ftn)); - $mo = Mailer::where('name',self::ID)->singleOrFail(); + $mo = Mailer::where('name','EMSI')->sole(); if ($this->option('now')) Job::dispatchSync($ao,$mo); diff --git a/app/Console/Commands/Debug/ZoneCheck.php b/app/Console/Commands/Debug/ZoneCheck.php index 3a68122..cd8cdf6 100644 --- a/app/Console/Commands/Debug/ZoneCheck.php +++ b/app/Console/Commands/Debug/ZoneCheck.php @@ -16,7 +16,7 @@ class ZoneCheck extends Command public function handle(): int { - $do = Domain::where('name',$this->argument('domain'))->singleOrFail(); + $do = Domain::where('name',$this->argument('domain'))->sole(); foreach ($do->zones->sortby('zone_id') as $zo) { if ($this->option('zone') && ($this->option('zone') != $zo->zone_id)) diff --git a/app/Console/Commands/EchoareaImport.php b/app/Console/Commands/EchoareaImport.php index 01ba12e..2d05489 100644 --- a/app/Console/Commands/EchoareaImport.php +++ b/app/Console/Commands/EchoareaImport.php @@ -34,7 +34,8 @@ class EchoareaImport extends Command */ public function handle(): int { - $do = Domain::where('name',strtolower($this->argument('domain')))->singleOrFail(); + $do = Domain::where('name',strtolower($this->argument('domain')))->single(); + return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix') ?: '',$this->option('unlink')); } } \ No newline at end of file diff --git a/app/Console/Commands/FileareaImport.php b/app/Console/Commands/FileareaImport.php index 8efd8df..3db6a4c 100644 --- a/app/Console/Commands/FileareaImport.php +++ b/app/Console/Commands/FileareaImport.php @@ -34,7 +34,8 @@ class FileareaImport extends Command */ public function handle(): int { - $do = Domain::where('name',strtolower($this->argument('domain')))->singleOrFail(); + $do = Domain::where('name',strtolower($this->argument('domain')))->sole(); + return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix') ?: '',$this->option('unlink')); } } \ No newline at end of file diff --git a/app/Console/Commands/Filefix/Rescan.php b/app/Console/Commands/Filefix/Rescan.php index 5121ad0..afe0180 100644 --- a/app/Console/Commands/Filefix/Rescan.php +++ b/app/Console/Commands/Filefix/Rescan.php @@ -40,7 +40,7 @@ class Rescan extends Command if (! $this->argument('area')) throw new \Exception('Areaname is required'); - $fao = Filearea::where('name',$this->argument('area'))->singleOrFail(); + $fao = Filearea::where('name',$this->argument('area'))->sole(); if ($fao->domain_id !== $ao->zone->domain_id) throw new \Exception(sprintf('File area [%s] is not in domain [%s] for FTN [%s]',$fao->name,$ao->zone->domain->name,$ao->ftn)); diff --git a/app/Console/Commands/NodesNew.php b/app/Console/Commands/NodesNew.php index 8e84db1..e54b98b 100644 --- a/app/Console/Commands/NodesNew.php +++ b/app/Console/Commands/NodesNew.php @@ -33,7 +33,7 @@ class NodesNew extends Command */ public function handle(): int { - $do = Domain::where('name',$this->argument('domain'))->singleOrFail(); + $do = Domain::where('name',$this->argument('domain'))->sole(); $ao = NULL; if ($this->option('netmail')) { diff --git a/app/Console/Commands/UserCodeSend.php b/app/Console/Commands/UserCodeSend.php index 4b497b4..030325b 100644 --- a/app/Console/Commands/UserCodeSend.php +++ b/app/Console/Commands/UserCodeSend.php @@ -25,7 +25,7 @@ class UserCodeSend extends Command public function handle(): int { $ao = Address::findFTN($this->argument('ftn')); - $uo = User::where('email',$this->argument('email'))->singleOrFail(); + $uo = User::where('email',$this->argument('email'))->sole(); Notification::route('netmail',$ao->uplink())->notify(new AddressLink($uo)); diff --git a/app/Http/Controllers/ZoneController.php b/app/Http/Controllers/ZoneController.php index 81ef762..a46f0b3 100644 --- a/app/Http/Controllers/ZoneController.php +++ b/app/Http/Controllers/ZoneController.php @@ -69,7 +69,7 @@ class ZoneController extends Controller $o->save(); $zo = Zone::where('zone_id',$request->zone_id) ->where('domain_id',$request->domain_id) - ->singleOrFail(); + ->sole(); // Find the zones 0/0 address, and assign it to this host. $ao = Address::where('zone_id',$zo->id) diff --git a/app/Jobs/NodelistImport.php b/app/Jobs/NodelistImport.php index 2e22c0a..241d030 100644 --- a/app/Jobs/NodelistImport.php +++ b/app/Jobs/NodelistImport.php @@ -144,8 +144,8 @@ class NodelistImport implements ShouldQueue return; } - $mailer_binkp = Mailer::where('name','BINKP')->singleOrFail(); - $mailer_emsi = Mailer::where('name','EMSI')->singleOrFail(); + $mailer_binkp = Mailer::where('name','BINKP')->sole(); + $mailer_emsi = Mailer::where('name','EMSI')->sole(); $p = $c = 0; diff --git a/app/Jobs/TicProcess.php b/app/Jobs/TicProcess.php index b2cb66e..a6d38dc 100644 --- a/app/Jobs/TicProcess.php +++ b/app/Jobs/TicProcess.php @@ -34,7 +34,7 @@ class TicProcess implements ShouldQueue */ public function __construct(private string $file,private ?string $domain=NULL) { - $this->do = $domain ? Domain::where('name',$domain)->singleOrFail() : NULL; + $this->do = $domain ? Domain::where('name',$domain)->sole() : NULL; $this->onQueue(self::QUEUE); } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index d302896..ab3ac4a 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -17,11 +17,11 @@ use App\Listeners\EchomailListener; use App\Listeners\Matrix\MessageListener; use App\Notifications\Channels\{EchomailChannel,MatrixChannel,NetmailChannel}; use App\Models\{Echomail,Netmail,User}; -use App\Traits\SingleOrFail; +use App\Traits\Single; class AppServiceProvider extends ServiceProvider { - use SingleOrFail; + use Single; /** * Register any application services. @@ -52,7 +52,7 @@ class AppServiceProvider extends ServiceProvider */ public function boot() { - static::bootSingleOrFail(); + static::bootSingle(); // Add our page assets Blade::directive('pa',function($expression) { diff --git a/app/Traits/SingleOrFail.php b/app/Traits/Single.php similarity index 51% rename from app/Traits/SingleOrFail.php rename to app/Traits/Single.php index 9335bc9..22d251a 100644 --- a/app/Traits/SingleOrFail.php +++ b/app/Traits/Single.php @@ -1,31 +1,17 @@ get(); - - if (($x=$result->count()) === 1) - return $result->first(); - - if ($x === 0) - throw new ModelNotFoundException('Query brings back 0 record(s) called for singleOrFail()'); - else - throw new \Exception(sprintf('Query brings back %d record(s) called for singleOrFail()',$x)); - }); - // When a query should return 1 object, or NULL if it doesnt Builder::macro('single',function () { $result = $this->get(); diff --git a/database/seeders/TestNodeHierarchy.php b/database/seeders/TestNodeHierarchy.php index 7641b0a..aee96bc 100644 --- a/database/seeders/TestNodeHierarchy.php +++ b/database/seeders/TestNodeHierarchy.php @@ -38,7 +38,7 @@ class TestNodeHierarchy extends Seeder ]); foreach (['a','b'] as $domain) { - $do = Domain::where('name',$domain)->singleOrFail(); + $do = Domain::where('name',$domain)->sole(); $this->hierarchy($do,100); $this->hierarchy($do,101); } @@ -84,7 +84,7 @@ class TestNodeHierarchy extends Seeder 'updated_at'=>Carbon::now(), ]); - $zo = Zone::where('zone_id',$zoneid)->where('domain_id',$domain->id)->singleOrFail(); + $zo = Zone::where('zone_id',$zoneid)->where('domain_id',$domain->id)->sole(); if (self::DEBUG) dump(['zo'=>$zo->zone_id,'rid'=>0,'hid'=>0,'nid'=>0]); diff --git a/tests/Feature/RoutingTest.php b/tests/Feature/RoutingTest.php index 2f49fde..214f654 100644 --- a/tests/Feature/RoutingTest.php +++ b/tests/Feature/RoutingTest.php @@ -57,7 +57,7 @@ class RoutingTest extends TestCase { //$this->seed(TestNodeHierarchy::class); - $do = Domain::where('name','a')->singleOrFail(); + $do = Domain::where('name','a')->sole(); $zo = $do->zones->where('zone_id',100)->pop(); return $zo->addresses; }