Store::__set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Bmatovu\Ussd;
4
5
use Illuminate\Contracts\Cache\Repository as CacheContract;
6
use Illuminate\Support\Facades\Cache;
7
8
class Store
9
{
10
    protected CacheContract $cache;
11
    protected int $ttl;
12
    protected string $prefix;
13
14 49
    public function __construct(string $driver, int $ttl, string $prefix)
15
    {
16 49
        $this->cache = Cache::store($driver);
17 49
        $this->ttl = $ttl;
18 49
        $this->prefix = $prefix;
19
    }
20
21 1
    public function __get(string $key)
22
    {
23 1
        if (property_exists($this, $key)) {
24 1
            return $this->{$key};
25
        }
26
27 1
        return $this->cache->get("{$this->prefix}{$key}");
28
    }
29
30 1
    public function __set(string $key, $value)
31
    {
32 1
        if (property_exists($this, $key)) {
33 1
            $this->{$key} = $value;
34
        }
35
36 1
        $this->cache->put("{$this->prefix}{$key}", $value, $this->ttl);
37
    }
38
39 37
    public function get(string $key, $default = null)
40
    {
41 37
        return $this->cache->get("{$this->prefix}{$key}", $default);
42
    }
43
44 2
    public function pull(string $key)
45
    {
46 2
        return $this->cache->pull("{$this->prefix}{$key}");
47
    }
48
49 35
    public function put(string $key, $value): void
50
    {
51 35
        $this->cache->put("{$this->prefix}{$key}", $value, $this->ttl);
52
    }
53
54 2
    public function append(string $key, string $extra): void
55
    {
56 2
        $value = $this->cache->get("{$this->prefix}{$key}");
57
58 2
        $this->cache->put("{$this->prefix}{$key}", "{$value}{$extra}", $this->ttl);
59
    }
60
61 49
    public function flush()
62
    {
63 49
        $this->cache->flush();
64
    }
65
}
66