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 ( a31e21...94043f )
by Cees-Jan
01:58
created

AsyncClient::subscribe()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
cc 2
eloc 10
nc 1
nop 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\Pusher;
5
6
use Ratchet\Client\Connector;
7
use Ratchet\Client\WebSocket;
8
use Ratchet\RFC6455\Messaging\MessageInterface;
9
use React\EventLoop\LoopInterface;
10
use Rx\Observable;
11
use Rx\ObservableInterface;
12
use Rx\ObserverInterface;
13
use Rx\React\Promise;
14
use WyriHaximus\ApiClient\Transport\Client as Transport;
15
use WyriHaximus\ApiClient\Transport\Factory;
16
use function React\Promise\resolve;
17
18
class AsyncClient
19
{
20
    protected $transport;
21
    protected $app;
22
    protected $url;
23
    protected $socket;
24
    protected $connection;
25
    protected $channels = [];
26
27
    public function __construct(LoopInterface $loop, string $app, Transport $transport = null)
0 ignored issues
show
Unused Code introduced by
The parameter $transport is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        /*if (!($transport instanceof Transport)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
            $transport = Factory::create($loop, [
31
                    'resource_namespace' => 'Async',
32
                ] + ApiSettings::TRANSPORT_OPTIONS);
33
        }
34
        $this->transport = $transport;*/
35
36
        $this->app = $app;
37
38
        $this->url = 'wss://ws.pusherapp.com:443/app/' .
39
            $this->app .
40
            '?client=wyrihaximus-php-pusher-client&version=0.0.1&protocol=7'
41
        ;
42
        $connector = new Connector($loop);
43
        $this->socket = $connector($this->url);
44
        $this->connection = Promise::toObservable($this->socket)
45
            ->flatMap(function (WebSocket $socket) {
46
                $this->socket = resolve($socket);
47
                return Observable::create(function (ObserverInterface $observer) use ($socket) {
48
                    $socket->on('message', function (MessageInterface $message) use ($observer) {
49
                        $observer->onNext($message);
50
                    });
51
                    $socket->on('close', function ($code, $reason) {
0 ignored issues
show
Unused Code introduced by
The parameter $code is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $reason is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
53
                    });
54
                });
55
            })->map(function (MessageInterface $message) {
56
                return json_decode((string)$message, true);
57
            });
58
    }
59
60
    public function subscribe(string $channel): ObservableInterface
61
    {
62
        $this->channels[$channel] = $channel;
63
        $this->send([
64
            'event' => 'pusher:subscribe',
65
            'data' => [
66
                'channel' => $channel,
67
            ],
68
        ]);
69
70
        return $this->connection->filter(function (array $event) use ($channel) {
71
            return isset($event['channel']) && $event['channel'] == $channel;
72
        })->filter(function (array $event) use ($channel) {
73
            return $event['event'] !== 'pusher_internal:subscription_succeeded';
74
        });
75
    }
76
77
    public function send(array $message)
78
    {
79
        $this->socket->then(function (WebSocket $socket) use ($message) {
80
            $socket->send(json_encode($message));
81
        });
82
    }
83
}
84