Passed
Push — master ( 08e0de...df0894 )
by Gombos
03:08
created

HasSettingsTable::settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 42
    public function settings(): SettingsManagerContract
26
    {
27 42
        return new TableSettingsManager($this);
28
    }
29
30
    /**
31
     * @return array
32
     * @throws \Exception
33
     */
34 42
    public function getSettingsValue(): array
35
    {
36 42
        if (config('model_settings.settings_table_use_cache')) {
37
            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
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( 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...
65
    }
66
67
    abstract public function getTable();
68
}
69