Skip to content

Commit f5a99bf

Browse files
Merge pull request #39 from stefanak-michal/abstract_connection_class
Abstract connection class
2 parents f36d72d + 2313a3a commit f5a99bf

File tree

3 files changed

+65
-96
lines changed

3 files changed

+65
-96
lines changed

src/connection/AConnection.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Bolt\connection;
4+
5+
/**
6+
* Class AConnection
7+
*
8+
* @author Michal Stefanak
9+
* @link https://github.com/stefanak-michal/Bolt
10+
* @package Bolt\connection
11+
*/
12+
abstract class AConnection implements IConnection
13+
{
14+
15+
/**
16+
* @var string
17+
*/
18+
protected $ip;
19+
20+
/**
21+
* @var int
22+
*/
23+
protected $port;
24+
25+
/**
26+
* @var int
27+
*/
28+
protected $timeout;
29+
30+
/**
31+
* AConnection constructor.
32+
* @param string $ip
33+
* @param int $port
34+
* @param int $timeout
35+
*/
36+
public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15)
37+
{
38+
$this->ip = $ip;
39+
$this->port = $port;
40+
$this->timeout = $timeout;
41+
}
42+
43+
/**
44+
* Print buffer as HEX
45+
* @param string $str
46+
* @param bool $write
47+
*/
48+
protected function printHex(string $str, bool $write = true)
49+
{
50+
$str = implode(unpack('H*', $str));
51+
echo '<pre>';
52+
echo $write ? '> ' : '< ';
53+
foreach (str_split($str, 8) as $chunk) {
54+
echo implode(' ', str_split($chunk, 2));
55+
echo ' ';
56+
}
57+
echo '</pre>';
58+
}
59+
}

src/connection/Socket.php

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,25 @@
1212
* @link https://github.com/stefanak-michal/Bolt
1313
* @package Bolt\connection
1414
*/
15-
class Socket implements IConnection
15+
class Socket extends AConnection
1616
{
1717

18-
/**
19-
* @var string
20-
*/
21-
private $ip;
22-
23-
/**
24-
* @var int
25-
*/
26-
private $port;
27-
28-
/**
29-
* @var int
30-
*/
31-
private $timeout;
32-
3318
/**
3419
* @var resource
3520
*/
3621
private $socket;
3722

38-
/**
39-
* @param string $ip
40-
* @param int $port
41-
* @param int $timeout
42-
* @throws Exception
43-
*/
44-
public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15)
45-
{
46-
if (!extension_loaded('sockets')) {
47-
Bolt::error('PHP Extension sockets not enabled');
48-
}
49-
50-
$this->ip = $ip;
51-
$this->port = $port;
52-
$this->timeout = $timeout;
53-
}
54-
5523
/**
5624
* Create socket connection
5725
* @return bool
5826
* @throws Exception
5927
*/
6028
public function connect(): bool
6129
{
30+
if (!extension_loaded('sockets')) {
31+
throw new ConnectException('PHP Extension sockets not enabled');
32+
}
33+
6234
$this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
6335
if (!is_resource($this->socket)) {
6436
Bolt::error('Cannot create socket');
@@ -147,23 +119,6 @@ public function read(int $length = 2048): string
147119
return $output;
148120
}
149121

150-
/**
151-
* Print buffer as HEX
152-
* @param string $str
153-
* @param bool $write
154-
*/
155-
private function printHex(string $str, bool $write = true)
156-
{
157-
$str = implode(unpack('H*', $str));
158-
echo '<pre>';
159-
echo $write ? '> ' : '< ';
160-
foreach (str_split($str, 8) as $chunk) {
161-
echo implode(' ', str_split($chunk, 2));
162-
echo ' ';
163-
}
164-
echo '</pre>';
165-
}
166-
167122
/**
168123
* Close socket connection
169124
*/

src/connection/StreamSocket.php

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,9 @@
1313
* @link https://github.com/stefanak-michal/Bolt
1414
* @package Bolt\connection
1515
*/
16-
class StreamSocket implements IConnection
16+
class StreamSocket extends AConnection
1717
{
1818

19-
/**
20-
* @var string
21-
*/
22-
private $ip;
23-
24-
/**
25-
* @var int
26-
*/
27-
private $port;
28-
29-
/**
30-
* @var int
31-
*/
32-
private $timeout;
33-
3419
/**
3520
* @var array
3621
*/
@@ -41,19 +26,6 @@ class StreamSocket implements IConnection
4126
*/
4227
private $stream;
4328

44-
/**
45-
* StreamSocket constructor.
46-
* @param string $ip
47-
* @param int $port
48-
* @param int $timeout
49-
*/
50-
public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15)
51-
{
52-
$this->ip = $ip;
53-
$this->port = $port;
54-
$this->timeout = $timeout;
55-
}
56-
5729
/**
5830
* Set SSL Context options
5931
* @link https://www.php.net/manual/en/context.ssl.php
@@ -142,21 +114,4 @@ public function disconnect()
142114
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
143115
}
144116

145-
/**
146-
* Print buffer as HEX
147-
* @param string $str
148-
* @param bool $write
149-
*/
150-
private function printHex(string $str, bool $write = true)
151-
{
152-
$str = implode(unpack('H*', $str));
153-
echo '<pre>';
154-
echo $write ? '> ' : '< ';
155-
foreach (str_split($str, 8) as $chunk) {
156-
echo implode(' ', str_split($chunk, 2));
157-
echo ' ';
158-
}
159-
echo '</pre>';
160-
}
161-
162117
}

0 commit comments

Comments
 (0)