From 006e819f3a7c340afc6fdae2cd8bb0dcd459c98b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 18 Jun 2024 10:24:03 +1000 Subject: [PATCH] Refactor for cache statics moving to container --- .../RecurringExpenseToExpenseFactory.php | 10 ++- .../Transformer/TransactionTransformer.php | 20 ++--- .../Yodlee/Transformer/IncomeTransformer.php | 16 ++-- .../SubscriptionPurchaseController.php | 19 +++-- .../Requests/Client/StoreClientRequest.php | 46 ++++++------ .../Requests/Client/UpdateClientRequest.php | 28 +++---- .../Requests/Shop/StoreShopClientRequest.php | 20 +++-- app/Import/Transformer/BaseTransformer.php | 19 ++--- .../Authentication/RegisterOrLogin.php | 4 +- app/Livewire/BillingPortalPurchase.php | 24 ++++-- app/Livewire/BillingPortalPurchasev2.php | 23 ++++-- app/Models/Client.php | 36 +-------- app/Models/ClientContact.php | 12 ++- app/Models/Company.php | 64 +++++----------- app/Models/Vendor.php | 12 ++- app/Models/VendorContact.php | 8 +- .../Authorize/AuthorizeCustomer.php | 8 +- app/Providers/StaticServiceProvider.php | 19 +++-- app/Services/Chart/ChartService.php | 8 +- app/Services/Chart/ChartServiceLegacy.php | 8 +- app/Services/Pdf/PdfConfiguration.php | 12 ++- app/Utils/HtmlEngine.php | 39 ++++------ app/Utils/Statics.php | 75 ++++++++++++------- app/Utils/TranslationHelper.php | 30 ++++++-- app/Utils/VendorHtmlEngine.php | 28 ++----- 25 files changed, 292 insertions(+), 296 deletions(-) diff --git a/app/Factory/RecurringExpenseToExpenseFactory.php b/app/Factory/RecurringExpenseToExpenseFactory.php index de59952d87..d190340840 100644 --- a/app/Factory/RecurringExpenseToExpenseFactory.php +++ b/app/Factory/RecurringExpenseToExpenseFactory.php @@ -82,11 +82,15 @@ class RecurringExpenseToExpenseFactory } else { $locale = $recurring_expense->company->locale(); - $date_formats = Cache::get('date_formats'); + //@deprecated + // $date_formats = Cache::get('date_formats'); - $date_format = $date_formats->filter(function ($item) use ($recurring_expense) { + /** @var \Illuminate\Support\Collection<\App\Models\DateFormat> */ + $date_formats = app('date_formats'); + + $date_format = $date_formats->first(function ($item) use ($recurring_expense) { return $item->id == $recurring_expense->company->settings->date_format_id; - })->first()->format; + })->format; } Carbon::setLocale($locale); diff --git a/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php b/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php index d2259150e1..8e85a80628 100644 --- a/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php +++ b/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php @@ -156,22 +156,16 @@ class TransactionTransformer implements BankRevenueInterface private function convertCurrency(string $code) { - $currencies = Cache::get('currencies'); + $currencies = app('currencies'); - if (!$currencies) { - $this->buildCache(true); - } - - $currency = $currencies->filter(function ($item) use ($code) { + $currency = $currencies->first(function ($item) use ($code) { + /** @var \App\Models\Currency $item */ return $item->code == $code; - })->first(); - - if ($currency) { - return $currency->id; - } - - return 1; + }); + /** @var \App\Models\Currency $currency */ + return $currency ? $currency->id : 1; //@phpstan-ignore-line + } private function formatDate(string $input) diff --git a/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php b/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php index d804df272b..d14e67bc01 100644 --- a/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php +++ b/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php @@ -171,20 +171,16 @@ class IncomeTransformer implements BankRevenueInterface private function convertCurrency(string $code) { - $currencies = Cache::get('currencies'); - if (! $currencies) { - $this->buildCache(true); - } + $currencies = app('currencies'); - $currency = $currencies->filter(function ($item) use ($code) { + $currency = $currencies->first(function ($item) use ($code) { + /** @var \App\Models\Currency $item */ return $item->code == $code; - })->first(); + }); - if ($currency) { - return $currency->id; - } + /** @var \App\Models\Currency $currency */ + return $currency ? $currency->id : 1; //@phpstan-ignore-line - return 1; } } diff --git a/app/Http/Controllers/ClientPortal/SubscriptionPurchaseController.php b/app/Http/Controllers/ClientPortal/SubscriptionPurchaseController.php index 579047f06d..677fb3ccee 100644 --- a/app/Http/Controllers/ClientPortal/SubscriptionPurchaseController.php +++ b/app/Http/Controllers/ClientPortal/SubscriptionPurchaseController.php @@ -90,15 +90,20 @@ class SubscriptionPurchaseController extends Controller * Set locale for incoming request. * * @param string $locale + * @return string */ - private function setLocale(string $locale): void + private function setLocale(string $locale): string { - $record = Cache::get('languages')->filter(function ($item) use ($locale) { - return $item->locale == $locale; - })->first(); + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); - if ($record) { - App::setLocale($record->locale); - } + $record = $languages->first(function ($item) use ($locale) { + /** @var \App\Models\Language $item */ + return $item->locale == $locale; + }); + + return $record ? $record->locale : 'en'; + } } diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index f1d4c84518..8592d93f2e 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -185,48 +185,44 @@ class StoreClientRequest extends Request ]; } - private function getLanguageId($language_code) + private function getLanguageId(string $language_code) { - $languages = Cache::get('languages'); + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); - $language = $languages->filter(function ($item) use ($language_code) { + $language = $languages->first(function ($item) use ($language_code) { return $item->locale == $language_code; - })->first(); + }); - if ($language) { - return (string) $language->id; - } + return $language ? (string)$language->id : ''; - return ''; } - private function getCountryCode($country_code) + private function getCountryCode(string $country_code) { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); - $country = $countries->filter(function ($item) use ($country_code) { + $country = $countries->first(function ($item) use ($country_code) { return $item->iso_3166_2 == $country_code || $item->iso_3166_3 == $country_code; - })->first(); + }); - if ($country) { - return (string) $country->id; - } - - return ''; + return $country ? (string) $country->id : ''; + } private function getCurrencyCode($code) { - $currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); - $currency = $currencies->filter(function ($item) use ($code) { + $currency = $currencies->first(function ($item) use ($code) { return $item->code == $code; - })->first(); + }); - if ($currency) { - return (string) $currency->id; - } - - return ''; + return $currency ? (string)$currency->id : ''; + } } diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index 1378b239ed..8933954de9 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -139,32 +139,28 @@ class UpdateClientRequest extends Request private function getCountryCode($country_code) { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); - $country = $countries->filter(function ($item) use ($country_code) { + $country = $countries->first(function ($item) use ($country_code) { return $item->iso_3166_2 == $country_code || $item->iso_3166_3 == $country_code; - })->first(); + }); - if ($country) { - return (string) $country->id; - } - - return ''; + return $country ? (string) $country->id : ''; } private function getLanguageId($language_code) { - $languages = Cache::get('languages'); + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); - $language = $languages->filter(function ($item) use ($language_code) { + $language = $languages->first(function ($item) use ($language_code) { return $item->locale == $language_code; - })->first(); + }); - if ($language) { - return (string) $language->id; - } - - return ''; + return $language ? (string) $language->id : ''; } /** diff --git a/app/Http/Requests/Shop/StoreShopClientRequest.php b/app/Http/Requests/Shop/StoreShopClientRequest.php index a41c4e26d0..3dc6e78d3c 100644 --- a/app/Http/Requests/Shop/StoreShopClientRequest.php +++ b/app/Http/Requests/Shop/StoreShopClientRequest.php @@ -155,23 +155,27 @@ class StoreShopClientRequest extends Request private function getCountryCode($country_code) { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); - $country = $countries->filter(function ($item) use ($country_code) { + $country = $countries->first(function ($item) use ($country_code) { return $item->iso_3166_2 == $country_code || $item->iso_3166_3 == $country_code; - })->first(); + }); - return (string) $country->id; + return $country ? (string) $country->id : ''; } private function getCurrencyCode($code) { - $currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $currencies = app('currencies'); - $currency = $currencies->filter(function ($item) use ($code) { + $currency = $currencies->first(function ($item) use ($code) { return $item->code == $code; - })->first(); + }); - return (string) $currency->id; + return $currency ? (string) $currency->id : ''; } } diff --git a/app/Import/Transformer/BaseTransformer.php b/app/Import/Transformer/BaseTransformer.php index 45c7fd8fca..2dc91e6c2f 100644 --- a/app/Import/Transformer/BaseTransformer.php +++ b/app/Import/Transformer/BaseTransformer.php @@ -119,17 +119,18 @@ class BaseTransformer { $code = array_key_exists($key, $data) ? $data[$key] : false; - $currencies = Cache::get('currencies'); + if(!$code) + return $this->company->settings->currency_id; - $currency = $currencies - ->filter(function ($item) use ($code) { - return $item->code == $code; - }) - ->first(); + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + $currency = $currencies->first(function ($item) use ($code) { + return $item->code == $code; + }); + + return $currency ? (string) $currency->id : $this->company->settings->currency_id; - return $currency - ? $currency->id - : $this->company->settings->currency_id; } public function getFrequency($frequency = RecurringInvoice::FREQUENCY_MONTHLY): int diff --git a/app/Livewire/BillingPortal/Authentication/RegisterOrLogin.php b/app/Livewire/BillingPortal/Authentication/RegisterOrLogin.php index 100d1e6e17..559ad798d1 100644 --- a/app/Livewire/BillingPortal/Authentication/RegisterOrLogin.php +++ b/app/Livewire/BillingPortal/Authentication/RegisterOrLogin.php @@ -256,7 +256,9 @@ class RegisterOrLogin extends Component public function render() { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); return view('billing-portal.v3.authentication.register-or-login', [ 'countries' => $countries, diff --git a/app/Livewire/BillingPortalPurchase.php b/app/Livewire/BillingPortalPurchase.php index d59e5e4789..0a15710018 100644 --- a/app/Livewire/BillingPortalPurchase.php +++ b/app/Livewire/BillingPortalPurchase.php @@ -281,17 +281,25 @@ class BillingPortalPurchase extends Component } if (array_key_exists('currency_id', $this->request_data)) { - $currency = Cache::get('currencies')->filter(function ($item) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + $currency = $currencies->first(function ($item) { return $item->id == $this->request_data['currency_id']; - })->first(); + }); if ($currency) { $data['settings']->currency_id = $currency->id; } } elseif ($this->subscription->group_settings && property_exists($this->subscription->group_settings->settings, 'currency_id')) { - $currency = Cache::get('currencies')->filter(function ($item) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + $currency = $currencies->first(function ($item) { return $item->id == $this->subscription->group_settings->settings->currency_id; - })->first(); + }); if ($currency) { $data['settings']->currency_id = $currency->id; @@ -300,10 +308,12 @@ class BillingPortalPurchase extends Component if (array_key_exists('locale', $this->request_data)) { $request = $this->request_data; - - $record = Cache::get('languages')->filter(function ($item) use ($request) { + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); + $record = $languages->first(function ($item) use ($request) { return $item->locale == $request['locale']; - })->first(); + }); if ($record) { $data['settings']['language_id'] = (string)$record->id; diff --git a/app/Livewire/BillingPortalPurchasev2.php b/app/Livewire/BillingPortalPurchasev2.php index 0bdab5de77..d707707578 100644 --- a/app/Livewire/BillingPortalPurchasev2.php +++ b/app/Livewire/BillingPortalPurchasev2.php @@ -676,17 +676,25 @@ class BillingPortalPurchasev2 extends Component } if (array_key_exists('currency_id', $this->request_data)) { - $currency = Cache::get('currencies')->filter(function ($item) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + $currency = $currencies->first(function ($item) { return $item->id == $this->request_data['currency_id']; - })->first(); + }); if ($currency) { $data['settings']->currency_id = $currency->id; } } elseif ($this->subscription->group_settings && property_exists($this->subscription->group_settings->settings, 'currency_id')) { - $currency = Cache::get('currencies')->filter(function ($item) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + $currency = $currencies->first(function ($item) { return $item->id == $this->subscription->group_settings->settings->currency_id; - })->first(); + }); if ($currency) { $data['settings']->currency_id = $currency->id; @@ -696,9 +704,12 @@ class BillingPortalPurchasev2 extends Component if (array_key_exists('locale', $this->request_data)) { $request = $this->request_data; - $record = Cache::get('languages')->filter(function ($item) use ($request) { + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); + + $record = $languages->first(function ($item) use ($request) { return $item->locale == $request['locale']; - })->first(); + }); if ($record) { $data['settings']['language_id'] = (string)$record->id; diff --git a/app/Models/Client.php b/app/Models/Client.php index c25df2f994..f9a68fbe8f 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -378,15 +378,11 @@ class Client extends BaseModel implements HasLocalePreference public function language() { - $languages = app('languages'); - // $languages = Cache::get('languages'); - // if (! $languages) { - // $this->buildCache(true); - // } + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); return $languages->first(function ($item) { - /** @var \stdClass $item */ return $item->id == $this->getSetting('language_id'); }); } @@ -412,17 +408,10 @@ class Client extends BaseModel implements HasLocalePreference public function date_format() { - /** @var \Illuminate\Support\Collection $date_formats */ + /** @var \Illuminate\Support\Collection */ $date_formats = app('date_formats'); - // $date_formats = Cache::get('date_formats'); - - // if (! $date_formats) { - // $this->buildCache(true); - // } return $date_formats->first(function ($item) { - - /** @var \stdClass $item */ return $item->id == $this->getSetting('date_format_id'); })->format; } @@ -430,17 +419,10 @@ class Client extends BaseModel implements HasLocalePreference public function currency() { - /** @var \Illuminate\Support\Collection $currencies */ + /** @var \Illuminate\Support\Collection */ $currencies = app('currencies'); - // $currencies = Cache::get('currencies'); - - // if (! $currencies) { - // $this->buildCache(true); - // } return $currencies->first(function ($item) { - - /** @var \stdClass $item */ return $item->id == $this->getSetting('currency_id'); }); } @@ -751,16 +733,6 @@ class Client extends BaseModel implements HasLocalePreference public function preferredLocale() { $this->language()->locale ?? 'en'; - // $languages = app('languages'); - // $languages = Cache::get('languages'); - - // if (! $languages) { - // $this->buildCache(true); - // } - - // return $languages->first(function ($item) { - // return $item->id == $this->getSetting('language_id'); - // })->locale; } public function backup_path(): string diff --git a/app/Models/ClientContact.php b/app/Models/ClientContact.php index 33816d5c01..fd47d0e6fe 100644 --- a/app/Models/ClientContact.php +++ b/app/Models/ClientContact.php @@ -271,15 +271,13 @@ class ClientContact extends Authenticatable implements HasLocalePreference public function preferredLocale() { - $languages = Cache::get('languages'); + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); - if (! $languages) { - $this->buildCache(true); - } - - return $languages->filter(function ($item) { + return $languages->first(function ($item) { return $item->id == $this->client->getSetting('language_id'); - })->first()->locale; + })->locale; } public function routeNotificationForMail($notification) diff --git a/app/Models/Company.php b/app/Models/Company.php index c20d096c24..8a0ef899de 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -636,24 +636,13 @@ class Company extends BaseModel public function country() { + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ $countries = app('countries'); - // $countries = Cache::get('countries'); - - // if (! $companies) { - // $this->buildCache(true); - - // $companies = Cache::get('countries'); - // } - return $countries->first(function ($item) { - - /** @var \stdClass $item */ return $item->id == $this->getSetting('country_id'); }); - - // return $this->belongsTo(Country::class); - // return Country::find($this->settings->country_id); } public function group_settings() @@ -663,20 +652,14 @@ class Company extends BaseModel public function timezone() { - // $timezones = Cache::get('timezones'); + /** @var \Illuminate\Support\Collection<\App\Models\TimeZone> */ $timezones = app('timezones'); - // if (! $timezones) { - // $this->buildCache(true); - // } - return $timezones->first(function ($item) { - /** @var \stdClass $item */ return $item->id == $this->settings->timezone_id; }); - // return Timezone::find($this->settings->timezone_id); } public function designs() @@ -701,22 +684,15 @@ class Company extends BaseModel public function language() { - $languages = Cache::get('languages'); + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); - //build cache and reinit - if (! $languages) { - $this->buildCache(true); - $languages = Cache::get('languages'); - } - - //if the cache is still dead, get from DB - if (!$languages && property_exists($this->settings, 'language_id')) { - return Language::find($this->settings->language_id); - } - - return $languages->filter(function ($item) { + $language = $languages->first(function ($item) { return $item->id == $this->settings->language_id; - })->first(); + }); + + return $language ?? $languages->first(); } public function getLocale() @@ -757,11 +733,13 @@ class Company extends BaseModel public function currency() { - $currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); - return $currencies->filter(function ($item) { + return $currencies->first(function ($item) { return $item->id == $this->settings->currency_id; - })->first(); + }); } /** @@ -974,15 +952,13 @@ class Company extends BaseModel public function date_format() { - $date_formats = Cache::get('date_formats'); + + /** @var \Illuminate\Support\Collection<\App\Models\DateFormat> */ + $date_formats = app('date_formats'); - if (! $date_formats) { - $this->buildCache(true); - } - - return $date_formats->filter(function ($item) { + return $date_formats->first(function ($item) { return $item->id == $this->getSetting('date_format_id'); - })->first()->format; + })->format; } public function getInvoiceCert() diff --git a/app/Models/Vendor.php b/app/Models/Vendor.php index a07980508c..7c7cc24985 100644 --- a/app/Models/Vendor.php +++ b/app/Models/Vendor.php @@ -177,19 +177,17 @@ class Vendor extends BaseModel public function currency() { - $currencies = Cache::get('currencies'); - - if (! $currencies) { - $this->buildCache(true); - } + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); if (! $this->currency_id) { return $this->company->currency(); } - return $currencies->filter(function ($item) { + return $currencies->first(function ($item) { return $item->id == $this->currency_id; - })->first(); + }); } public function timezone() diff --git a/app/Models/VendorContact.php b/app/Models/VendorContact.php index c04f53541f..156928f800 100644 --- a/app/Models/VendorContact.php +++ b/app/Models/VendorContact.php @@ -183,11 +183,13 @@ class VendorContact extends Authenticatable implements HasLocalePreference public function preferredLocale() { - $languages = Cache::get('languages'); + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); - return $languages->filter(function ($item) { + return $languages->first(function ($item) { return $item->id == $this->company->getSetting('language_id'); - })->first()->locale; + })->locale; } /** diff --git a/app/PaymentDrivers/Authorize/AuthorizeCustomer.php b/app/PaymentDrivers/Authorize/AuthorizeCustomer.php index 253a016b4b..887da45dd8 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCustomer.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCustomer.php @@ -161,11 +161,13 @@ class AuthorizeCustomer private function getCountryCode($country_code) { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); - $country = $countries->filter(function ($item) use ($country_code) { + $country = $countries->first(function ($item) use ($country_code) { return $item->iso_3166_2 == $country_code || $item->iso_3166_3 == $country_code; - })->first(); + }); return (string) $country->id; } diff --git a/app/Providers/StaticServiceProvider.php b/app/Providers/StaticServiceProvider.php index 523a92e08c..5cdd13a58f 100644 --- a/app/Providers/StaticServiceProvider.php +++ b/app/Providers/StaticServiceProvider.php @@ -33,56 +33,61 @@ class StaticServiceProvider extends ServiceProvider */ public function register() { - - + /** @return \Illuminate\Support\Collection */ app()->singleton('currencies', function ($app) { return Currency::query()->orderBy('name')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('languages', function ($app) { return Language::query()->orderBy('name')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('countries', function ($app) { return Country::query()->orderBy('name')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('payment_types', function ($app) { return PaymentTerm::query()->orderBy('num_days')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('industries', function ($app) { return Industry::query()->orderBy('name')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('banks', function ($app) { return Bank::query()->orderBy('name')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('date_formats', function ($app) { return DateFormat::query()->orderBy('id')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('timezones', function ($app) { return Timezone::query()->orderBy('id')->get(); }); - + + /** @return \Illuminate\Support\Collection */ app()->singleton('gateways', function ($app) { return Gateway::query()->orderBy('id')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('industries', function ($app) { return Industry::query()->orderBy('id')->get(); }); + /** @return \Illuminate\Support\Collection */ app()->singleton('sizes', function ($app) { return Size::query()->orderBy('id')->get(); }); - /** @deprecated */ - app()->singleton('banks', function ($app) { - return Bank::query()->orderBy('id')->get(); - }); app()->singleton('templates', function ($app) { return [ diff --git a/app/Services/Chart/ChartService.php b/app/Services/Chart/ChartService.php index b6178436db..7bd2ed911a 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -57,7 +57,9 @@ class ChartService /* Merge and filter by unique */ $currencies = $currencies->merge($expense_currencies)->unique(); - $cache_currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $cache_currencies = app('currencies'); $filtered_currencies = $cache_currencies->whereIn('id', $currencies)->all(); @@ -162,7 +164,9 @@ class ChartService private function addCurrencyCodes($data_set): array { - $currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); foreach ($data_set as $key => $value) { $data_set[$key]->currency_id = str_replace('"', '', $value->currency_id); diff --git a/app/Services/Chart/ChartServiceLegacy.php b/app/Services/Chart/ChartServiceLegacy.php index bebc45bc33..84b839ac11 100644 --- a/app/Services/Chart/ChartServiceLegacy.php +++ b/app/Services/Chart/ChartServiceLegacy.php @@ -53,7 +53,9 @@ class ChartServiceLegacy /* Merge and filter by unique */ $currencies = $currencies->merge($expense_currencies)->unique(); - $cache_currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $cache_currencies = app('currencies'); $filtered_currencies = $cache_currencies->whereIn('id', $currencies)->all(); @@ -135,7 +137,9 @@ class ChartServiceLegacy private function addCurrencyCodes($data_set): array { - $currencies = Cache::get('currencies'); + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); foreach ($data_set as $key => $value) { $data_set[$key]->currency_id = str_replace('"', '', $value->currency_id); diff --git a/app/Services/Pdf/PdfConfiguration.php b/app/Services/Pdf/PdfConfiguration.php index df6c9d3853..fddbc0766a 100644 --- a/app/Services/Pdf/PdfConfiguration.php +++ b/app/Services/Pdf/PdfConfiguration.php @@ -457,15 +457,13 @@ class PdfConfiguration */ public function setDateFormat(): self { - $date_formats = Cache::get('date_formats'); + + /** @var \Illuminate\Support\Collection<\App\Models\DateFormat> */ + $date_formats = app('date_formats'); - if (! $date_formats) { - $this->buildCache(true); - } - - $this->date_format = $date_formats->filter(function ($item) { + $this->date_format = $date_formats->first(function ($item) { return $item->id == $this->settings->date_format_id; - })->first()->format; + })->format; return $this; } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 5efa54297c..073d7d55b8 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -918,39 +918,30 @@ class HtmlEngine private function getCountryName(): string { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); - if (! $countries) { - $this->buildCache(true); + $country = $countries->first(function ($item) { + return $item->id == $this->settings->country_id; + }); - $countries = Cache::get('countries'); - } - - if ($countries) { - $country = $countries->filter(function ($item) { - return $item->id == $this->settings->country_id; - })->first(); - } else { - $country = Country::find($this->settings->country_id); - } - - if ($country) { - return ctrans('texts.country_' . $country->name); - } - - return ' '; + return $country ? ctrans('texts.country_' . $country->name) : ctrans('texts.country_' . $countries->first()->name); } private function getCountryCode(): string { - $country = Country::find($this->settings->country_id); - if ($country) { - return $country->iso_3166_2; - } - return ' '; + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); + + $country = $countries->first(function ($item) { + return $item->id == $this->settings->country_id; + }); + + return $country ? $country->iso_3166_2 : ' '; } /** * Due to the way we are compiling the blade template we diff --git a/app/Utils/Statics.php b/app/Utils/Statics.php index e4b2201db6..aa8021c3d8 100644 --- a/app/Utils/Statics.php +++ b/app/Utils/Statics.php @@ -69,62 +69,81 @@ class Statics { $data = []; - foreach (config('ninja.cached_tables') as $name => $class) { - if (! Cache::has($name)) { - // check that the table exists in case the migration is pending - if (! Schema::hasTable((new $class())->getTable())) { - continue; - } - if ($name == 'payment_terms') { - $orderBy = 'num_days'; - } elseif ($name == 'fonts') { - $orderBy = 'sort_order'; - } elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) { - $orderBy = 'name'; - } else { - $orderBy = 'id'; - } - $tableData = $class::orderBy($orderBy)->get(); - if ($tableData->count()) { - Cache::forever($name, $tableData); - } - } + // foreach (config('ninja.cached_tables') as $name => $class) { + // if (! Cache::has($name)) { + // // check that the table exists in case the migration is pending + // if (! Schema::hasTable((new $class())->getTable())) { + // continue; + // } + // if ($name == 'payment_terms') { + // $orderBy = 'num_days'; + // } elseif ($name == 'fonts') { + // $orderBy = 'sort_order'; + // } elseif (in_array($name, ['currencies', 'industries', 'languages', 'countries', 'banks'])) { + // $orderBy = 'name'; + // } else { + // $orderBy = 'id'; + // } + // $tableData = $class::orderBy($orderBy)->get(); + // if ($tableData->count()) { + // Cache::forever($name, $tableData); + // } + // } - $data[$name] = Cache::get($name); - } + // $data[$name] = app($name); + // } if ($locale) { - $data['industries'] = Cache::get('industries')->each(function ($industry) { + + /** @var \Illuminate\Support\Collection<\App\Models\Industry> */ + $industries = app('industries'); + + $data['industries'] = $industries->each(function ($industry) { $industry->name = ctrans('texts.industry_'.$industry->name); })->sortBy(function ($industry) { return $industry->name; })->values(); - $data['countries'] = Cache::get('countries')->each(function ($country) { + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); + + $data['countries'] = $countries->each(function ($country) { $country->name = ctrans('texts.country_'.$country->name); })->sortBy(function ($country) { return $country->name; })->values(); - $data['payment_types'] = Cache::get('payment_types')->each(function ($pType) { + + /** @var \Illuminate\Support\Collection<\App\Models\PaymentType> */ + $payment_types = app('payment_types'); + + $data['payment_types'] = $payment_types->each(function ($pType) { $pType->name = ctrans('texts.payment_type_'.$pType->name); })->sortBy(function ($pType) { return $pType->name; })->values(); - $data['languages'] = Cache::get('languages')->each(function ($lang) { + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $payment_types = app('languages'); + + $data['languages'] = $payment_types->each(function ($lang) { $lang->name = ctrans('texts.lang_'.$lang->name); })->sortBy(function ($lang) { return $lang->name; })->values(); - $data['currencies'] = Cache::get('currencies')->each(function ($currency) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + $data['currencies'] = $currencies->each(function ($currency) { $currency->name = ctrans('texts.currency_'.Str::slug($currency->name, '_')); })->sortBy(function ($currency) { return $currency->name; })->values(); - $data['templates'] = Cache::get('templates'); + $data['templates'] = app('templates'); } $data['bulk_updates'] = [ diff --git a/app/Utils/TranslationHelper.php b/app/Utils/TranslationHelper.php index 71ebec95f9..19793aad89 100644 --- a/app/Utils/TranslationHelper.php +++ b/app/Utils/TranslationHelper.php @@ -19,7 +19,11 @@ class TranslationHelper { public static function getIndustries() { - return Cache::get('industries')->each(function ($industry) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $industries = app('industries'); + + return $industries->each(function ($industry) { $industry->name = ctrans('texts.industry_'.$industry->name); })->sortBy(function ($industry) { return $industry->name; @@ -28,7 +32,11 @@ class TranslationHelper public static function getCountries() { - return Cache::get('countries')->each(function ($country) { + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); + + return $countries->each(function ($country) { $country->name = ctrans('texts.country_'.$country->name); })->sortBy(function ($country) { return $country->iso_3166_2; @@ -37,7 +45,11 @@ class TranslationHelper public static function getPaymentTypes() { - return Cache::get('payment_types')->each(function ($pType) { + + /** @var \Illuminate\Support\Collection<\App\Models\PaymentType> */ + $payment_types = app('payment_types'); + + return $payment_types->each(function ($pType) { $pType->name = ctrans('texts.payment_type_'.$pType->name); })->sortBy(function ($pType) { return $pType->name; @@ -46,7 +58,11 @@ class TranslationHelper public static function getLanguages() { - return Cache::get('languages')->each(function ($lang) { + + /** @var \Illuminate\Support\Collection<\App\Models\Language> */ + $languages = app('languages'); + + return $languages->each(function ($lang) { $lang->name = ctrans('texts.lang_'.$lang->name); })->sortBy(function ($lang) { return $lang->name; @@ -55,7 +71,11 @@ class TranslationHelper public static function getCurrencies() { - return Cache::get('currencies')->each(function ($currency) { + + /** @var \Illuminate\Support\Collection<\App\Models\Currency> */ + $currencies = app('currencies'); + + return $currencies->each(function ($currency) { $currency->name = ctrans('texts.currency_'.Str::slug($currency->name, '_')); })->sortBy(function ($currency) { return $currency->name; diff --git a/app/Utils/VendorHtmlEngine.php b/app/Utils/VendorHtmlEngine.php index 1d89396178..1b2b435df2 100644 --- a/app/Utils/VendorHtmlEngine.php +++ b/app/Utils/VendorHtmlEngine.php @@ -567,27 +567,15 @@ class VendorHtmlEngine private function getCountryName(): string { - $countries = Cache::get('countries'); + + /** @var \Illuminate\Support\Collection<\App\Models\Country> */ + $countries = app('countries'); - if (! $countries) { - $this->buildCache(true); - - $countries = Cache::get('countries'); - } - - if ($countries) { - $country = $countries->filter(function ($item) { - return $item->id == $this->settings->country_id; - })->first(); - } else { - $country = Country::find($this->settings->country_id); - } - - if ($country) { - return ctrans('texts.country_' . $country->name); - } - - return ' '; + $country = $countries->first(function ($item) { + return $item->id == $this->settings->country_id; + }); + + return $country ? ctrans('texts.country_' . $country->name) : ' '; }