UseAblyChannelConventions::formatChannels()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
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