ChunkedSocketChannel::fread()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 22
ccs 0
cts 20
cp 0
crap 30
rs 9.4888
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * soluble-japha / PHPJavaBridge driver client.
6
 *
7
 * Refactored version of phpjababridge's Java.inc file compatible
8
 * with php java bridge 6.2
9
 *
10
 *
11
 * @credits   http://php-java-bridge.sourceforge.net/pjb/
12
 *
13
 * @see      http://github.com/belgattitude/soluble-japha
14
 *
15
 * @author Jost Boekemeier
16
 * @author Vanvelthem Sébastien (refactoring and fixes from original implementation)
17
 * @license   MIT
18
 *
19
 * The MIT License (MIT)
20
 * Copyright (c) 2014-2017 Jost Boekemeier
21
 * Permission is hereby granted, free of charge, to any person obtaining a copy
22
 * of this software and associated documentation files (the "Software"), to deal
23
 * in the Software without restriction, including without limitation the rights
24
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25
 * copies of the Software, and to permit persons to whom the Software is
26
 * furnished to do so, subject to the following conditions:
27
 *
28
 * The above copyright notice and this permission notice shall be included in
29
 * all copies or substantial portions of the Software.
30
 *
31
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37
 * THE SOFTWARE.
38
 */
39
40
namespace Soluble\Japha\Bridge\Driver\Pjb62;
41
42
use Soluble\Japha\Bridge\Driver\Pjb62\Exception\BrokenConnectionException;
43
use Soluble\Japha\Bridge\Exception;
44
45
class ChunkedSocketChannel extends SocketChannel
46
{
47
    /**
48
     * @throws Exception\RuntimeException
49
     *
50
     * @param string $data
51
     */
52
    public function fwrite(string $data): int
53
    {
54
        $len = dechex(strlen($data));
55
        $written = fwrite($this->peer, "${len}\r\n${data}\r\n");
56
        if (!$written) {
57
            $msg = 'Cannot write to socket';
58
            throw new Exception\RuntimeException($msg);
59
        }
60
61
        return $written;
62
    }
63
64
    /**
65
     * @throws BrokenConnectionException
66
     */
67
    public function fread(int $size): ?string
68
    {
69
        $line = fgets($this->peer, $this->recv_size);
70
        if ($line === false) {
71
            throw new BrokenConnectionException(
72
                'Cannot read from socket'
73
            );
74
        }
75
76
        $length = (int) hexdec($line);
77
        $data = '';
78
        while ($length > 0) {
79
            $str = fread($this->peer, $length);
80
            if (feof($this->peer) || $str === false) {
81
                return null;
82
            }
83
            $length -= strlen($str);
84
            $data .= $str;
85
        }
86
        fgets($this->peer, 3);
87
88
        return $data;
89
    }
90
91
    public function keepAlive(): void
92
    {
93
        $this->keepAliveSC();
94
        $this->checkE();
95
        fclose($this->peer);
96
    }
97
}
98