Completed
Pull Request — master (#447)
by Alexandru
01:25
created

PusherMessageFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A createForMessage() 0 11 2
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Server\Messages;
4
5
use BeyondCode\LaravelWebSockets\Contracts\ChannelManager;
6
use BeyondCode\LaravelWebSockets\Contracts\PusherMessage;
7
use Illuminate\Support\Str;
8
use Ratchet\ConnectionInterface;
9
use Ratchet\RFC6455\Messaging\MessageInterface;
10
11
class PusherMessageFactory
12
{
13
    /**
14
     * Create a new message.
15
     *
16
     * @param  \Ratchet\RFC6455\Messaging\MessageInterface  $message
17
     * @param  \Ratchet\ConnectionInterface  $connection
18
     * @param  \BeyondCode\LaravelWebSockets\Contracts\ChannelManager  $channelManager
19
     * @return PusherMessage
20
     */
21
    public static function createForMessage(
22
        MessageInterface $message,
23
        ConnectionInterface $connection,
24
        ChannelManager $channelManager): PusherMessage
25
    {
26
        $payload = json_decode($message->getPayload());
27
28
        return Str::startsWith($payload->event, 'pusher:')
29
            ? new PusherChannelProtocolMessage($payload, $connection, $channelManager)
30
            : new PusherClientMessage($payload, $connection, $channelManager);
31
    }
32
}
33