Deprecate singleOrFail() in favour of sole()

This commit is contained in:
Deon George 2024-11-08 23:31:21 +11:00
parent 5874b2aef1
commit 7352a74a12
18 changed files with 27 additions and 43 deletions

View File

@ -160,7 +160,7 @@ abstract class Packet extends FTNBase implements \Iterator, \Countable
try { try {
$o->zone = Zone::where('zone_id',$o->fz) $o->zone = Zone::where('zone_id',$o->fz)
->where('default',TRUE) ->where('default',TRUE)
->singleOrFail(); ->sole();
} catch (ModelNotFoundException $e) { } 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)); throw new InvalidPacketException(sprintf('%s:! We couldnt work out the packet zone, and there isnt a default for[%d]',self::LOGKEY,$o->fz));

View File

@ -179,11 +179,11 @@ final class DNS extends BaseProtocol
switch ($labels->first()) { switch ($labels->first()) {
case '_binkp': case '_binkp':
$mailer = Mailer::where('name','BINKP')->singleOrFail(); $mailer = Mailer::where('name','BINKP')->sole();
break; break;
case '_ifcico': case '_ifcico':
$mailer = Mailer::where('name','EMSI')->singleOrFail(); $mailer = Mailer::where('name','EMSI')->sole();
break; break;
default: default:

View File

@ -30,7 +30,7 @@ class AddressIdle extends Command
*/ */
public function handle(): int 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); return Job::dispatchSync($do,$this->option('ftn') ? Address::findFTN($this->option('ftn')) : NULL);
} }

View File

@ -27,8 +27,6 @@ class CommBinkpSend extends Command
*/ */
protected $description = 'BINKP send'; protected $description = 'BINKP send';
private const ID = 'BINKP';
/** /**
* Execute the console command. * Execute the console command.
* *
@ -42,7 +40,7 @@ class CommBinkpSend extends Command
Log::info(sprintf('CBS:- Call BINKP send for %s',$ao->ftn)); 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')) if ($this->option('now'))
Job::dispatchSync($ao,$mo); Job::dispatchSync($ao,$mo);

View File

@ -27,8 +27,6 @@ class CommEMSISend extends Command
*/ */
protected $description = 'EMSI send'; protected $description = 'EMSI send';
private const ID = 'EMSI';
/** /**
* Execute the console command. * Execute the console command.
* *
@ -42,7 +40,7 @@ class CommEMSISend extends Command
Log::info(sprintf('CES:- Call EMSI send for %s',$ao->ftn)); 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')) if ($this->option('now'))
Job::dispatchSync($ao,$mo); Job::dispatchSync($ao,$mo);

View File

@ -16,7 +16,7 @@ class ZoneCheck extends Command
public function handle(): int 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) { foreach ($do->zones->sortby('zone_id') as $zo) {
if ($this->option('zone') && ($this->option('zone') != $zo->zone_id)) if ($this->option('zone') && ($this->option('zone') != $zo->zone_id))

View File

@ -34,7 +34,8 @@ class EchoareaImport extends Command
*/ */
public function handle(): int 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')); return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix') ?: '',$this->option('unlink'));
} }
} }

View File

@ -34,7 +34,8 @@ class FileareaImport extends Command
*/ */
public function handle(): int 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')); return Job::dispatchSync($this->argument('file'),$do,$this->option('prefix') ?: '',$this->option('unlink'));
} }
} }

View File

@ -40,7 +40,7 @@ class Rescan extends Command
if (! $this->argument('area')) if (! $this->argument('area'))
throw new \Exception('Areaname is required'); 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) 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)); throw new \Exception(sprintf('File area [%s] is not in domain [%s] for FTN [%s]',$fao->name,$ao->zone->domain->name,$ao->ftn));

View File

