Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d97eed4

Browse files
Code style && SSL => TLS
1 parent 89f4b44 commit d97eed4

2 files changed

Lines changed: 20 additions & 19 deletions

File tree

src/Protocol/ProtocolEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ enum ProtocolEnum: string
1010
case TCP = 'tcp';
1111
case TCP4 = 'tcp4';
1212
case TCP6 = 'tcp6';
13+
case TLS = 'tls';
1314
}

src/Transport/StreamTransport.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(
4141
private function getProtocol(): ProtocolEnum
4242
{
4343
if ($this->config->getUseTls()) {
44-
return ProtocolEnum::SSL;
44+
return ProtocolEnum::TLS;
4545
} elseif ($this->config->isForceIpv6()) {
4646
return ProtocolEnum::TCP6;
4747
} elseif ($this->config->isForceIpv4()) {
@@ -128,22 +128,20 @@ public function close (): void
128128

129129
public function isOpen (): bool
130130
{
131-
if (!is_resource($this->socket))
131+
if (!is_resource($this->socket)) {
132132
return false;
133+
}
133134

134135
$r = null;
135136
$w = null;
136137
$e = [$this->socket];
137138
$res = stream_select($r, $w, $e, 0);
138139

139-
if ($res === false)
140-
throw new SocketTransportException('Could not examine stream');
141-
142-
// if there is an exception on our stream it's probably dead
143-
if (!empty($e))
144-
return false;
140+
if (false === $res) {
141+
throw new SocketTransportException("Stream can't connect");
142+
}
145143

146-
return true;
144+
return empty($e);
147145
}
148146

149147
public function hasData(): bool
@@ -162,7 +160,7 @@ public function hasData(): bool
162160
public function read(int $length): string
163161
{
164162
if (!$this->socket) {
165-
throw new SocketTransportException('Socket is not open');
163+
throw new SocketTransportException('Stream is not open');
166164
}
167165

168166
if (!is_resource($this->socket) || feof($this->socket)) {
@@ -182,11 +180,11 @@ public function read(int $length): string
182180
if (stream_select($read, $write, $except, $readTimeout['sec'], $readTimeout['usec']) > 0) {
183181
$data = fread($this->socket, $length);
184182
} else {
185-
throw new SocketTransportException('No answer from server');
183+
throw new SocketTransportException('Stream no answer from server');
186184
}
187185

188-
if ($data === false) {
189-
throw new SocketTransportException('Could not read ' . $length . ' bytes from stream');
186+
if (false === $data) {
187+
throw new SocketTransportException("Stream could not read $length bytes");
190188
}
191189

192190
return $data;
@@ -203,8 +201,9 @@ private function millisecToArray(int $milliseconds): array
203201

204202
public function write(string $buffer, int $length): void
205203
{
206-
if (!is_resource($this->socket) || feof($this->socket))
204+
if (!is_resource($this->socket) || feof($this->socket)) {
207205
throw new SocketTransportException('Stream connection error');
206+
}
208207

209208
$r = $length;
210209
$writeTimeout = $this->millisecToArray($this->config->getDefaultSendTimeout());
@@ -215,16 +214,17 @@ public function write(string $buffer, int $length): void
215214
while ($r > 0) {
216215
$wrote = fwrite($this->socket, $buffer, $r);
217216

218-
if ($wrote === false)
219-
throw new SocketTransportException('Could not write ' . $length . ' bytes to stream');
217+
if (false === $wrote) {
218+
throw new SocketTransportException("Stream fail to write $length bytes");
219+
}
220220

221221
$r -= $wrote;
222-
223-
$buffer = substr($buffer, $wrote);
224-
222+
225223
if (0 === $r) {
226224
return;
227225
}
226+
227+
$buffer = substr($buffer, $wrote);
228228
}
229229
}
230230
}

0 commit comments

Comments
 (0)