Passed
Push — master ( 5dc97c...6d4d86 )
by Brian
02:30
created

Store   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

8 Methods

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