Completed
Push — master ( 2bd6c2...9d13eb )
by Antonio Carlos
08:09 queued 01:00
created

Cache::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace PragmaRX\Firewall\Repositories\Cache;
4
5
use Illuminate\Cache\CacheManager;
6
use PragmaRX\Support\Config;
7
8
class Cache implements CacheInterface
9
{
10
    const CACHE_BASE_NAME = 'firewall.';
11
12
    private $memory = [];
0 ignored issues
show
Unused Code introduced by
The property $memory is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
14
    private $cache;
15
16
    private $config;
17
18
    public function __construct(Config $config, CacheManager $cache)
19
    {
20
        $this->config = $config;
21
22
        $this->cache = $cache;
23
    }
24
25
    /**
26
     * Increment is not supported.
27
     */
28
    public function increment($key, $value = 1)
29
    {
30
        throw new \Exception('Increment operations not supported by this driver.');
31
    }
32
33
    /**
34
     * Decrement is not supported.
35
     */
36
    public function decrement($key, $value = 1)
37
    {
38
        throw new \Exception('Decrement operations not supported by this driver.');
39
    }
40
41
    /**
42
     * Insert or replace a value for a key and remember is forever.
43
     *
44
     * @param string $key
45
     * @param mixed  $value
46
     *
47
     * @return void
48
     */
49
    public function forever($key, $value)
50
    {
51
        if ($this->expireTime()) {
52
            $this->put($this->key($key), $value);
53
        }
54
    }
55
56
    /**
57
     * Cache remember.
58
     *
59
     * @param $model
60
     *
61
     * @return void
62
     */
63
    public function remember($model)
64
    {
65
        if ($timeout = $this->expireTime()) {
66
            $this->put($this->key($model->ip_address), $model, $timeout);
67
        }
68
    }
69
70
    /**
71
     * Make a cache key.
72
     *
73
     * @param $ip
74
     *
75
     * @return string
76
     */
77
    public function key($key)
78
    {
79
        return sha1(static::CACHE_BASE_NAME."ip_address.$key");
80
    }
81
82
    /**
83
     * Check if cache has key.
84
     *
85
     * @param $ip
86
     *
87
     * @return bool
88
     */
89
    public function has($key)
90
    {
91
        if ($this->expireTime()) {
92
            return $this->cache->has($this->key($key));
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Get a value from the cache.
100
     *
101
     * @param $ip
102
     *
103
     * @return mixed
104
     */
105
    public function get($key)
106
    {
107
        if ($this->expireTime()) {
108
            return $this->cache->get($this->key($key));
109
        }
110
    }
111
112
    /**
113
     * Remove an ip address from cache.
114
     *
115
     * @param string $ip
116
     *
117
     * @return void
118
     */
119
    public function forget($key)
120
    {
121
        if ($this->expireTime()) {
122
            $this->cache->forget($this->key($key));
123
        }
124
    }
125
126
    /**
127
     * Erase the whole cache.
128
     *
129
     * @return void
130
     */
131
    public function flush()
132
    {
133
        $this->cache->flush();
134
    }
135
136
    /**
137
     * Store an item in the cache for a given number of minutes.
138
     *
139
     * @param string        $key
140
     * @param mixed         $value
141
     * @param int|null|bool $minutes
142
     *
143
     * @return void
144
     */
145
    public function put($key, $value, $minutes = null)
146
    {
147
        if ($timeout = $this->expireTime()) {
148
            $this->cache->put($this->key($key), $value, $minutes ?: $timeout);
149
        }
150
    }
151
152
    /**
153
     * Get cache expire time.
154
     *
155
     * @return int|bool
156
     */
157
    public function expireTime()
158
    {
159
        return $this->config->get('cache_expire_time');
160
    }
161
}
162