Passed
Push — master ( 3d0d87...075b69 )
by Antonio Carlos
09:13
created

Cache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 120
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A remember() 0 6 2
A key() 0 4 1
A forget() 0 6 2
A put() 0 6 3
A expireTime() 0 4 1
A has() 0 8 2
A get() 0 6 2
A enabled() 0 5 2
1
<?php
2
3
namespace PragmaRX\Firewall\Repositories\Cache;
4
5
use Illuminate\Cache\CacheManager;
6
use PragmaRX\Firewall\Support\ServiceInstances;
7
8
class Cache
9
{
10
    use ServiceInstances;
11
12
    const CACHE_BASE_NAME = 'firewall.';
13
14
    private $cache;
15
16 49
    public function __construct(CacheManager $cache)
17
    {
18 49
        $this->cache = $cache;
19 49
    }
20
21
    /**
22
     * Cache remember.
23
     *
24
     * @param $model
25
     *
26
     * @return void
27
     */
28 33
    public function remember($model)
29
    {
30 33
        if (($timeout = $this->expireTime()) > 0) {
31 33
            $this->put($model->ip_address, $model, $timeout);
32
        }
33 33
    }
34
35
    /**
36
     * Make a cache key.
37
     *
38
     * @param $key
39
     *
40
     * @return string
41
     */
42 49
    public function key($key)
43
    {
44 49
        return sha1(static::CACHE_BASE_NAME."ip_address.$key");
45
    }
46
47
    /**
48
     * Check if cache has key.
49
     *
50
     * @param $key
51
     *
52
     * @return bool
53
     */
54 48
    public function has($key)
55
    {
56 48
        if ($this->enabled()) {
57 48
            return $this->cache->has($this->key($key));
58
        }
59
60 1
        return false;
61
    }
62
63
    /**
64
     * Get a value from the cache.
65
     *
66
     * @param $key
67
     *
68
     * @return mixed|null
69
     */
70 23
    public function get($key)
71
    {
72 23
        if ($this->enabled()) {
73 23
            return $this->cache->get($this->key($key));
0 ignored issues
show
Bug introduced by
The method get() cannot be called from this context as it is declared protected in class Illuminate\Cache\CacheManager.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
74
        }
75 1
    }
76
77
    /**
78
     * Remove an ip address from cache.
79
     *
80
     * @param $key
81
     *
82
     * @return void
83
     */
84 7
    public function forget($key)
85
    {
86 7
        if ($this->enabled()) {
87 7
            $this->cache->forget($this->key($key));
88
        }
89 7
    }
90
91
    /**
92
     * Store an item in the cache for a given number of minutes.
93
     *
94
     * @param string        $key
95
     * @param mixed         $value
96
     * @param int|null|bool $minutes
97
     *
98
     * @return void
99
     */
100 36
    public function put($key, $value, $minutes = null)
101
    {
102 36
        if ($timeout = $this->enabled()) {
103 36
            $this->cache->put($this->key($key), $value, $minutes ?: $timeout);
104
        }
105 36
    }
106
107
    /**
108
     * Get cache expire time.
109
     *
110
     * @return int|bool
111
     */
112 49
    public function expireTime()
113
    {
114 49
        return $this->config()->get('cache_expire_time');
115
    }
116
117
    /**
118
     * Get enabled state.
119
     *
120
     * @return int|bool
121
     */
122 49
    public function enabled()
123
    {
124 49
        return $this->config()->get('cache_expire_time') !== false &&
125 49
            $this->expireTime() > 0;
126
    }
127
}
128