Client::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 0
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
 * Client class
15
 *
16
 * Client socket implementation
17
 */
18
class Client extends AbstractSocket
19
{
20
    /**
21
     * Connects to socket server
22
     *
23
     * @return boolean
24
     */
25
    public function connect()
26
    {
27
        if (!($connected = @socket_connect($this->socket, $this->host, $this->port))) {
28
            $errno = socket_last_error();
29
            $this->error(socket_last_error(), socket_strerror($errno));
0 ignored issues
show
Bug introduced by
The method error() does not exist on Drone\Network\Socket\Client. 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

29
            $this->/** @scrutinizer ignore-call */ 
30
                   error(socket_last_error(), socket_strerror($errno));
Loading history...
30
31
            return false;
32
        }
33
34
        return $connected;
35
    }
36
37
    /**
38
     * Reads a message from server
39
     *
40
     * @return string|boolean
41
     */
42
    public function read()
43
    {
44
        if (($message = @socket_read($this->socket, 1024)) === false) {
45
            $errno = socket_last_error();
46
            $this->error(socket_last_error(), socket_strerror($errno));
47
48
            return false;
49
        }
50
51
        return $message;
52
    }
53
54
    /**
55
     * Sends a message to server socket
56
     *
57
     * @param string $message
58
     *
59
     * @return integer|boolean
60
     */
61
    public function send($message)
62
    {
63
        if (($bytes = @socket_write($this->socket, $message, strlen($message))) === false) {
64
            $errno = socket_last_error();
65
            $this->error(socket_last_error(), socket_strerror($errno));
66
67
            return false;
68
        }
69
70
        return $bytes;
71
    }
72
}
73