@ -33,7 +33,7 @@ class NodesNew extends Command
*/ */
public function handle(): int public function handle(): int
{ {
$do = Domain::where('name',$this->argument('domain'))->singleOrFail(); $do = Domain::where('name',$this->argument('domain'))->sole();
$ao = NULL; $ao = NULL;
if ($this->option('netmail')) { if ($this->option('netmail')) {

View File

@ -25,7 +25,7 @@ class UserCodeSend extends Command
public function handle(): int public function handle(): int
{ {
$ao = Address::findFTN($this->argument('ftn')); $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)); Notification::route('netmail',$ao->uplink())->notify(new AddressLink($uo));

View File

@ -69,7 +69,7 @@ class ZoneController extends Controller
$o->save(); $o->save();
$zo = Zone::where('zone_id',$request->zone_id) $zo = Zone::where('zone_id',$request->zone_id)
->where('domain_id',$request->domain_id) ->where('domain_id',$request->domain_id)
->singleOrFail(); ->sole();
// Find the zones 0/0 address, and assign it to this host. // Find the zones 0/0 address, and assign it to this host.
$ao = Address::where('zone_id',$zo->id) $ao = Address::where('zone_id',$zo->id)

View File

@ -144,8 +144,8 @@ class NodelistImport implements ShouldQueue
return; return;
} }
$mailer_binkp = Mailer::where('name','BINKP')->singleOrFail(); $mailer_binkp = Mailer::where('name','BINKP')->sole();
$mailer_emsi = Mailer::where('name','EMSI')->singleOrFail(); $mailer_emsi = Mailer::where('name','EMSI')->sole();
$p = $c = 0; $p = $c = 0;

View File

@ -34,7 +34,7 @@ class TicProcess implements ShouldQueue
*/ */
public function __construct(private string $file,private ?string $domain=NULL) 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); $this->onQueue(self::QUEUE);
} }

View File

@ -17,11 +17,11 @@ use App\Listeners\EchomailListener;
use App\Listeners\Matrix\MessageListener; use App\Listeners\Matrix\MessageListener;
use App\Notifications\Channels\{EchomailChannel,MatrixChannel,NetmailChannel}; use App\Notifications\Channels\{EchomailChannel,MatrixChannel,NetmailChannel};
use App\Models\{Echomail,Netmail,User}; use App\Models\{Echomail,Netmail,User};
use App\Traits\SingleOrFail; use App\Traits\Single;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
use SingleOrFail; use Single;
/** /**
* Register any application services. * Register any application services.
@ -52,7 +52,7 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
static::bootSingleOrFail(); static::bootSingle();
// Add our page assets // Add our page assets
Blade::directive('pa',function($expression) { Blade::directive('pa',function($expression) {

View File

@ -1,31 +1,17 @@
<?php <?php
/** /**
* Add eloquent queries single(), singleOrFail(), singleOrNew() * Add eloquent queries single(), singleOrNew()
*/ */
namespace App\Traits; namespace App\Traits;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
trait SingleOrFail trait Single
{ {
private static function bootSingleOrFail(): void private static function bootSingle(): void
{ {
// When a query should return 1 object, or FAIL if it doesnt
// @deprecated use sole()
Builder::macro('singleOrFail',function () {
$result = $this->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 // When a query should return 1 object, or NULL if it doesnt
Builder::macro('single',function () { Builder::macro('single',function () {
$result = $this->get(); $result = $this->get();

View File

@ -38,7 +38,7 @@ class TestNodeHierarchy extends Seeder
]); ]);
foreach (['a','b'] as $domain) { foreach (['a','b'] as $domain) {
$do = Domain::where('name',$domain)->singleOrFail(); $do = Domain::where('name',$domain)->sole();
$this->hierarchy($do,100); $this->hierarchy($do,100);
$this->hierarchy($do,101); $this->hierarchy($do,101);
} }
@ -84,7 +84,7 @@ class TestNodeHierarchy extends Seeder
'updated_at'=>Carbon::now(), '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) if (self::DEBUG)
dump(['zo'=>$zo->zone_id,'rid'=>0,'hid'=>0,'nid'=>0]); dump(['zo'=>$zo->zone_id,'rid'=>0,'hid'=>0,'nid'=>0]);

View File

@ -57,7 +57,7 @@ class RoutingTest extends TestCase
{ {
//$this->seed(TestNodeHierarchy::class); //$this->seed(TestNodeHierarchy::class);
$do = Domain::where('name','a')->singleOrFail(); $do = Domain::where('name','a')->sole();
$zo = $do->zones->where('zone_id',100)->pop(); $zo = $do->zones->where('zone_id',100)->pop();
return $zo->addresses; return $zo->addresses;
} }