1 | <?php declare(strict_types=1); |
||
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) |
||
90 | } |
||
91 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.