Passed
Push — master ( 87bd55...eebb79 )
by M
02:12
created

Clients::_getSerializerArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 13
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Kazuakim\Reddish;
4
5
/**
6
 * Clients.
7
 *
8
 * @copyright KazuakiM <[email protected]>
9
 * @author    KazuakiM <[email protected]>
10
 * @license   http://www.opensource.org/licenses/mit-license.php  MIT License
11
 *
12
 * @link      https://github.com/KazuakiM/reddish
13
 */
14
class Clients extends \Redis //{{{
15
{
16
    protected $config;
17
    private static $_defaultConfig = [
18
        'host' => null,           //can be a host, or the path to a unix domain socket
19
        'port' => 6379,
20
        'timeout' => 0.0,         //value in seconds (optional, default is 0 meaning unlimited)
21
        'reserved' => null,       //should be NULL if retry_interval is specified
22
        'retry_interval' => null, //value in milliseconds
23
        'persistent_id' => '',    //identity for the requested persistent connection
24
        'password' => null,
25
        'serializer' => \Redis::SERIALIZER_NONE,
26
        'persistent' => false,    //default is connect
27
    ];
28
29 5
    public function __construct(array $config) //{{{
30
    {
31 5
        $this->config = array_merge(self::$_defaultConfig, $config);
32
33 5
        $this->connection();
34 3
    } //}}}
35
36 5
    public function connection() //{{{
37
    {
38
        // connect
39 5
        if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['reserved'], $this->config['retry_interval'])) {
0 ignored issues
show
Unused Code introduced by
The call to Redis::connect() has too many arguments starting with $this->config['reserved']. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], /** @scrutinizer ignore-call */ $this->config['reserved'], $this->config['retry_interval'])) {

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. Please note the @ignore annotation hint above.

Loading history...
40 1
            throw new ReddishException('connect errored.');
41 4
        } elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['persistent_id'], $this->config['retry_interval'])) {
0 ignored issues
show
Unused Code introduced by
The call to Redis::pconnect() has too many arguments starting with $this->config['persistent_id']. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        } elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], /** @scrutinizer ignore-call */ $this->config['persistent_id'], $this->config['retry_interval'])) {

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. Please note the @ignore annotation hint above.

Loading history...
42 1
            throw new ReddishException('pconnect errored.');
43
        }
44
45
        // auth
46 3
        if (0 < strlen($this->config['password']) && !$this->auth($this->config['password'])) {
47
            throw new ReddishException('auth errored.');
48
        }
49
50
        // serializer
51 3
        $this->setSerializer($this->config['serializer']);
52 3
    } //}}}
53
54 3
    public function setSerializer($serializer) //{{{
55
    {
56 3
        assert(in_array($serializer, self::_getSerializerArray(), true), 'serializer setting errored.');
57
58 3
        if (!$this->setOption(\Redis::OPT_SERIALIZER, $serializer)) {
59
            throw new ReddishException('serializer errored.');
60
        }
61 3
    } //}}}
62
63 2
    public function close() //{{{
64
    {
65 2
        if ($this->config['persistent']) {
66 2
            parent::close();
67
        }
68 2
    } //}}}
69
70 2
    public function ping() //{{{
71
    {
72
        try {
73 2
            parent::ping();
74 1
        } catch (\RedisException $e) {
75 1
            throw new ReddishException($e->getMessage());
76
        }
77 1
    } //}}}
78
79 3
    private static function _getSerializerArray(): array //{{{
80
    {
81 3
        if (extension_loaded('igbinary') === false) {
82
            return [
83 3
                \Redis::SERIALIZER_NONE,
84
                \Redis::SERIALIZER_PHP,
85
            ];
86
        }
87
88
        return [
89
            \Redis::SERIALIZER_NONE,
90
            \Redis::SERIALIZER_PHP,
91
            \Redis::SERIALIZER_IGBINARY,
92
        ];
93
    } //}}}
94
} //}}}
95