1 | <?php |
||||
2 | |||||
3 | namespace Glorand\Model\Settings\Traits; |
||||
4 | |||||
5 | use Glorand\Model\Settings\Contracts\SettingsManagerContract; |
||||
6 | use Glorand\Model\Settings\Managers\TableSettingsManager; |
||||
7 | use Glorand\Model\Settings\Models\ModelSettings; |
||||
8 | use Illuminate\Database\Eloquent\Relations\MorphOne; |
||||
9 | use Illuminate\Support\Facades\Cache; |
||||
10 | |||||
11 | /** |
||||
12 | * Trait HasSettingsTable |
||||
13 | * @package Glorand\Model\Settings\Traits |
||||
14 | * @property ModelSettings $modelSettings |
||||
15 | * @property array $settings |
||||
16 | * @method morphOne($model, $name) |
||||
17 | */ |
||||
18 | trait HasSettingsTable |
||||
19 | { |
||||
20 | 1 | use HasSettings; |
|||
21 | |||||
22 | /** |
||||
23 | * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract |
||||
24 | * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException |
||||
25 | */ |
||||
26 | 42 | public function settings(): SettingsManagerContract |
|||
27 | { |
||||
28 | 42 | return new TableSettingsManager($this); |
|||
29 | } |
||||
30 | |||||
31 | /** |
||||
32 | * @return array |
||||
33 | */ |
||||
34 | 42 | public function getSettingsValue(): array |
|||
35 | { |
||||
36 | 42 | if (config('model_settings.settings_table_use_cache')) { |
|||
37 | 28 | return Cache::rememberForever($this->getSettingsCacheKey(), function () { |
|||
38 | 42 | return $this->__getSettingsValue(); |
|||
39 | 42 | }); |
|||
40 | } |
||||
41 | |||||
42 | 3 | return $this->__getSettingsValue(); |
|||
43 | } |
||||
44 | |||||
45 | 42 | private function __getSettingsValue(): array |
|||
46 | { |
||||
47 | 42 | if ($modelSettings = $this->modelSettings()->first()) { |
|||
48 | 39 | return $modelSettings->settings; |
|||
49 | } |
||||
50 | |||||
51 | 33 | return []; |
|||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * @return \Illuminate\Database\Eloquent\Relations\MorphOne |
||||
56 | */ |
||||
57 | 42 | public function modelSettings(): MorphOne |
|||
58 | { |
||||
59 | 42 | return $this->morphOne(ModelSettings::class, 'model'); |
|||
60 | } |
||||
61 | |||||
62 | 42 | public function getSettingsCacheKey(): string |
|||
63 | { |
||||
64 | 42 | return config('model_settings.settings_table_cache_prefix') . $this->getTable() . '::' . $this->getKey(); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() Are you sure
$this->getKey() of type Glorand\Model\Settings\M...tingsManager|mixed|null can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
65 | } |
||||
66 | |||||
67 | abstract public function getTable(); |
||||
68 | } |
||||
69 |