Completed
Pull Request — master (#140)
by
unknown
02:59 queued 01:21
created

FakeReplication   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 116
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A publish() 0 4 1
A subscribe() 0 4 1
A unsubscribe() 0 4 1
A joinChannel() 0 4 1
A leaveChannel() 0 7 2
A channelMembers() 0 8 1
A channelMemberCounts() 0 10 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\PubSub\Fake;
4
5
use stdClass;
6
use React\EventLoop\LoopInterface;
7
use React\Promise\FulfilledPromise;
8
use React\Promise\PromiseInterface;
9
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
10
11
class FakeReplication implements ReplicationInterface
12
{
13
    protected $channels = [];
14
15
    /**
16
     * Boot the pub/sub provider (open connections, initial subscriptions, etc).
17
     *
18
     * @param LoopInterface $loop
19
     * @return self
20
     */
21
    public function boot(LoopInterface $loop) : ReplicationInterface
22
    {
23
        return $this;
24
    }
25
26
    /**
27
     * Publish a payload on a specific channel, for a specific app.
28
     *
29
     * @param string $appId
30
     * @param string $channel
31
     * @param stdClass $payload
32
     * @return bool
33
     */
34
    public function publish(string $appId, string $channel, stdClass $payload) : bool
35
    {
36
        return true;
37
    }
38
39
    /**
40
     * Subscribe to receive messages for a channel.
41
     *
42
     * @param string $appId
43
     * @param string $channel
44
     * @return bool
45
     */
46
    public function subscribe(string $appId, string $channel) : bool
47
    {
48
        return true;
49
    }
50
51
    /**
52
     * Unsubscribe from a channel.
53
     *
54
     * @param string $appId
55
     * @param string $channel
56
     * @return bool
57
     */
58
    public function unsubscribe(string $appId, string $channel) : bool
59
    {
60
        return true;
61
    }
62
63
    /**
64
     * Add a member to a channel. To be called when they have
65
     * subscribed to the channel.
66
     *
67
     * @param string $appId
68
     * @param string $channel
69
     * @param string $socketId
70
     * @param string $data
71
     */
72
    public function joinChannel(string $appId, string $channel, string $socketId, string $data)
73
    {
74
        $this->channels["$appId:$channel"][$socketId] = $data;
75
    }
76
77
    /**
78
     * Remove a member from the channel. To be called when they have
79
     * unsubscribed from the channel.
80
     *
81
     * @param string $appId
82
     * @param string $channel
83
     * @param string $socketId
84
     */
85
    public function leaveChannel(string $appId, string $channel, string $socketId)
86
    {
87
        unset($this->channels["$appId:$channel"][$socketId]);
88
        if (empty($this->channels["$appId:$channel"])) {
89
            unset($this->channels["$appId:$channel"]);
90
        }
91
    }
92
93
    /**
94
     * Retrieve the full information about the members in a presence channel.
95
     *
96
     * @param string $appId
97
     * @param string $channel
98
     * @return PromiseInterface
99
     */
100
    public function channelMembers(string $appId, string $channel) : PromiseInterface
101
    {
102
        $data = array_map(function ($user) {
103
            return json_decode($user);
104
        }, $this->channels["$appId:$channel"]);
105
106
        return new FulfilledPromise($data);
107
    }
108
109
    /**
110
     * Get the amount of users subscribed for each presence channel.
111
     *
112
     * @param string $appId
113
     * @param array $channelNames
114
     * @return PromiseInterface
115
     */
116
    public function channelMemberCounts(string $appId, array $channelNames) : PromiseInterface
117
    {
118
        $data = [];
119
120
        foreach ($channelNames as $channel) {
121
            $data[$channel] = count($this->channels["$appId:$channel"]);
122
        }
123
124
        return new FulfilledPromise($data);
125
    }
126
}
127