Test Failed
Pull Request — master (#173)
by
unknown
15:48 queued 13:33
created

Cache   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 129
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A remember() 0 6 2
A key() 0 4 1
A flush() 0 4 1
A has() 0 8 2
A get() 0 6 2
A forget() 0 6 2
A put() 0 6 3
A expireTime() 0 4 1
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
    public function __construct(CacheManager $cache)
17
    {
18
        $this->cache = $cache;
19
    }
20
21
    /**
22
     * Cache remember.
23
     *
24
     * @param $model
25
     *
26
     * @return void
27
     */
28
    public function remember($model)
29
    {
30
        if (($timeout = $this->expireTime()) > 0) {
31
            $this->put($model->ip_address, $model, $timeout);
32
        }
33
    }
34
35
    /**
36
     * Make a cache key.
37
     *
38
     * @param $key
39
     *
40
     * @return string
41
     */
42
    public function key($key)
43
    {
44
        return sha1(static::CACHE_BASE_NAME."ip_address.$key");
45
    }
46
47
    /**
48
     * Flush cache.
49
     */
50
    public function flush()
51
    {
52
        $this->cache->flush();
53
    }
54
55
    /**
56
     * Check if cache has key.
57
     *
58
     * @param $key
59
     *
60
     * @return bool
61
     */
62
    public function has($key)
63
    {
64
        if ($this->enabled()) {
65
            return $this->cache->has($this->key($key));
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * Get a value from the cache.
73
     *
74
     * @param $key
75
     * @param null $default
76
     *
77
     * @return mixed|null
78
     */
79
    public function get($key, $default = null)
80
    {
81
        if ($this->enabled()) {
82
            return $this->cache->get($this->key($key), $default);
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...
Unused Code introduced by
The call to CacheManager::get() has too many arguments starting with $default.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
83
        }
84
    }
85
86
    /**
87
     * Remove an ip address from cache.
88
     *
89
     * @param $key
90
     *
91
     * @return void
92
     */
93
    public function forget($key)
94
    {
95
        if ($this->enabled()) {
96
            $this->cache->forget($this->key($key));
0 ignored issues
show
Bug introduced by
The method forget() does not exist on Illuminate\Cache\CacheManager. Did you maybe mean forgetDriver()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
        }
98
    }
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
    public function put($key, $value, $minutes = null)
110
    {
111
        if ($timeout = $this->enabled()) {
112
            $this->cache->put($this->key($key), $value, $minutes ?: $timeout);
113
        }
114
    }
115
116
    /**
117
     * Get cache expire time.
118
     *
119
     * @return int|bool
120
     */
121
    public function expireTime()
122
    {
123
        return $this->config()->get('cache_expire_time');
124
    }
125
126
    /**
127
     * Get enabled state.
128
     *
129
     * @return int|bool
130
     */
131
    public function enabled()
132
    {
133
        return $this->config()->get('cache_expire_time') !== false &&
134
            $this->expireTime() > 0;
135
    }
136
}
137