Passed
Push — master ( a1e4e6...73ed56 )
by Brian
02:43
created

Store::get()   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 2
crap 1
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 35
    public function __construct(string $driver, int $ttl, string $prefix)
15
    {
16 35
        $this->cache = Cache::store($driver);
17 35
        $this->ttl = $ttl;
18 35
        $this->prefix = $prefix;
19
    }
20
21
    public function __get(string $key)
22
    {
23
        if (property_exists($this, $key)) {
24
            return $this->{$key};
25
        }
26
27
        return $this->cache->get("{$this->prefix}{$key}");
28
    }
29
30
    public function __set(string $key, $value)
31
    {
32
        if (property_exists($this, $key)) {
33
            $this->{$key} = $value;
34
        }
35
36
        $this->cache->put("{$this->prefix}{$key}", $value, $this->ttl);
37
    }
38
39 30
    public function get(string $key, $default = null)
40
    {
41 30
        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 29
    public function put(string $key, $value): void
50
    {
51
        // dd(['key' => "{$this->prefix}{$key}", 'value' => $value]);
52 29
        $this->cache->put("{$this->prefix}{$key}", $value, $this->ttl);
53
    }
54
55 5
    public function append(string $key, string $extra): void
56
    {
57 5
        $value = $this->cache->get("{$this->prefix}{$key}");
58
59 5
        $this->cache->put("{$this->prefix}{$key}", "{$value}{$extra}", $this->ttl);
60
    }
61
62 35
    public function flush()
63
    {
64 35
        $this->cache->flush();
65
    }
66
}
67