Completed
Pull Request — master (#3)
by M
02:18
created

Clients::connection()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 1
nop 0
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
crap 6.0493
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Kazuakim\Reddish;
4
5
/**
6
 * Clients.
7
 *
8
 * @property array $config
9
 * @property array $_defaultConfig
10
 *
11
 * @copyright KazuakiM <[email protected]>
12
 * @author    KazuakiM <[email protected]>
13
 * @license   http://www.opensource.org/licenses/mit-license.php  MIT License
14
 *
15
 * @link      https://github.com/KazuakiM/reddish
16
 */
17
class Clients extends \Redis //{{{
18
{
19
    protected $config;
20
    private $_defaultConfig = [
21
        'host' => null,        //can be a host, or the path to a unix domain socket
22
        'port' => 6379,
23
        'timeout' => 0.0,      //value in seconds (optional, default is 0 meaning unlimited)
24
        'reserved' => null,    //should be NULL if retry_interval is specified
25
        'persistent_id' => '', //identity for the requested persistent connection
26
        'password' => null,
27
        'serializer' => \Redis::SERIALIZER_NONE,
28
        'persistent' => false, //default is connect
29
    ];
30
31
    /**
32
     * __construct.
33
     *
34
     * @param array $config
35
     *
36
     * @throws \RedisException
37
     */
38 6
    public function __construct(array $config) //{{{
39
    {
40 6
        $this->config = array_merge($this->_defaultConfig, $config);
41
42 6
        $this->connection();
43 6
    } //}}}
44
45
    /**
46
     * connection.
47
     *
48
     * @throws \RedisException
49
     *
50
     * @return \Kazuakim\Reddish\Clients
51
     */
52 6
    public function connection(): \Kazuakim\Reddish\Clients //{{{
53
    {
54
        // connect
55 6
        if (!$this->config['persistent'] && !@$this->connect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['reserved'])) {
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

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

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...
56 1
            throw new \RedisException('connect errored.');
57 6
        } elseif (!@$this->pconnect($this->config['host'], $this->config['port'], $this->config['timeout'], $this->config['persistent_id'])) {
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

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

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...
58 1
            throw new \RedisException('pconnect errored.');
59
        }
60
61
        // auth
62 6
        if (0 < strlen($this->config['password']) && !@$this->auth($this->config['password'])) {
63
            throw new \RedisException('auth errored.');
64
        }
65
66
        // serializer
67 6
        $this->setSerializer($this->config['serializer']);
68
69 6
        return $this;
70
    } //}}}
71
72
    /**
73
     * setSerializer.
74
     *
75
     * @param int $serializer
76
     *
77
     * @throws \Expectation
78
     *
79
     * @return \Kazuakim\Reddish\Clients
80
     */
81 6
    public function setSerializer($serializer): \Kazuakim\Reddish\Clients //{{{
82
    {
83 6
        assert(in_array($serializer, self::_getSerializerArray(), true), 'serializer setting errored.');
84
85 6
        $this->setOption(\Redis::OPT_SERIALIZER, $serializer);
86
87 6
        return $this;
88
    } //}}}
89
90
    /**
91
     * close.
92
     */
93 3
    public function close() //{{{
94
    {
95 3
        if ($this->config['persistent']) {
96 3
            parent::close();
97
        }
98
99 3
        return;
100
    } //}}}
101
102
    /**
103
     * _getSerializerArray.
104
     *
105
     * @return array
106
     */
107 6
    private static function _getSerializerArray(): array //{{{
108
    {
109 6
        if (false === extension_loaded('igbinary')) {
110
            return [
111 6
                \Redis::SERIALIZER_NONE,
112
                \Redis::SERIALIZER_PHP,
113
            ];
114
        }
115
116
        return [
117
            \Redis::SERIALIZER_NONE,
118
            \Redis::SERIALIZER_PHP,
119
            \Redis::SERIALIZER_IGBINARY,
120
        ];
121
    } //}}}
122
} //}}}
123