HasSettingsRedis   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 33
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cacheKey() 0 7 1
A getSettingsValue() 0 6 2
A settings() 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\RedisSettingsManager;
7
use Illuminate\Support\Facades\Redis;
8
9
/**
10
 * Trait HasSettingsRedis
11
 * @package Glorand\Model\Settings\Traits
12
 * @property array $settings
13
 */
14
trait HasSettingsRedis
15
{
16 1
    use HasSettings;
17
18
    /**
19
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
20
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
21
     */
22 42
    public function settings(): SettingsManagerContract
23
    {
24 42
        return new RedisSettingsManager($this);
25
    }
26
27 42
    public function getSettingsValue(): array
28
    {
29 42
        $redisValue = Redis::connection()->get($this->cacheKey());
30 42
        $value = json_decode($redisValue, true);
31
32 42
        return is_array($value) ? $value : [];
33
    }
34
35 42
    public function cacheKey(string $key = null): string
36
    {
37 42
        return sprintf(
38 42
            "r-k-%s:%s",
39 42
            $this->getTable(),
40 42
            $this->getKey()
41 42
        ) . $key;
42
    }
43
44
    abstract public function getTable();
45
46
    abstract public function getKey();
47
}
48