Skip to content

Commit 0a3b02b

Browse files
committed
Cache settings composer
1 parent ee92975 commit 0a3b02b

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

src/View/Composers/SettingsComposer.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,53 @@
22

33
namespace VentureDrake\LaravelCrm\View\Composers;
44

5+
use Illuminate\Support\Facades\Cache;
56
use Illuminate\Support\Facades\Schema;
67
use Illuminate\View\View;
78
use VentureDrake\LaravelCrm\Models\Setting;
89

910
class SettingsComposer
1011
{
12+
public static ?array $cachedParameters = null;
13+
1114
public function compose(View $view)
1215
{
13-
if (Schema::hasTable(config('laravel-crm.db_table_prefix').'settings')) {
14-
$view->with('dateFormat', Setting::where('name', 'date_format')->first()->value ?? 'Y/m/d');
15-
$view->with('timeFormat', Setting::where('name', 'time_format')->first()->value ?? 'H:i');
16-
$view->with('timezone', Setting::where('name', 'timezone')->first()->value ?? 'UTC');
17-
$view->with('taxName', Setting::where('name', 'tax_name')->first()->value ?? 'Tax');
18-
19-
if($setting = Setting::where('name', 'dynamic_products')->first()) {
20-
if($setting->value == 1) {
21-
$view->with('dynamicProducts', 'true');
16+
static::$cachedParameters ??= Cache::remember(
17+
self::class,
18+
now()->addHour(),
19+
function () {
20+
$defaults = [
21+
'dateFormat' => 'Y/m/d',
22+
'timeFormat' => 'H:i',
23+
'timezone' => 'UTC',
24+
'taxName' => 'Tax',
25+
'dynamicProducts' => 'true',
26+
];
27+
28+
if (! Schema::hasTable(config('laravel-crm.db_table_prefix').'settings')) {
29+
return $defaults;
30+
}
31+
32+
if ($dynamicProductsSetting = Setting::where('name', 'dynamic_products')->first()) {
33+
if ($dynamicProductsSetting->value == 1) {
34+
$dynamicProducts = 'true';
35+
} else {
36+
$dynamicProducts = 'false';
37+
}
2238
} else {
23-
$view->with('dynamicProducts', 'false');
39+
$dynamicProducts = $defaults['dynamicProducts'];
2440
}
25-
} else {
26-
$view->with('dynamicProducts', 'true');
41+
42+
return [
43+
'dateFormat' => Setting::where('name', 'date_format')->first()?->value ?? $defaults['dateFormat'],
44+
'timeFormat' => Setting::where('name', 'time_format')->first()?->value ?? $defaults['timeFormat'],
45+
'timezone' => Setting::where('name', 'timezone')->first()?->value ?? $defaults['timezone'],
46+
'taxName' => Setting::where('name', 'tax_name')->first()?->value ?? $defaults['taxName'],
47+
'dynamicProducts' => $dynamicProducts,
48+
];
2749
}
28-
} else {
29-
$view->with('dateFormat', 'Y/m/d');
30-
$view->with('timeFormat', 'H:i');
31-
$view->with('timezone', 'UTC');
32-
$view->with('taxName', 'Tax');
33-
}
50+
);
51+
52+
$view->with(static::$cachedParameters);
3453
}
3554
}

0 commit comments

Comments
 (0)