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