AbstractSocket   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 131
ccs 0
cts 46
cp 0
rs 10
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setPort() 0 3 1
A getHost() 0 3 1
A getPort() 0 3 1
A getSocket() 0 3 1
A setHost() 0 3 1
A __construct() 0 16 5
A close() 0 3 1
A bind() 0 10 2
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Network\Socket;
12
13
/**
14
 * AbstractSocket class
15
 *
16
 * This class defines some standard functions to interact with sockets.
17
 * Client and Server behaviors must be implemented.
18
 */
19
abstract class AbstractSocket
20
{
21
    use \Drone\Error\ErrorTrait;
22
23
    /**
24
     * Host
25
     *
26
     * @var string
27
     */
28
    protected $host;
29
30
    /**
31
     * Port
32
     *
33
     * @var integer
34
     */
35
    protected $port;
36
37
    /**
38
     * Socket resource
39
     *
40
     * @var resource
41
     */
42
    protected $socket;
43
44
    /**
45
     * Returns the host attribute
46
     *
47
     * @return string
48
     */
49
    public function getHost()
50
    {
51
        return $this->host;
52
    }
53
54
    /**
55
     * Returns the port attribute
56
     *
57
     * @return integer
58
     */
59
    public function getPort()
60
    {
61
        return $this->port;
62
    }
63
64
    /**
65
     * Returns the socket attribute
66
     *
67
     * @return resource|boolean
68
     */
69
    public function getSocket()
70
    {
71
        return $this->socket;
72
    }
73
74
    /**
75
     * Sets host attribute
76
     *
77
     * @param string $host
78
     *
79
     * @return null
80
     */
81
    public function setHost($host)
82
    {
83
        $this->host = $host;
84
    }
85
86
    /**
87
     * Sets port attribute
88
     *
89
     * @param integer $port
90
     *
91
     * @return null
92
     */
93
    public function setPort($port)
94
    {
95
        $this->port = $port;
96
    }
97
98
    /**
99
     * Driver Constructor (here socket will be created!)
100
     *
101
     * All modifiable attributes (i.e. with setter method) can be passed as key
102
     *
103
     *
104
     * @param array $options
105
     * @throws RuntimeException
106
     */
107
    public function __construct($options)
108
    {
109
        foreach ($options as $option => $value) {
110
            if (property_exists(__CLASS__, strtolower($option)) && method_exists($this, 'set'.$option)) {
111
                $this->{'set'.$option}($value);
112
            }
113
        }
114
115
        if (!($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
116
            $errno = socket_last_error();
117
            $this->error($errno, socket_strerror($errno));
0 ignored issues
show
Bug introduced by
The method error() does not exist on Drone\Network\Socket\AbstractSocket. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

117
            $this->/** @scrutinizer ignore-call */ 
118
                   error($errno, socket_strerror($errno));
Loading history...
118
119
            throw new \RuntimeException("Could not create the socket");
120
        }
121
122
        $this->socket = $socket;
123
    }
124
125
    /**
126
     * Binds the socket
127
     *
128
     * @return bool
129
     */
130
    public function bind()
131
    {
132
        if (!($bind = @socket_bind($this->socket, $this->host, $this->port))) {
133
            $errno = socket_last_error();
134
            $this->error($errno, socket_strerror($errno));
135
136
            return false;
137
        }
138
139
        return $bind;
140
    }
141
142
    /**
143
     * Closes the socket
144
     *
145
     * @return void
146
     */
147
    public function close()
148
    {
149
        return socket_close($this->socket);
0 ignored issues
show
Bug introduced by
Are you sure the usage of socket_close($this->socket) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
150
    }
151
}
152