GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#2)
by Cees-Jan
09:40
created

AsyncStreamingClient::sample()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Twitter;
4
5
use ApiClients\Foundation\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiClients\Client\Twitter\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use ApiClients\Foundation\Factory;
7
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
8
use ApiClients\Foundation\Oauth1\Middleware\Oauth1Middleware;
9
use ApiClients\Foundation\Oauth1\Options as Oauth1Options;
10
use ApiClients\Foundation\Options;
11
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
12
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand;
13
use ApiClients\Foundation\Transport\Options as TransportOptions;
14
use ApiClients\Tools\Psr7\Oauth1\Definition;
15
use GuzzleHttp\Psr7\Request;
16
use Psr\Http\Message\RequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use React\EventLoop\LoopInterface;
19
use React\Promise\PromiseInterface;
20
use Rx\Extra\Operator\CutOperator;
21
use Rx\Observable;
22
use Rx\React\Promise;
23
use function React\Promise\resolve;
24
25
final class AsyncStreamingClient
26
{
27
    const STREAM_DELIMITER = "\r\n";
28
29
    /**
30
     * @var LoopInterface
31
     */
32
    private $loop;
33
34
    /**
35
     * @var Client
36
     */
37
    protected $client;
38
39
    public function __construct(Client $client)
40
    {
41
        $this->client = $client;
42
    }
43
44
    public function sample(): Observable
45
    {
46
        return $this->stream(
47
            new Request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json')
48
        );
49
    }
50
51
    public function filtered(array $filter = []): Observable
52
    {
53
        $postData = http_build_query($filter);
54
        return $this->stream(
55
            new Request(
56
                'POST',
57
                'https://stream.twitter.com/1.1/statuses/filter.json',
58
                [
59
                    'Content-Type' =>  'application/x-www-form-urlencoded',
60
                    'Content-Length' => strlen($postData),
61
                ],
62
                $postData
63
            )
64
        );
65
    }
66
67
    protected function stream(RequestInterface $request): Observable
68
    {
69
        return Promise::toObservable($this->client->handle(new StreamingRequestCommand(
70
            $request
71
        )))->switchLatest()->lift(function () {
72
            return new CutOperator(self::STREAM_DELIMITER);
73
        })->filter(function (string $json) {
74
            return trim($json) !== ''; // To keep the stream alive Twitter sends an empty line at times
75
        })->jsonDecode()->flatMap(function (array $document) {
76
            if (isset($document['delete'])) {
77
                return Promise::toObservable($this->client->handle(
78
                    new HydrateCommand('DeletedTweet', $document['delete'])
79
                ));
80
            }
81
82
            return Promise::toObservable($this->client->handle(new HydrateCommand('Tweet', $document)));
83
        });
84
    }
85
}
86