Test Failed
Push — master ( def136...222fba )
by Antonio Carlos
08:21
created

Cache::forget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 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 75
    public function __construct(CacheManager $cache)
17
    {
18 75
        $this->cache = $cache;
19 75
    }
20
21
    /**
22
     * Cache remember.
23
     *
24
     * @param $model
25
     *
26
     * @return void
27
     */
28 37
    public function remember($model)
29
    {
30 37
        if (($timeout = $this->expireTime()) > 0) {
31 14
            $this->put($model->ip_address, $model, $timeout);
32
        }
33 37
    }
34
35
    /**
36
     * Make a cache key.
37
     *
38
     * @param $key
39
     *
40
     * @return string
41
     */
42 30
    public function key($key)
43
    {
44 30
        return sha1(static::CACHE_BASE_NAME."ip_address.$key");
45
    }
46
47
    /**
48
     * Flush cache.
49
     *
50
     */
51 75
    public function flush()
52
    {
53 75
        $this->cache->flush();
54 75
    }
55
56
    /**
57
     * Check if cache has key.
58
     *
59
     * @param $key
60
     *
61
     * @return bool
62
     */
63 57
    public function has($key)
64
    {
65 57
        if ($this->enabled()) {
66 29
            return $this->cache->has($this->key($key));
67
        }
68
69 29
        return false;
70
    }
71
72
    /**
73
     * Get a value from the cache.
74
     *
75
     * @param $key
76
     *
77
     * @return mixed|null
78
     */
79 15
    public function get($key)
80
    {
81 15
        if ($this->enabled()) {
82 15
            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...
83
        }
84 1
    }
85
86
    /**
87
     * Remove an ip address from cache.
88
     *
89
     * @param $key
90
     *
91
     * @return void
92
     */
93 8
    public function forget($key)
94
    {
95 8
        if ($this->enabled()) {
96 2
            $this->cache->forget($this->key($key));
97
        }
98 8
    }
99
100
    /**
101
     * Store an item in the cache for a given number of minutes.
102
     *
103
     * @param string        $key
104
     * @param mixed         $value
105
     * @param int|null|bool $minutes
106
     *
107
     * @return void
108
     */
109 21
    public function put($key, $value, $minutes = null)
110
    {
111 21
        if ($timeout = $this->enabled()) {
112 21
            $this->cache->put($this->key($key), $value, $minutes ?: $timeout);
113
        }
114 21
    }
115
116
    /**
117
     * Get cache expire time.
118
     *
119
     * @return int|bool
120
     */
121 58
    public function expireTime()
122
    {
123 58
        return $this->config()->get('cache_expire_time');
124
    }
125
126
    /**
127
     * Get enabled state.
128
     *
129
     * @return int|bool
130
     */
131 58
    public function enabled()
132
    {
133 58
        return $this->config()->get('cache_expire_time') !== false &&
134 58
            $this->expireTime() > 0;
135
    }
136
}
137