RedisConfiguration::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 2
eloc 10
c 4
b 1
f 0
nc 2
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 2
rs 9.9332
1
<?php
2
/*
3
 * This file is part of the Koded package.
4
 *
5
 * (c) Mihail Binev <[email protected]>
6
 *
7
 * Please view the LICENSE distributed with this source code
8
 * for the full copyright and license information.
9
 */
10
11
namespace Koded\Caching\Configuration;
12
13
use Koded\Caching\CacheException;
14
use Koded\Stdlib\Serializer;
15
use Redis;
16
use function class_exists;
17
18
final class RedisConfiguration extends CacheConfiguration
19
{
20
    private int $type;
21
22 155
    public function __construct(array $values)
23
    {
24
        // @codeCoverageIgnoreStart
25
        if (false === class_exists('\Redis', false)) {
26
            throw CacheException::generic('Redis extension is not loaded on this machine.');
27
        }
28
        // @codeCoverageIgnoreEnd
29
30 155
        $values += [
31 155
            'serializer' => Serializer::PHP,
32 155
            'binary' => Serializer::PHP,
33 155
        ];
34
35 155
        parent::__construct($values);
36
37 155
        $this->type = match ($values['serializer']) {
38 155
            Serializer::PHP => Redis::SERIALIZER_PHP,
39 155
            Serializer::IGBINARY => Redis::SERIALIZER_IGBINARY,
40 155
            default => Redis::SERIALIZER_NONE,
41 155
        };
42
    }
43
44
    /**
45
     * Returns connection parameters for Redis client.
46
     *
47
     * string $host           can be a host, or the path to a unix domain socket
48
     * int    $port           optional, default 6379
49
     * float  $timeout        value in seconds (optional, default is 0.0 meaning unlimited)
50
     * null   $reserved       should be null if $retry_interval is specified
51
     * int    $retry          retry interval in milliseconds.
52
     * float  $readTimeout    value in seconds (optional, default is 0 meaning unlimited)
53
     *
54
     * @return array{host:string, port:int, timeout:float, reserved:null, retry:int, readTimeout:float}
55
     */
56 155
    public function getConnectionParams(): array
57
    {
58 155
        return [
59 155
            $this->get('host', '127.0.0.1'),
60 155
            $this->get('port', 6379),
61 155
            $this->get('timeout', 0.0),
62 155
            $this->get('reserved', null),
63 155
            $this->get('retry', 0),
64 155
            $this->get('readTimeout', 0.0),
65 155
        ];
66
    }
67
}
68