Passed
Push — master ( 0a106d...6f5789 )
by Gombos
08:09 queued 03:26
created

HasSettingsRedis::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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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\Arr;
8
use Illuminate\Support\Facades\Redis;
9
10
/**
11
 * Trait HasSettingsRedis
12
 * @package Glorand\Model\Settings\Traits
13
 * @property array $settings
14
 */
15
trait HasSettingsRedis
16
{
17
    use HasSettings;
18
19
    /**
20
     * @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
21
     * @throws \Glorand\Model\Settings\Exceptions\ModelSettingsException
22
     */
23 36
    public function settings(): SettingsManagerContract
24
    {
25 36
        return new RedisSettingsManager($this);
1 ignored issue
show
Bug introduced by
$this of type Glorand\Model\Settings\Traits\HasSettingsRedis is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Glorand\Model\Settings\M...sManager::__construct(). ( Ignorable by Annotation )

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

25
        return new RedisSettingsManager(/** @scrutinizer ignore-type */ $this);
Loading history...
26
    }
27
28 36
    public function getSettingsValue(): array
29
    {
30 36
        $redisValue = Redis::get($this->cacheKey());
31
32 36
        return Arr::wrap(json_decode($redisValue, true));
33
    }
34
35 36
    public function cacheKey(string $key = null): string
36
    {
37 36
        return sprintf(
38 36
                "r-k-%s:%s:%s",
39 36
                $this->getTable(),
40 36
                $this->getKey(),
41 36
                $this->updated_at->timestamp
42 36
            ) . $key;
43
    }
44
45
    abstract public function getTable();
46
47
    abstract public function getKey();
48
}
49