UseAblyChannelConventions   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 47
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isGuardedChannel() 0 3 1
A normalizeChannelName() 0 9 3
A formatChannels() 0 11 3
1
<?php
2
3
namespace SpringboardVR\LaravelAblyBroadcaster;
4
5
use Illuminate\Support\Str;
6
7
trait UseAblyChannelConventions
8
{
9
    /**
10
     * Return true if channel is protected by authentication.
11
     *
12
     * @param  string  $channel
13
     * @return bool
14
     */
15
    public function isGuardedChannel($channel)
16
    {
17
        return Str::startsWith($channel, ['private-', 'presence-']);
18
    }
19
20
    /**
21
     * Remove prefix from channel name.
22
     *
23
     * @param  string  $channel
24
     * @return string
25
     */
26
    public function normalizeChannelName($channel)
27
    {
28
        if ($this->isGuardedChannel($channel)) {
29
            return Str::startsWith($channel, 'private-')
30
                ? Str::replaceFirst('private-', '', $channel)
31
                : Str::replaceFirst('presence-', '', $channel);
32
        }
33
34
        return $channel;
35
    }
36
37
    /**
38
     * Format the channel array into an array of strings.
39
     *
40
     * @param  array  $channels
41
     * @return array
42
     */
43
    protected function formatChannels(array $channels)
44
    {
45
        return array_map(function ($channel) {
46
            $channel = (string) $channel;
47
            if (Str::startsWith($channel, ['private-', 'presence-'])) {
48
                return Str::startsWith($channel, 'private-')
49
                    ? Str::replaceFirst('private-', 'private:', $channel)
50
                    : Str::replaceFirst('presence-', 'presence:', $channel);
51
            }
52
            return 'public:' . $channel;
53
        }, $channels);
54
    }
55
}
56