Test Failed
Push — master ( 4e8d3d...451ac1 )
by Antonio Carlos
03:57 queued 48s
created

Cache::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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