Connection::getIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php namespace Comodojo\Daemon\Socket;
2
3
use \Comodojo\Exception\SocketException;
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
class Connection {
22
23
    protected $socket;
24
25
    protected $index;
26
27
    public function __construct($socket, $i) {
28
29
        $this->index = $i;
30
        $this->socket = @socket_accept($socket);
31
32
        if ( $this->socket === false ) {
33
            $error = self::checkConnectionError($socket);
34
            if ( $error !== true ) {
35
                throw new SocketException("Cannot put socket in listening mode: $error");
36
            }
37
        }
38
39
    }
40
41
    public function getIndex() {
42
        return $this->index;
43
    }
44
45
    public function getSocket() {
46
        return $this->socket;
47
    }
48
49
    public function destroy() {
50
        socket_close($this->socket);
51
    }
52
53
    protected static function checkConnectionError($socket) {
54
55
        $err_code = socket_last_error($socket);
56
        $err_string = socket_strerror($err_code);
57
58
        if ( $err_code === 11 || strtolower($err_string) == 'success' ) return true;
59
60
        return $err_string;
61
62
    }
63
64
}
65