Skip to content

Commit b8dcd07

Browse files
committed
feat: remove logic on model trait method and use setting service directly
1 parent fcb244f commit b8dcd07

File tree

1 file changed

+4
-66
lines changed

1 file changed

+4
-66
lines changed

src/Traits/HasSettings.php

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,7 @@ public function settings(): \Illuminate\Database\Eloquent\Relations\MorphMany
3434
*/
3535
public function setSetting(string $key, mixed $value): void
3636
{
37-
$this->settings()->updateOrCreate(
38-
[
39-
config('laravel-settings.key_name') => $key,
40-
],
41-
[
42-
config('laravel-settings.value_name') => $value,
43-
config('laravel-settings.morph_type') => $this->getMorphClass(),
44-
config('laravel-settings.morph_id') => $this->getKey(),
45-
]
46-
);
47-
48-
if (config('laravel-settings.with_cache')) {
49-
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
50-
}
37+
$this->getSettingService()->setWithModel($key, $value, $this->getMorphClass(), $this->getKey());
5138
}
5239

5340
/**
@@ -59,36 +46,7 @@ public function setSetting(string $key, mixed $value): void
5946
*/
6047
public function getSetting(string $key, mixed $default = null): mixed
6148
{
62-
if (config('laravel-settings.with_cache')) {
63-
$cacheKey = Support::getCacheKey($key, $this->getMorphClass(), $this->getKey());
64-
65-
if (Cache::has($cacheKey)) {
66-
return Cache::get($cacheKey);
67-
}
68-
}
69-
70-
$setting = $this->settings()->where(config('laravel-settings.key_name'), $key)->first();
71-
72-
if ($setting) {
73-
if (config('laravel-settings.with_cache')) {
74-
Cache::put($cacheKey, $setting->value, config('laravel-settings.cache_lifetime'));
75-
}
76-
return $setting->value;
77-
}
78-
79-
if (!is_null($default)) {
80-
return $default;
81-
}
82-
83-
if (
84-
config('laravel-settings.model_defaults') &&
85-
array_key_exists($this->getMorphClass(), config('laravel-settings.model_defaults')) &&
86-
array_key_exists($key, config('laravel-settings.model_defaults')[$this->getMorphClass()])
87-
) {
88-
return config('laravel-settings.model_defaults')[$this->getMorphClass()][$key];
89-
}
90-
91-
return $default;
49+
return $this->getSettingService()->getWithModel($key, $this->getMorphClass(), $this->getKey(), $default);
9250
}
9351

9452
/**
@@ -100,17 +58,7 @@ public function getSetting(string $key, mixed $default = null): mixed
10058
*/
10159
public function forgetSetting(string $key): void
10260
{
103-
$this->settings()->where(
104-
[
105-
config('laravel-settings.key_name') => $key,
106-
config('laravel-settings.morph_type') => $this->getMorphClass(),
107-
config('laravel-settings.morph_id') => $this->getKey(),
108-
]
109-
)->delete();
110-
111-
if (config('laravel-settings.with_cache')) {
112-
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
113-
}
61+
$this->getSettingService()->forgetWithModel($key, $this->getMorphClass(), $this->getKey());
11462
}
11563

11664
/**
@@ -121,17 +69,7 @@ public function forgetSetting(string $key): void
12169
*/
12270
public function deleteSetting(string $key): void
12371
{
124-
$this->settings()->where(
125-
[
126-
config('laravel-settings.key_name') => $key,
127-
config('laravel-settings.morph_type') => $this->getMorphClass(),
128-
config('laravel-settings.morph_id') => $this->getKey(),
129-
]
130-
)->delete();
131-
132-
if (config('laravel-settings.with_cache')) {
133-
Cache()->forget(Support::getCacheKey($key, $this->getMorphClass(), $this->getKey()));
134-
}
72+
$this->getSettingService()->deleteWithModel($key, $this->getMorphClass(), $this->getKey());
13573
}
13674

13775
/**

0 commit comments

Comments
 (0)