Completed
Push — master ( 411d94...8fae9a )
by John
02:59
created

SocketReader::read()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
ccs 16
cts 16
cp 1
cc 5
eloc 14
nc 7
nop 1
crap 5
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient;
14
15
use Socket\Raw\Socket;
16
17
class SocketReader
18
{
19
    /**
20
     * @param Socket $socket
21
     * @return string
22
     */
23 2
    public function read(Socket $socket)
24
    {
25 2
        $characterEtb = null;
26 2
        $buffer = '';
27 2
        while (true) {
28 2
            $character = $socket->read(1);
29
30
            // the character we terminate on depends on the first character in the response
31 2
            if (!isset($characterEtb)) {
32 2
                if ($character == "\x01") {
33 1
                    $characterEtb = "\x17";
34 1
                } else {
35 1
                    $characterEtb = "\n";
36
                }
37 2
            }
38
39 2
            $buffer .= $character;
40 2
            if ($character == $characterEtb) {
41 2
                break;
42
            }
43 2
        }
44
45 2
        return trim($buffer, "\x01\x17");
46
    }
47
}
48