Completed
Pull Request — master (#15)
by Mathieu
16:37 queued 14:28
created

Redis::getHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate\Cache;
6
7
use Suricate;
8
use Predis\Client;
0 ignored issues
show
Bug introduced by
The type Predis\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use \Exception;
10
use \BadMethodCallException;
11
12
/**
13
 * Redis extension for Suricate
14
 *
15
 * @package Suricate
16
 * @author  Mathieu LESNIAK <[email protected]>
17
 *
18
 * @property string $host           redis host (default: localhost)
19
 * @property int    $port           redis port (default: 6379)
20
 * @property int    $defaultExpiry  Key default expiry
21
 */
22
23
class Redis extends Suricate\Cache
24
{
25
    protected $parametersList = ['host', 'port', 'defaultExpiry'];
26
27
    private $handler;
28
29 4
    public function __construct()
30
    {
31 4
        parent::__construct();
32
33 4
        $this->handler = false;
34 4
        $this->host = 'localhost';
35 4
        $this->port = 6379;
36 4
        $this->defaultExpiry = 3600;
37 4
    }
38
39
    /**
40
     * Get redis host
41
     *
42
     * @return string
43
     */
44 1
    public function getHost(): string
45
    {
46 1
        return $this->host;
47
    }
48
49
    /**
50
     * Set Redis host
51
     *
52
     * @param string $host redis hostname/ip
53
     *
54
     * @return Redis
55
     */
56 1
    public function setHost(string $host): Redis
57
    {
58 1
        $this->host = $host;
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Get Redis port
65
     *
66
     * @return integer
67
     */
68 1
    public function getPort(): int
69
    {
70 1
        return $this->port;
71
    }
72
73
    /**
74
     * Set Redis port
75
     *
76
     * @param int $port Redis port
77
     *
78
     * @return Redis
79
     */
80 1
    public function setPort(int $port): Redis
81
    {
82 1
        $this->port = intval($port);
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * Get default cache expiration duration
89
     *
90
     * @return integer
91
     */
92 1
    public function getDefaultExpiry(): int
93
    {
94 1
        return $this->defaultExpiry;
95
    }
96
97
    /**
98
     * Set default cache expiration duration
99
     *
100
     * @param integer $expiry
101
     *
102
     * @return Redis
103
     */
104 1
    public function setDefaultExpiry(int $expiry): Redis
105
    {
106 1
        $this->defaultExpiry = $expiry;
107
108 1
        return $this;
109
    }
110
111
    /**
112
     * Connect to Redis host
113
     *
114
     * @throws Exception
115
     * @throws BadMethodCallException
116
     *
117
     * @return Redis
118
     */
119
    private function connect(): Redis
120
    {
121
        if ($this->handler === false) {
122
            if (class_exists('\Predis\Client')) {
123
                $this->handler = new Client([
124
                    'scheme' => 'tcp',
125
                    'host' => $this->host,
126
                    'port' => $this->port
127
                ]);
128
                if ($this->handler->connect() === false) {
129
                    throw new Exception('Can\'t connect to redis server');
130
                }
131
132
                return $this;
133
            }
134
135
            throw new BadMethodCallException('Can\'t find Redis extension');
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Put a value into redis
143
     * @param string $keyname  Key name
144
     * @param mixed  $value    Value
145
     * @param int    $expiry   Cache expiry
146
     * @throws Exception
147
     * @throws BadMethodCallException
148
     *
149
     * @return bool
150
     */
151
    public function set(string $keyname, $value, $expiry = null)
152
    {
153
        $this->connect();
154
155
        if ($expiry === null) {
156
            $expiry = $this->defaultExpiry;
157
        }
158
159
        $retVal = $this->handler->set($keyname, $value);
160
        $expireRetVal = true;
161
        if ($expiry !== -1) {
162
            $expireRetVal = $this->handler->expire($keyname, $expiry);
163
        }
164
165
        return $retVal && $expireRetVal;
166
    }
167
168
    /**
169
     * Get a cached value from keyname
170
     *
171
     * @param string $keyname
172
     * @throws Exception
173
     * @throws BadMethodCallException
174
     *
175
     * @return string
176
     */
177
    public function get(string $keyname): string
178
    {
179
        $this->connect();
180
        return $this->handler->get($keyname);
181
    }
182
183
    /**
184
     * Delete a variable from redis
185
     *
186
     * @param string $keyname
187
     * @throws Exception
188
     * @throws BadMethodCallException
189
     *
190
     * @return bool
191
     */
192
    public function delete(string $keyname): bool
193
    {
194
        $this->connect();
195
        return $this->handler->delete($keyname);
196
    }
197
}
198