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 (#3)
by Cees-Jan
08:36
created

AsyncClient::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 8
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\Pusher;
5
6
use React\EventLoop\LoopInterface;
7
use Rx\Disposable\CallbackDisposable;
8
use Rx\Observable;
9
use Rx\ObservableInterface;
10
use Rx\Observer\CallbackObserver;
11
use Rx\ObserverInterface;
12
use Rx\React\Promise;
13
use Rx\Scheduler\EventLoopScheduler;
14
use Rx\SchedulerInterface;
15
use Rx\Websocket\Client;
16
use Rx\Websocket\MessageSubject;
17
use WyriHaximus\ApiClient\Transport\Client as Transport;
18
use WyriHaximus\ApiClient\Transport\Factory;
19
use function React\Promise\resolve;
20
use function EventLoop\getLoop;
21
use function EventLoop\setLoop;
22
23
class AsyncClient
24
{
25
    protected $transport;
26
    protected $app;
27
    protected $url;
28
    protected $client;
29
    protected $message;
30
    protected $channels = [];
31
32
    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...
33
    {
34
        setLoop($loop);
35
        /*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...
36
            $transport = Factory::create($loop, [
37
                    'resource_namespace' => 'Async',
38
                ] + ApiSettings::TRANSPORT_OPTIONS);
39
        }
40
        $this->transport = $transport;*/
41
42
        $this->app = $app;
43
44
        $this->url = 'wss://ws.pusherapp.com/app/' .
45
            $this->app .
46
            '?client=wyrihaximus-php-pusher-client&version=0.0.1&protocol=7'
47
        ;
48
        $this->client   = (new Client($this->url))->shareReplay(1); //Only create one connection and share the most recent among all subscriber
49
        $this->messages = $this->client
0 ignored issues
show
Bug introduced by
The property messages does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50
            ->flatMap(function (MessageSubject $ms) {
51
                return $ms;
52
            })
53
            ->map('json_decode');
54
    }
55
56
    public function channel(string $channel): ObservableInterface
57
    {
58
        $channelMessages = $this->messages->filter(function ($event) use ($channel) {
0 ignored issues
show
Bug introduced by
The property messages does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59
            return isset($event->channel) && $event->channel == $channel;
60
        });
61
62
        $events = Observable::create(function (ObserverInterface $observer, SchedulerInterface $scheduler) use ($channel, $channelMessages) {
63
64
            $subscription = $channelMessages
65
                ->filter(function ($msg) {
66
                    return $msg->event !== 'pusher_internal:subscription_succeeded';
67
                })
68
                ->subscribe($observer, $scheduler);
69
70
            $this->send(['event' => 'pusher:subscribe', 'data' => ['channel' => $channel]]);
71
72
            return new CallbackDisposable(function () use ($channel, $subscription) {
73
                $this->send(['event' => 'pusher:unsubscribe', 'data' => ['channel' => $channel]]);
74
                $subscription->dispose();
75
            });
76
        });
77
78
        return $events->share();
79
    }
80
81
    public function send(array $message)
82
    {
83
        $this->client
84
            ->take(1)
85
            ->subscribeCallback(function (MessageSubject $ms) use ($message) {
86
                $ms->send(json_encode($message));
87
            });
88
    }
89
}
90