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

HasSettingsTable::getSettingsValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 10
cc 2
nc 2
nop 0
crap 2.032
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