Completed
Pull Request — master (#100)
by Maxime
02:21
created

Url::initialize()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
crap 20
1
<?php
2
3
/**
4
 * This file is a part of Woketo package.
5
 *
6
 * (c) Nekland <[email protected]>
7
 *
8
 * For the full license, take a look to the LICENSE file
9
 * on the root directory of this project
10
 */
11
12
namespace Nekland\Woketo\Http;
13
14
use Nekland\Tools\StringTools;
15
16
/**
17
 * Class Url
18
 *
19
 * Represent a WebSocket URL
20
 */
21
class Url
22
{
23
    /**
24
     * @var bool
25
     */
26
    private $secured;
27
28
    /**
29
     * example: 127.0.0.1
30
     *
31
     * @var string
32
     */
33
    private $host;
34
35
    /**
36
     * example: 8080
37
     *
38
     * @var int
39
     */
40
    private $port;
41
42
    /**
43
     * example: /chat
44
     *
45
     * @var string
46
     */
47
    private $uri;
48
49
    public function __construct(string $url = null)
50
    {
51
        if (null !== $url) {
52
            $this->initialize($url);
53
        }
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function isSecured(): bool
60
    {
61
        return $this->secured;
62
    }
63
64
    /**
65
     * @param bool $secured
66
     * @return Url
67
     */
68
    public function setSecured(bool $secured = true)
69
    {
70
        $this->secured = $secured;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getHost(): string
79
    {
80
        return $this->host;
81
    }
82
83
    /**
84
     * @param string $host
85
     * @return Url
86
     */
87
    public function setHost(string $host)
88
    {
89
        $this->host = $host;
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getPort(): int
98
    {
99
        return $this->port;
100
    }
101
102
    /**
103
     * @param int $port
104
     * @return Url
105
     */
106
    public function setPort(int $port)
107
    {
108
        $this->port = $port;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getUri(): string
117
    {
118
        return $this->uri;
119
    }
120
121
    /**
122
     * @param string $uri
123
     * @return Url
124
     */
125
    public function setUri($uri)
126
    {
127
        $this->uri = $uri;
128
129
        return $this;
130
    }
131
132
    private function initialize(string $url)
133
    {
134
        $match = preg_match('/^wss?:\/\/(.+):([\d]{2,5})(\/.+)?/', $url, $matches);
135
136
        if ($match !== 1 || !in_array(count($matches), [3, 4])) {
137
            throw new \InvalidArgumentException(sprintf('The URL %s is invalid.', $url));
138
        }
139
140
        $this->secured = StringTools::startsWith($url, 'wss');
141
        $this->host = $matches[1];
142
        $this->port = $matches[2];
0 ignored issues
show
Documentation Bug introduced by
The property $port was declared of type integer, but $matches[2] is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
143
        $this->uri = isset($matches[3]) ? $matches[3] : '/';
144
    }
145
146
    public function __toString()
147
    {
148
        $str = $this->secured ? 'wss://' : 'ws://';
149
        $str .= $this->host . ':' . $this->port . $this->uri;
150
151
        return $str;
152
    }
153
}
154