Passed
Push — master ( 550b9e...103607 )
by Sébastien
09:49 queued 07:35
created

HttpTunnelHandler::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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