-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathCarrierServer.php
213 lines (170 loc) · 5.85 KB
/
CarrierServer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Ivoz\Provider\Domain\Model\CarrierServer;
use Assert\Assertion;
/**
* CarrierServer
*/
class CarrierServer extends CarrierServerAbstract implements CarrierServerInterface
{
use CarrierServerTrait;
/**
* @codeCoverageIgnore
* @return array<string, mixed>
*/
public function getChangeSet(): array
{
return parent::getChangeSet();
}
/**
* Get id
* @codeCoverageIgnore
* @return integer
*/
public function getId(): ?int
{
return $this->id;
}
protected function sanitizeValues(): void
{
$this->sanitizeBrandByCarrier();
$this->sanitizeAuth();
}
protected function sanitizeBrandByCarrier(): void
{
$isNew = !$this->getId();
$isNotNewAndCarrierHasChanged =
!$isNew
&& $this->hasChanged('carrierId');
if ($isNotNewAndCarrierHasChanged || !$this->getBrand()->getId()) {
$brand = $this
->getCarrier()
->getBrand();
$this->setBrand($brand);
}
}
protected function sanitizeAuth(): void
{
if ($this->getAuthNeeded() === 'no') {
$this->setAuthUser(null);
$this->setAuthPassword(null);
}
}
public function setOutboundProxy(?string $outboundProxy = null): static
{
if ($outboundProxy) {
$outboundProxy = trim($outboundProxy);
if ($outboundProxy === '') {
return parent::setOutboundProxy(null);
}
if (str_contains($outboundProxy, ' ')) {
throw new \DomainException('Outbound Proxy must not contain spaces.', 70001);
}
$parts = explode(':', $outboundProxy);
if (count($parts) > 2) {
throw new \DomainException('Outbound Proxy must be in format IP or IP:port, not more than one colon.', 70002);
}
$ip = $parts[0];
if (!preg_match('/^(\d{1,3}\.){3}\d{1,3}$/', $ip)) {
throw new \DomainException('IP format is invalid in Outbound Proxy.', 70003);
}
foreach (explode('.', $ip) as $octet) {
$value = (int) $octet;
if ($value < 0 || $value > 255) {
throw new \DomainException('Each segment of the IP must be between 0 and 255.', 70006);
}
}
$this->setIp($ip);
if (isset($parts[1])) {
$port = $parts[1];
if (!ctype_digit($port)) {
throw new \DomainException('Port must be a numeric value.', 70007);
}
$port = (int) $port;
if ($port < 1 || $port > 65535) {
throw new \DomainException('Port must be between 1 and 65535.', 70004);
}
$this->setPort($port);
} else {
$this->setPort(5060);
}
}
return parent::setOutboundProxy($outboundProxy);
}
/**
* {@inheritDoc}
*/
public function setIp(?string $ip = null): static
{
if (!is_null($ip)) {
Assertion::ip($ip);
}
return parent::setIp($ip);
}
public function getName(): string
{
return
sprintf(
'b%dc%ds%d',
(int) $this->getBrand()->getId(),
(int) $this->getCarrier()->getId(),
(int) $this->getId()
);
}
protected function setAuthPassword(?string $authPassword = null): static
{
if ($authPassword) {
$authPassword = trim($authPassword);
}
return parent::setAuthPassword($authPassword);
}
public function setSipProxy(?string $sipProxy = null): static
{
if ($sipProxy !== null) {
$sipProxy = trim($sipProxy);
if ($sipProxy === '') {
throw new \InvalidArgumentException('SIP Proxy cannot be empty.');
}
if (str_contains($sipProxy, ' ')) {
throw new \InvalidArgumentException('SIP Proxy must not contain spaces.');
}
$parts = explode(':', $sipProxy);
if (count($parts) > 2) {
throw new \DomainException('SIP Proxy can only contain one colon at most.');
}
$host = $parts[0];
if (preg_match('/^(\d{1,3}\.){3}\d{1,3}$/', $host)) {
foreach (explode('.', $host) as $octet) {
$value = (int) $octet;
if ($value < 0 || $value > 255) {
throw new \DomainException('Each segment of the IP must be between 0 and 255.');
}
}
} else {
if (str_contains($host, ':') || str_contains($host, ' ')) {
throw new \DomainException('Invalid domain format for SIP Proxy.');
}
if (!str_contains($host, '.')) {
throw new \DomainException('Domain must contain at least one dot.');
}
if (str_ends_with($host, '.')) {
throw new \DomainException('Domain must not end with a dot.');
}
}
if (isset($parts[1])) {
$port = $parts[1];
if (!ctype_digit($port)) {
throw new \DomainException('SIP Proxy port must be numeric.');
}
$port = (int) $port;
if ($port < 1 || $port > 65535) {
throw new \DomainException('SIP Proxy port must be between 1 and 65535.');
}
$this->setPort($port);
} else {
$this->setPort(5060);
}
$this->setHostname($host);
}
return parent::setSipProxy($sipProxy);
}
}