Passed
Push — develop ( f821dc...ade763 )
by Manuele
13:55
created

Socket::read()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
/**
4
 * Votifier PHP Client
5
 *
6
 * @package   VotifierClient
7
 * @author    Manuele Vaccari <[email protected]>
8
 * @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]>
9
 * @license   https://github.com/D3strukt0r/votifier-client-php/blob/master/LICENSE.txt GNU General Public License v3.0
10
 * @link      https://github.com/D3strukt0r/votifier-client-php
11
 */
12
13
namespace D3strukt0r\VotifierClient\ServerType;
14
15
use D3strukt0r\VotifierClient\Exception\Socket\NoConnectionException;
16
use D3strukt0r\VotifierClient\Exception\Socket\PackageNotReceivedException;
17
use D3strukt0r\VotifierClient\Exception\Socket\PackageNotSentException;
18
19
/**
20
 * Creates a class for socket functionality.
21
 */
22
class Socket
23
{
24
    /**
25
     * @var resource the connection to the server
26
     */
27
    private $socket;
28
29
    /**
30
     * Closes the connection when the object is destroyed.
31
     */
32
    public function __destruct()
33
    {
34
        if (is_resource($this->socket)) {
35
            fclose($this->socket);
36
        }
37
    }
38
39
    /**
40
     * Creates a socket connection.
41
     *
42
     * @param string $host The hostname or IP address
43
     * @param int    $port The port of Votifier
44
     *
45
     * @throws NoConnectionException If connection couldn't be established
46
     */
47
    public function open(string $host, int $port): void
48
    {
49
        $socket = @fsockopen($host, $port, $errorNumber, $errorString, 3);
50
        if (false === $socket) {
51
            throw new NoConnectionException($errorString, $errorNumber);
52
        }
53
        $this->socket = $socket;
54
    }
55
56
    /**
57
     * Sends a string to the server.
58
     *
59
     * @param string $string The string to send
60
     *
61
     * @throws NoConnectionException   If connection has not been set up
62
     * @throws PackageNotSentException If there was an error sending the package
63
     */
64
    public function write(string $string): void
65
    {
66
        if (!is_resource($this->socket)) {
67
            throw new NoConnectionException();
68
        }
69
70
        if (false === fwrite($this->socket, $string)) {
71
            throw new PackageNotSentException();
72
        }
73
    }
74
75
    /**
76
     * Gets a string from the server.
77
     *
78
     * @param int $length The length of the string to be received
79
     *
80
     * @throws NoConnectionException       If connection has not been set up
81
     * @throws PackageNotReceivedException If there was an error receiving the package
82
     *
83
     * @return string returns the string received from the server
84
     */
85
    public function read(int $length = 64): string
86
    {
87
        if (!is_resource($this->socket)) {
88
            throw new NoConnectionException();
89
        }
90
91
        if (!$string = fread($this->socket, $length)) {
92
            throw new PackageNotReceivedException();
93
        }
94
95
        return $string;
96
    }
97
}
98