Passed
Push — master ( f519e1...ef65fe )
by Brian
02:51
created

Store   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 43.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 19
c 1
b 0
f 0
dl 0
loc 57
ccs 10
cts 23
cp 0.4348
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A put() 0 4 1
A append() 0 5 1
A __set() 0 7 2
A __construct() 0 5 1
A __get() 0 7 2
A flush() 0 3 1
A get() 0 3 1
A pull() 0 3 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 28
    public function __construct(string $driver, int $ttl, string $prefix)
15
    {
16 28
        $this->cache = Cache::store($driver);
17 28
        $this->ttl = $ttl;
18 28
        $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 24
    public function get(string $key, $default = null)
40
    {
41 24
        return $this->cache->get("{$this->prefix}{$key}", $default);
42
    }
43
44
    public function pull(string $key)
45
    {
46
        return $this->cache->pull("{$this->prefix}{$key}");
47
    }
48
49 22
    public function put(string $key, $value): void
50
    {
51
        // dd(['key' => "{$this->prefix}{$key}", 'value' => $value]);
52 22
        $this->cache->put("{$this->prefix}{$key}", $value, $this->ttl);
53
    }
54
55
    public function append(string $key, string $extra): void
56
    {
57
        $value = $this->cache->get("{$this->prefix}{$key}");
58
59
        $this->cache->put("{$this->prefix}{$key}", "{$value}{$extra}", $this->ttl);
60
    }
61
62 28
    public function flush()
63
    {
64 28
        $this->cache->flush();
65
    }
66
}
67