WebSocket   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 10
c 2
b 1
f 0
lcom 0
cbo 1
dl 0
loc 56
ccs 27
cts 27
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 6 1
D parseOptions() 0 34 9
1
<?php
2
namespace SocketCluster;
3
4
use WebSocket\Client;
5
6
class WebSocket
7
{
8
    /**
9
     * Get Client
10
     *
11
     * @return Client
12
     */
13 5
    public static function factory($options)
14
    {
15 5
        $socket_uri = self::parseOptions($options);
16
17 5
        return new Client($socket_uri);
18
    }
19
20
    /**
21
     * Parse Options
22
     *
23
     * @param string|array $options
24
     *
25
     * @return void
26
     */
27 5
    protected static function parseOptions($options)
28
    {
29
        $default = [
30 5
            'scheme' => '',
31 5
            'host' => '',
32 5
            'port' => '',
33 5
            'path' => '',
34 5
            'query' => [],
35 5
            'fragment' => '',
36 5
        ];
37
38 5
        $optArr = (!is_array($options)) ? parse_url($options) : $options;
39 5
        $optArr = array_merge($default, $optArr);
40
41 5
        if (isset($optArr['secure'])) {
42 3
            $scheme = ((bool) $optArr['secure']) ? 'wss' : 'ws';
43
44 3
        } else {
45 4
            $scheme = (in_array($optArr['scheme'], ['wss', 'https'])) ? 'wss' : 'ws';
46
        }
47
48 5
        $query = $optArr['query'];
49 5
        if (!is_array($query)) {
50 1
            parse_str($optArr['query'], $query);
51 1
        }
52
53 5
        $host = trim($optArr['host'], "/");
54 5
        $port = !empty($optArr['port']) ? ":".$optArr['port'] : '';
55 5
        $path = trim($optArr['path'], "/");
56 5
        $path = !empty($path) ? $path."/" : '';
57 5
        $query = count($query) ? '?'.http_build_query($query) : '';
58
59 5
        return sprintf("%s://%s%s/%s%s", $scheme, $host, $port, $path, $query);
60
    }
61
}
62