3
3
namespace Clue \React \Redis ;
4
4
5
5
use Evenement \EventEmitter ;
6
- use React \Stream \Util ;
7
6
use React \EventLoop \LoopInterface ;
7
+ use React \EventLoop \TimerInterface ;
8
+ use React \Promise \PromiseInterface ;
9
+ use React \Stream \Util ;
8
10
use function React \Promise \reject ;
9
11
10
12
/**
11
13
* @internal
12
14
*/
13
15
class LazyClient extends EventEmitter implements Client
14
16
{
15
- private $ target ;
16
- /** @var Factory */
17
- private $ factory ;
18
- private $ closed = false ;
19
- private $ promise ;
20
-
21
- private $ loop ;
22
- private $ idlePeriod = 60.0 ;
23
- private $ idleTimer ;
24
- private $ pending = 0 ;
25
-
26
- private $ subscribed = [];
27
- private $ psubscribed = [];
28
-
29
- /**
30
- * @param $target
31
- */
32
- public function __construct ($ target , Factory $ factory , LoopInterface $ loop )
17
+ private string $ target ;
18
+ private Factory $ factory ;
19
+ private bool $ closed = false ;
20
+ private ?PromiseInterface $ promise = null ;
21
+
22
+ private LoopInterface $ loop ;
23
+ private float $ idlePeriod = 60.0 ;
24
+ private ?TimerInterface $ idleTimer = null ;
25
+ private int $ pending = 0 ;
26
+
27
+ private array $ subscribed = [];
28
+ private array $ psubscribed = [];
29
+
30
+ public function __construct (string $ target , Factory $ factory , LoopInterface $ loop )
33
31
{
34
32
$ args = [];
35
33
\parse_str ((string ) \parse_url ($ target , \PHP_URL_QUERY ), $ args );
@@ -42,7 +40,7 @@ public function __construct($target, Factory $factory, LoopInterface $loop)
42
40
$ this ->loop = $ loop ;
43
41
}
44
42
45
- private function client ()
43
+ private function client (): PromiseInterface
46
44
{
47
45
if ($ this ->promise !== null ) {
48
46
return $ this ->promise ;
@@ -71,16 +69,16 @@ private function client()
71
69
});
72
70
73
71
// keep track of all channels and patterns this connection is subscribed to
74
- $ redis ->on ('subscribe ' , function ($ channel ) {
72
+ $ redis ->on ('subscribe ' , function (string $ channel ) {
75
73
$ this ->subscribed [$ channel ] = true ;
76
74
});
77
- $ redis ->on ('psubscribe ' , function ($ pattern ) {
75
+ $ redis ->on ('psubscribe ' , function (string $ pattern ) {
78
76
$ this ->psubscribed [$ pattern ] = true ;
79
77
});
80
- $ redis ->on ('unsubscribe ' , function ($ channel ) {
78
+ $ redis ->on ('unsubscribe ' , function (string $ channel ) {
81
79
unset($ this ->subscribed [$ channel ]);
82
80
});
83
- $ redis ->on ('punsubscribe ' , function ($ pattern ) {
81
+ $ redis ->on ('punsubscribe ' , function (string $ pattern ) {
84
82
unset($ this ->psubscribed [$ pattern ]);
85
83
});
86
84
@@ -106,7 +104,7 @@ private function client()
106
104
});
107
105
}
108
106
109
- public function __call ($ name , $ args )
107
+ public function __call (string $ name , array $ args ): PromiseInterface
110
108
{
111
109
if ($ this ->closed ) {
112
110
return reject (new \RuntimeException (
@@ -122,15 +120,15 @@ function ($result) {
122
120
$ this ->idle ();
123
121
return $ result ;
124
122
},
125
- function ($ error ) {
123
+ function (\ Exception $ error ) {
126
124
$ this ->idle ();
127
125
throw $ error ;
128
126
}
129
127
);
130
128
});
131
129
}
132
130
133
- public function end ()
131
+ public function end (): void
134
132
{
135
133
if ($ this ->promise === null ) {
136
134
$ this ->close ();
@@ -140,15 +138,15 @@ public function end()
140
138
return ;
141
139
}
142
140
143
- return $ this ->client ()->then (function (Client $ redis ) {
141
+ $ this ->client ()->then (function (Client $ redis ) {
144
142
$ redis ->on ('close ' , function () {
145
143
$ this ->close ();
146
144
});
147
145
$ redis ->end ();
148
146
});
149
147
}
150
148
151
- public function close ()
149
+ public function close (): void
152
150
{
153
151
if ($ this ->closed ) {
154
152
return ;
@@ -176,10 +174,7 @@ public function close()
176
174
$ this ->removeAllListeners ();
177
175
}
178
176
179
- /**
180
- * @internal
181
- */
182
- public function awake ()
177
+ private function awake (): void
183
178
{
184
179
++$ this ->pending ;
185
180
@@ -189,10 +184,7 @@ public function awake()
189
184
}
190
185
}
191
186
192
- /**
193
- * @internal
194
- */
195
- public function idle ()
187
+ private function idle (): void
196
188
{
197
189
--$ this ->pending ;
198
190
0 commit comments