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.

AsyncStreamingClient   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 62
ccs 0
cts 40
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 16 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 GuzzleHttp\Psr7\Request;
9
use Psr\Http\Message\RequestInterface;
10
use React\EventLoop\LoopInterface;
11
use Rx\Observable;
12
use Rx\Operator\CutOperator;
13
use Rx\React\Promise;
14
use Rx\Scheduler\ImmediateScheduler;
15
16
final class AsyncStreamingClient implements AsyncStreamingClientInterface
17
{
18
    const STREAM_DELIMITER = "\r\n";
19
20
    /**
21
     * @var Client
22
     */
23
    protected $client;
24
25
    /**
26
     * @var LoopInterface
27
     */
28
    private $loop;
29
30
    public function __construct(Client $client)
31
    {
32
        $this->client = $client;
33
    }
34
35
    public function sample(): Observable
36
    {
37
        return $this->stream(
38
            new Request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json')
39
        );
40
    }
41
42
    public function filtered(array $filter = []): Observable
43
    {
44
        $postData = http_build_query($filter);
45
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(
0 ignored issues
show
Deprecated Code introduced by
The method Rx\Observable::switchLatest() has been deprecated with message: Use `switch`
Alias for switch

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
            $request
63
        )))->switchLatest()->lift(function () {
64
            return new CutOperator(self::STREAM_DELIMITER, new ImmediateScheduler());
65
        })->filter(function (string $json) {
66
            return trim($json) !== ''; // To keep the stream alive Twitter sends an empty line at times
67
        })->_ApiClients_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