| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApiClients\Client\Twitter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ApiClients\Foundation\Client; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ApiClients\Tools\Psr7\Oauth1\Definition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Http\Message\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use React\EventLoop\LoopInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Rx\Extra\Operator\CutOperator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Rx\Observable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Rx\React\Promise; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function React\Promise\resolve; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | final class AsyncStreamingClient implements AsyncStreamingClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const STREAM_DELIMITER = "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var LoopInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $loop; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct(Client $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     public function sample(): Observable | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         return $this->stream( | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             new Request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json') | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function filtered(array $filter = []): Observable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $postData = http_build_query($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this->stream( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             new Request( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'POST', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 'https://stream.twitter.com/1.1/statuses/filter.json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     'Content-Type' =>  'application/x-www-form-urlencoded', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     'Content-Length' => strlen($postData), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $postData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected function stream(RequestInterface $request): Observable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return Promise::toObservable($this->client->handle(new StreamingRequestCommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         )))->switchLatest()->lift(function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return new CutOperator(self::STREAM_DELIMITER); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         })->filter(function (string $json) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return trim($json) !== ''; // To keep the stream alive Twitter sends an empty line at times | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         })->jsonDecode()->flatMap(function (array $document) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             if (isset($document['delete'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 return Promise::toObservable($this->client->handle( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     new HydrateCommand('DeletedTweet', $document['delete']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return Promise::toObservable($this->client->handle(new HydrateCommand('Tweet', $document))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                        
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: