HasSettingsTable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 12
c 3
b 0
f 0
dl 0
loc 50
ccs 17
cts 17
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A modelSettings() 0 3 1
A __getSettingsValue() 0 7 2
A getSettingsCacheKey() 0 3 1
A settings() 0 3 1
A getSettingsValue() 0 9 2
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
The method getKey() does not exist on Glorand\Model\Settings\Traits\HasSettingsTable. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        return config('model_settings.settings_table_cache_prefix') . $this->getTable() . '::' . $this->/** @scrutinizer ignore-call */ getKey();
Loading history...
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 ignore-type  annotation

64
        return config('model_settings.settings_table_cache_prefix') . $this->getTable() . '::' . /** @scrutinizer ignore-type */ $this->getKey();
Loading history...
65
    }
66
67
    abstract public function getTable();
68
}
69