Server::read()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 6
rs 10
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
 * Server class
15
 *
16
 * Server socket implementation
17
 */
18
class Server extends AbstractSocket
19
{
20
    /**
21
     * Reads a message from client
22
     *
23
     * @param resource
24
     * @param mixed $socket
25
     *
26
     * @throws RuntimeException
27
     *
28
     * @return string
29
     */
30
    public function read($socket)
31
    {
32
        if (($message = @socket_read($socket, 1024)) === false) {
33
            $errno = socket_last_error();
34
            $this->error($errno, socket_strerror($errno));
0 ignored issues
show
Bug introduced by
The method error() does not exist on Drone\Network\Socket\Server. 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

34
            $this->/** @scrutinizer ignore-call */ 
35
                   error($errno, socket_strerror($errno));
Loading history...
35
36
            throw new \RuntimeException("Could not read message from client socket");
37
        }
38
39
        return $message;
40
    }
41
42
    /**
43
     * Sends a message to client socket
44
     *
45
     * @param resource $socket
46
     * @param string $message
47
     *
48
     * @throws RuntimeException
49
     *
50
     * @return integer
51
     */
52
    public function send($socket, $message)
53
    {
54
        if (($bytes = @socket_write($socket, $message, strlen($message))) === false) {
55
            $errno = socket_last_error();
56
            $this->error($errno, socket_strerror($errno));
57
58
            throw new \RuntimeException("Could not send message to the client socket");
59
        }
60
61
        return $bytes;
62
    }
63
64
    /**
65
     * Sets socket to listening
66
     *
67
     * @param array $eventHandlers
68
     *
69
     * @throws RuntimeException
70
     *
71
     * @return boolean
72
     */
73
    public function listen(array $eventHandlers = [])
74
    {
75
        $event = $eventHandlers;
76
        $clousure = function () {
77
        };
78
79
        if (!array_key_exists('success', $event)) {
80
            $event["success"] = $clousure;
81
        }
82
83
        if (!array_key_exists('error', $event)) {
84
            $event["error"] = $clousure;
85
        }
86
87
        $listener = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $listener is dead and can be removed.
Loading history...
88
89
        if (!($listener = @socket_listen($this->socket, 30))) {
90
            $errno = socket_last_error();
91
            $this->error($errno, socket_strerror($errno));
92
93
            throw new \RuntimeException("Could not set socket to listen");
94
        } else {
95
            echo "\n";
96
            echo "Server Started : " . date('Y-m-d H:i:s') . "\n";
97
            echo "Master socket  : " . $this->socket . "\n";
0 ignored issues
show
Bug introduced by
Are you sure $this->socket of type resource can be used in concatenation? ( Ignorable by Annotation )

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

97
            echo "Master socket  : " . /** @scrutinizer ignore-type */ $this->socket . "\n";
Loading history...
98
            echo "Listening on   : " . $this->host . " port " . $this->port . "\n\n";
99
100
            if (!($spawn = @socket_accept($this->socket))) {
101
                $errno = socket_last_error();
102
                $this->error($errno, socket_strerror($errno));
103
104
                throw new \RuntimeException("Could not accept incoming connection");
105
            }
106
107
            $input = $this->read($spawn);
108
109
            $input = trim($input);
110
            call_user_func($event["success"], $input, $this, $spawn);
111
112
            socket_close($spawn);
113
        }
114
115
        if (!$listener) {
0 ignored issues
show
introduced by
The condition $listener is always true.
Loading history...
116
            call_user_func($event["error"], $this->getLastError());
117
        }
118
119
        return $listener;
120
    }
121
}
122