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
Push — master ( 5e68ac...cec8b8 )
by Cees-Jan
10s
created

AsyncStreamingClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 61
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sample() 0 6 1
A filtered() 0 15 1
A stream() 0 18 2
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\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