Completed
Push — master ( 863091...4f2e75 )
by Marco
04:08
created

AbstractSocket::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
c 0
b 0
f 0
1
<?php namespace Comodojo\Daemon\Socket;
2
3
use \Comodojo\Foundation\Validation\DataFilter;
4
5
/**
6
 * @package     Comodojo Daemon
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
abstract class AbstractSocket {
22
23
    const VERSION = '1.0';
24
25
    const DEFAULT_READ_BUFFER = 1024;
26
27
    const DEFAULT_SOCKET_PORT = 10042;
28
29
    protected $socket;
30
31
    protected $handler;
32
33
    protected $socket_domain;
34
35
    protected $socket_type = SOCK_STREAM;
36
37
    protected $socket_protocol = 0;
38
39
    protected $socket_resource;
40
41
    protected $socket_port = 0;
42
43
    protected $read_buffer;
44
45 5
    public function __construct($handler, $read_buffer = null) {
46
47 5
        $this->setHandler($handler);
48
49 5
        $this->read_buffer = is_null($read_buffer)
50 5
            ? self::DEFAULT_READ_BUFFER
51
            : DataFilter::filterInteger($read_buffer, 1024, PHP_INT_MAX, self::DEFAULT_READ_BUFFER);
0 ignored issues
show
Bug introduced by
self::DEFAULT_READ_BUFFER of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            : DataFilter::filterInteger($read_buffer, 1024, PHP_INT_MAX, /** @scrutinizer ignore-type */ self::DEFAULT_READ_BUFFER);
Loading history...
Bug introduced by
1024 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            : DataFilter::filterInteger($read_buffer, /** @scrutinizer ignore-type */ 1024, PHP_INT_MAX, self::DEFAULT_READ_BUFFER);
Loading history...
Bug introduced by
Comodojo\Daemon\Socket\PHP_INT_MAX of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            : DataFilter::filterInteger($read_buffer, 1024, /** @scrutinizer ignore-type */ PHP_INT_MAX, self::DEFAULT_READ_BUFFER);
Loading history...
52
53 5
    }
54
55
    public function getSocket() {
56
57
        return $this->socket;
58
59
    }
60
61
    abstract public function connect();
62
63
    abstract public function close();
64
65
    protected function setSocket($socket) {
66
67
        $this->socket = $socket;
68
69
        return $this;
70
71
    }
72
73 5
    protected function setHandler($handler) {
74
75 5
        $this->handler = $handler;
76
77 5
        list($domain, $resource) = preg_split( '@(:\/\/)@', $handler );
78
79 5
        $domain = strtolower($domain);
80
81 5
        if ( $domain == 'unix' ) {
82
            $this->socket_domain =  AF_UNIX;
83
            $this->socket_resource = $resource;
84
        } else {
85 5
            $this->socket_domain = AF_INET;
86 5
            $this->socket_protocol = $domain == 'udp' ? SOL_UDP : SOL_TCP;
87 5
            if (strpos($resource, ':') !== false) {
88 5
                list($this->socket_resource, $this->socket_port) = explode(":", $resource);
89
            } else {
90
                $this->socket_resource = $resource;
91
                $this->socket_port = self::DEFAULT_SOCKET_PORT;
92
            }
93
94
        }
95
96 5
    }
97
98
    public static function getSocketError($socket = null) {
99
100
        return socket_strerror(socket_last_error($socket));
101
102
    }
103
104
}
105