From 72b11172c8a89b4d60ccf1fce8384442652ac2d2 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 22 May 2025 17:57:39 +1000 Subject: [PATCH] Move some service\type::class methods into __get(), no functional changes --- app/Interfaces/ServiceItem.php | 26 ------- app/Models/Service/Broadband.php | 2 + app/Models/Service/SSL.php | 30 +++----- app/Models/Service/Type.php | 68 ++++++++----------- .../adminlte/service/domain/list.blade.php | 2 +- .../adminlte/service/email/list.blade.php | 2 +- .../adminlte/service/hosting/list.blade.php | 2 +- .../widget/broadband/details.blade.php | 4 +- .../service/widget/email/details.blade.php | 2 +- .../service/widget/hosting/details.blade.php | 2 +- .../service/widget/phone/details.blade.php | 4 +- 11 files changed, 50 insertions(+), 94 deletions(-) diff --git a/app/Interfaces/ServiceItem.php b/app/Interfaces/ServiceItem.php index 1a58feb..bb07a0d 100644 --- a/app/Interfaces/ServiceItem.php +++ b/app/Interfaces/ServiceItem.php @@ -6,13 +6,6 @@ use Carbon\Carbon; interface ServiceItem { - /** - * Months the service is contracted for. - * - * @return int - */ - public function getContractTermAttribute(): int; - /** * Return the Service Description. * @@ -20,29 +13,10 @@ interface ServiceItem */ public function getServiceDescriptionAttribute(): string; - /** - * Date the service expires - */ - public function getServiceExpireAttribute(): ?Carbon; - /** * Return the Service Name. * * @return string */ public function getServiceNameAttribute(): string; - - /** - * Has this service expired - * - * @return bool - */ - public function hasExpired(): bool; - - /** - * Is this service in a contract - * - * @return bool - */ - public function inContract(): bool; } \ No newline at end of file diff --git a/app/Models/Service/Broadband.php b/app/Models/Service/Broadband.php index 93dd0dd..d968c09 100644 --- a/app/Models/Service/Broadband.php +++ b/app/Models/Service/Broadband.php @@ -44,6 +44,8 @@ class Broadband extends Type implements ServiceUsage return $this->service_number ?: ($this->service_address ?: '-'); } + /* RELATIONS */ + /** * The usage information for broadband * diff --git a/app/Models/Service/SSL.php b/app/Models/Service/SSL.php index 846faf1..4153359 100644 --- a/app/Models/Service/SSL.php +++ b/app/Models/Service/SSL.php @@ -17,6 +17,15 @@ class SSL extends Type protected $public_key = NULL; protected $crt_parse = NULL; + public function __get($key): mixed + { + return match ($key) { + 'in_contract' => FALSE, + + default => parent::__get($key), + }; + } + /* STATIC METHODS */ public static function boot() @@ -82,25 +91,4 @@ class SSL extends Type ? Arr::get($this->crt_parse,'subject.CN') : Arr::get(openssl_csr_get_subject($this->csr),'CN',''); } - - /** - * Certificates have no contract and can be cancelled anytime. - * - * @return bool - */ - public function inContract(): bool - { - return FALSE; - } - - /* METHODS */ - - /** - * @return Carbon|null - * @deprecated use getServiceExpireAttribute() - */ - public function getValidToAttribute() - { - abort(500,'use getServiceExpireAttribute'); - } } \ No newline at end of file diff --git a/app/Models/Service/Type.php b/app/Models/Service/Type.php index 8968b5a..35ff785 100644 --- a/app/Models/Service/Type.php +++ b/app/Models/Service/Type.php @@ -22,6 +22,17 @@ abstract class Type extends Model implements ServiceItem public $timestamps = FALSE; + public function __get($key): mixed + { + return match ($key) { + 'contract_term' => $this->service->offering->contract_term, + 'has_expired' => (! $this->in_contract) && ($this->service->invoice_next && $this->service->invoice_next->isPast()), + 'in_contract' => $this->expire_at && $this->expire_at->isFuture(), + + default => parent::__get($key), + }; + } + /* RELATIONS */ /** @@ -50,11 +61,17 @@ abstract class Type extends Model implements ServiceItem ->where('site_id',$this->site_id); } - /* INTERFACE */ + /* ATTRIBUTES */ - public function getContractTermAttribute(): int + /** + * We need to cast some dates to LeenooksCarbon to get access to startOfHalf()/endOfHalf() methods + * + * @param $value + * @return LeenooksCarbon|null + */ + public function getExpireAtAttribute($value): ?LeenooksCarbon { - return $this->service->offering->contract_term; + return $value ? LeenooksCarbon::create($value) : NULL; } public function getServiceExpireAttribute(): ?LeenooksCarbon @@ -62,43 +79,8 @@ abstract class Type extends Model implements ServiceItem return $this->expire_at ?: $this->service->invoice_next_at; } - public function hasExpired(): bool - { - return (! $this->inContract()) - && ($this->service->invoice_next && $this->service->invoice_next->isPast()); - } - - public function inContract(): bool - { - return $this->expire_at - && $this->expire_at->isFuture(); - } - - /* ATTRIBUTES */ - - /** - * We need to cast some dates to LeenooksCarbon to get access to startOfHalf()/endOfHalf() methods - * - * @param $value - * @return LeenooksCarbon - */ - public function getExpireAtAttribute($value): ?LeenooksCarbon - { - return $value ? LeenooksCarbon::create($value) : NULL; - } - /* METHODS */ - /** - * Validation used to accept form input - * - * @return mixed - */ - public function validation(): array - { - return []; - } - /** * The supplier's service that we provide * @@ -119,4 +101,14 @@ abstract class Type extends Model implements ServiceItem { return collect(); } + + /** + * Validation used to accept form input + * + * @return mixed + */ + public function validation(): array + { + return []; + } } \ No newline at end of file diff --git a/resources/views/theme/backend/adminlte/service/domain/list.blade.php b/resources/views/theme/backend/adminlte/service/domain/list.blade.php index 16c6bee..416413f 100644 --- a/resources/views/theme/backend/adminlte/service/domain/list.blade.php +++ b/resources/views/theme/backend/adminlte/service/domain/list.blade.php @@ -37,7 +37,7 @@ @foreach ($o as $oo) - hasExpired()) class="table-danger" @endif> + has_expired) class="table-danger" @endif> {{ $oo->service->sid }} {{ $oo->service->account->name }} {{ $oo->service->product->name }} diff --git a/resources/views/theme/backend/adminlte/service/email/list.blade.php b/resources/views/theme/backend/adminlte/service/email/list.blade.php index 103782f..8ce18ae 100644 --- a/resources/views/theme/backend/adminlte/service/email/list.blade.php +++ b/resources/views/theme/backend/adminlte/service/email/list.blade.php @@ -39,7 +39,7 @@ @foreach ($o as $oo) - hasExpired()) class="table-danger" @endif> + has_expired) class="table-danger" @endif> {{ $oo->service->sid }} {{ $oo->service->account->name }} {{ $oo->service->product->name }} diff --git a/resources/views/theme/backend/adminlte/service/hosting/list.blade.php b/resources/views/theme/backend/adminlte/service/hosting/list.blade.php index 1426f1a..9a86e17 100644 --- a/resources/views/theme/backend/adminlte/service/hosting/list.blade.php +++ b/resources/views/theme/backend/adminlte/service/hosting/list.blade.php @@ -36,7 +36,7 @@ @foreach ($o as $oo) - hasExpired()) class="table-danger" @endif> + has_expired) class="table-danger" @endif> {{ $oo->service->sid }} {{ $oo->service->account->name }} {{ $oo->service->product->name }} diff --git a/resources/views/theme/backend/adminlte/service/widget/broadband/details.blade.php b/resources/views/theme/backend/adminlte/service/widget/broadband/details.blade.php index 2c3d21e..6bb1d52 100644 --- a/resources/views/theme/backend/adminlte/service/widget/broadband/details.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/broadband/details.blade.php @@ -62,7 +62,7 @@ IP6 Address {{ $o->ip6address ?: '-' }} - @if ($o->inContract()) + @if ($o->in_contract) Contract {{ $o->contract_term }} months @@ -74,7 +74,7 @@ @endif Cancel Notice - 1 month @if($o->inContract())(after {{ $o->service_expire->subMonth()->format('Y-m-d') }})@endif + 1 month @if($o->in_contract)(after {{ $o->service_expire->subMonth()->format('Y-m-d') }})@endif @if(($x=$o->service->changes()->where('service__change.active',TRUE)->where('complete',FALSE)->get()->pop())) diff --git a/resources/views/theme/backend/adminlte/service/widget/email/details.blade.php b/resources/views/theme/backend/adminlte/service/widget/email/details.blade.php index 94bc343..427637d 100644 --- a/resources/views/theme/backend/adminlte/service/widget/email/details.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/email/details.blade.php @@ -20,7 +20,7 @@ {{ $o->service_connect_date->format('Y-m-d') }} @endif - @if ($o->inContract()) + @if ($o->in_contract) Contract Term {{ $o->service->contract_term }} months diff --git a/resources/views/theme/backend/adminlte/service/widget/hosting/details.blade.php b/resources/views/theme/backend/adminlte/service/widget/hosting/details.blade.php index fd00fa5..46f1443 100644 --- a/resources/views/theme/backend/adminlte/service/widget/hosting/details.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/hosting/details.blade.php @@ -34,7 +34,7 @@ {{ $o->service_connect_date->format('Y-m-d') }} @endif - @if ($o->inContract()) + @if ($o->in_contract) Contract Term {{ $o->service->contract_term }} months diff --git a/resources/views/theme/backend/adminlte/service/widget/phone/details.blade.php b/resources/views/theme/backend/adminlte/service/widget/phone/details.blade.php index dbd7e03..103337f 100644 --- a/resources/views/theme/backend/adminlte/service/widget/phone/details.blade.php +++ b/resources/views/theme/backend/adminlte/service/widget/phone/details.blade.php @@ -46,7 +46,7 @@ {{ $o->service->product->type->allowance_string() }} Included Calls @endif - @if ($o->inContract()) + @if ($o->in_contract) Contract {{ $o->contract_term }} months @@ -58,7 +58,7 @@ @endif Cancel Notice - 1 month @if($o->inContract())(after {{ $o->service_expire->subMonth()->format('Y-m-d') }})@endif + 1 month @if($o->in_contract)(after {{ $o->service_expire->subMonth()->format('Y-m-d') }})@endif