Cluster::connecting()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 26
c 0
b 0
f 0
rs 9.7
cc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Redis cluster
4
 * User: moyo
5
 * Date: 24/10/2017
6
 * Time: 4:28 PM
7
 */
8
9
namespace Carno\Redis;
10
11
use Carno\Cluster\Classify\Scenes;
12
use Carno\Cluster\Contracts\Tags;
13
use Carno\Cluster\Managed;
14
use Carno\Cluster\Resources;
15
use Carno\DSN\DSN;
16
use Carno\Net\Endpoint;
17
use Carno\Pool\Options;
18
use Carno\Pool\Pool;
19
use Carno\Pool\Wrapper\SAR;
20
use Carno\Promise\Promised;
21
use Carno\Redis\Utils\Commands;
22
23
/**
24
 * @mixin Redis
25
 */
26
abstract class Cluster extends Managed
27
{
28
    use SAR;
29
30
    /**
31
     * @var array
32
     */
33
    protected $tags = [Tags::MASTER, Tags::SLAVE];
34
35
    /**
36
     * @var string
37
     */
38
    protected $type = 'redis';
39
40
    /**
41
     * @var int
42
     */
43
    protected $port = 6379;
44
45
    /**
46
     * @var int
47
     */
48
    protected $timeout = 3500;
49
50
    /**
51
     * Cluster constructor.
52
     * @param Resources $resources
53
     */
54
    public function __construct(Resources $resources)
55
    {
56
        $resources->initialize(Scenes::RESOURCE, $this->type, $this->server, $this);
57
    }
58
59
    /**
60
     * @param Endpoint $endpoint
61
     * @return Options
62
     */
63
    abstract protected function options(Endpoint $endpoint) : Options;
64
65
    /**
66
     * @param Endpoint $endpoint
67
     * @return Pool
68
     */
69
    protected function connecting(Endpoint $endpoint) : Pool
70
    {
71
        $node = $endpoint->address();
72
73
        $vid = "{$this->type}:{$this->server}";
74
75
        $dsn = new DSN(
76
            $node->port() <= 0
77
                ? $node->host()
78
                : sprintf('redis://%s:%d', $node->host(), $node->port())
79
        );
80
81
        $timeouts = new Timeouts(
82
            $dsn->option('connect', 1500),
0 ignored issues
show
Bug introduced by
It seems like $dsn->option('connect', 1500) can also be of type null; however, parameter $connect of Carno\Redis\Timeouts::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

82
            /** @scrutinizer ignore-type */ $dsn->option('connect', 1500),
Loading history...
83
            $dsn->option('execute', $this->timeout)
0 ignored issues
show
Bug introduced by
It seems like $dsn->option('execute', $this->timeout) can also be of type null; however, parameter $execute of Carno\Redis\Timeouts::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

83
            /** @scrutinizer ignore-type */ $dsn->option('execute', $this->timeout)
Loading history...
84
        );
85
86
        return new Pool($this->options($endpoint), static function () use ($timeouts, $dsn, $vid) {
87
            return new Redis(
88
                sprintf('%s:%d', $dsn->host(), $dsn->port()),
89
                $dsn->pass() ?: null,
90
                $dsn->option('db'),
91
                $timeouts,
92
                $vid
93
            );
94
        }, $vid);
95
    }
96
97
    /**
98
     * @param Pool $connected
99
     * @return Promised
100
     */
101
    protected function disconnecting($connected) : Promised
102
    {
103
        return $connected->shutdown();
104
    }
105
106
    /**
107
     * @param string $name
108
     * @param $arguments
109
     * @return mixed
110
     */
111
    public function __call(string $name, $arguments)
112
    {
113
        return yield $this->sarRun(
114
            $this->picking(
115
                $this->clustered() && Commands::readonly($name)
116
                    ? Tags::SLAVE
117
                    : Tags::MASTER
118
            ),
119
            $name,
120
            $arguments
121
        );
122
    }
123
}
124