1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Twitter; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildSyncFromAsyncCommand; |
6
|
|
|
use ApiClients\Foundation\Resource\ResourceInterface; |
7
|
|
|
use ApiClients\Tools\CommandBus\CommandBusInterface; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use ReflectionObject; |
10
|
|
|
use Rx\ObservableInterface; |
11
|
|
|
use Rx\React\Promise; |
12
|
|
|
|
13
|
|
|
final class StreamingClient implements StreamingClientInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var LoopInterface |
17
|
|
|
*/ |
18
|
|
|
protected $loop; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var CommandBusInterface |
22
|
|
|
*/ |
23
|
|
|
protected $commandBus; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var AsyncStreamingClientInterface |
27
|
|
|
*/ |
28
|
|
|
protected $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $hydrateClassConstantCache = []; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* StreamingClient constructor. |
37
|
|
|
* @param LoopInterface $loop |
38
|
|
|
* @param CommandBusInterface $commandBus |
39
|
|
|
* @param AsyncStreamingClientInterface $client |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
LoopInterface $loop, |
43
|
|
|
CommandBusInterface $commandBus, |
44
|
|
|
AsyncStreamingClientInterface $client |
45
|
|
|
) { |
46
|
|
|
$this->loop = $loop; |
47
|
|
|
$this->commandBus = $commandBus; |
48
|
|
|
$this->client = $client; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function sample(callable $listener) |
52
|
|
|
{ |
53
|
|
|
$this->stream($this->client->sample(), $listener); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function filtered(callable $listener, array $filter = []) |
57
|
|
|
{ |
58
|
|
|
$this->stream($this->client->filtered($filter), $listener); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function stream(ObservableInterface $observable, callable $listener) |
62
|
|
|
{ |
63
|
|
|
$observable->flatMap(function (ResourceInterface $resource) { |
64
|
|
|
return Promise::toObservable( |
65
|
|
|
$this->commandBus->handle( |
66
|
|
|
new BuildSyncFromAsyncCommand( |
67
|
|
|
$this->loopUpHydrateClassConstant($resource), |
68
|
|
|
$resource |
69
|
|
|
) |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
})->subscribeCallback( |
73
|
|
|
$listener, |
74
|
|
|
function ($error) { |
75
|
|
|
throw $error; |
76
|
|
|
} |
77
|
|
|
); |
78
|
|
|
$this->loop->run(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function loopUpHydrateClassConstant(ResourceInterface $resource) |
82
|
|
|
{ |
83
|
|
|
$class = get_class($resource); |
84
|
|
|
if (!isset($this->hydrateClassConstantCache[$class])) { |
85
|
|
|
$this->hydrateClassConstantCache[$class] = (new ReflectionObject($resource))->getConstant('HYDRATE_CLASS'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->hydrateClassConstantCache[$class]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.