DashboardLogger::log()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 30
rs 9.7666
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets;
4
5
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
6
7
class DashboardLogger
8
{
9
    const LOG_CHANNEL_PREFIX = 'private-websockets-dashboard-';
10
11
    const TYPE_DISCONNECTED = 'disconnected';
12
13
    const TYPE_CONNECTED = 'connected';
14
15
    const TYPE_SUBSCRIBED = 'subscribed';
16
17
    const TYPE_WS_MESSAGE = 'ws-message';
18
19
    const TYPE_API_MESSAGE = 'api-message';
20
21
    const TYPE_REPLICATOR_SUBSCRIBED = 'replicator-subscribed';
22
23
    const TYPE_REPLICATOR_UNSUBSCRIBED = 'replicator-unsubscribed';
24
25
    const TYPE_REPLICATOR_MESSAGE_RECEIVED = 'replicator-message-received';
26
27
    /**
28
     * The list of all channels.
29
     *
30
     * @var array
31
     */
32
    public static $channels = [
33
        self::TYPE_DISCONNECTED,
34
        self::TYPE_CONNECTED,
35
        self::TYPE_SUBSCRIBED,
36
        self::TYPE_WS_MESSAGE,
37
        self::TYPE_API_MESSAGE,
38
        self::TYPE_REPLICATOR_SUBSCRIBED,
39
        self::TYPE_REPLICATOR_UNSUBSCRIBED,
40
        self::TYPE_REPLICATOR_MESSAGE_RECEIVED,
41
    ];
42
43
    /**
44
     * Log an event for an app.
45
     *
46
     * @param  mixed  $appId
47
     * @param  string  $type
48
     * @param  array  $details
49
     * @return void
50
     */
51
    public static function log($appId, string $type, array $details = [])
52
    {
53
        $channelManager = app(ChannelManager::class);
54
55
        $channelName = static::LOG_CHANNEL_PREFIX.$type;
56
57
        $payload = [
58
            'event' => 'log-message',
59
            'channel' => $channelName,
60
            'data' => [
61
                'type' => $type,
62
                'time' => strftime('%H:%M:%S'),
63
                'details' => $details,
64
            ],
65
        ];
66
67
        // Here you can use the ->find(), even if the channel
68
        // does not exist on the server. If it does not exist,
69
        // then the message simply will get broadcasted
70
        // across the other servers.
71
        $channel = $channelManager->find($appId, $channelName);
72
73
        if ($channel) {
74
            $channel->broadcastLocally(
75
                $appId, (object) $payload
76
            );
77
        }
78
79
        $channelManager->broadcastAcrossServers(
80
            $appId, null, $channelName, (object) $payload
81
        );
82
    }
83
}
84