Passed
Push — master ( b54e8f...ab9cd5 )
by Gombos
03:13
created

HasSettingsTable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 48
ccs 14
cts 15
cp 0.9333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A settings() 0 3 1
A getSettingsValue() 0 9 2
A __getSettingsValue() 0 6 2
A getSettingsCacheKey() 0 3 1
A modelSettings() 0 3 1
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
10
/**
11
 * Trait HasSettingsTable
12
 * @package Glorand\Model\Settings\Traits
13
 * @property ModelSettings $modelSettings
14
 * @property array $settings
15
 * @method morphOne($model, $name)
16
 */
17
trait HasSettingsTable
18
{
19
    use HasSettings;
20
21
    /**
22
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
23
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
24
     */
25 36
    public function settings(): SettingsManagerContract
26
    {
27 36
        return new TableSettingsManager($this);
28
    }
29
30
    /**
31
     * @return array
32
     * @throws \Exception
33
     */
34 36
    public function getSettingsValue(): array
35
    {
36 36
        if (config('model_settings.settings_table_use_cache')) {
37
            return cache()->rememberForever($this->getSettingsCacheKey(), function () {
38 36
                return $this->__getSettingsValue();
39 36
            });
40
        }
41
42
        return $this->__getSettingsValue();
43
    }
44
45 36
    private function __getSettingsValue(): array
46
    {
47 36
        if ($modelSettings = $this->modelSettings()->first()) {
48 33
            return $modelSettings->settings;
49
        } else {
50 21
            return [];
51
        }
52
    }
53
54
    /**
55
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
56
     */
57 36
    public function modelSettings(): MorphOne
58
    {
59 36
        return $this->morphOne(ModelSettings::class, 'model');
60
    }
61
62 36
    public function getSettingsCacheKey(): string
63
    {
64 36
        return config('model_settings.settings_table_cache_prefix') . $this->id;
65
    }
66
}
67