Passed
Push — master ( 4bebbb...d78a10 )
by Sébastien
02:34
created

SocketChannel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 51.85%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 74
ccs 14
cts 27
cp 0.5185
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fwrite() 0 13 2
A fread() 0 12 2
A shutdownBrokenConnection() 0 5 2
A __construct() 0 6 1
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\Exception\BrokenConnectionException;
41
42
abstract class SocketChannel extends EmptyChannel
43
{
44
    /**
45
     * @var resource
46
     */
47
    public $peer;
48
49
    /**
50
     * @var string
51
     */
52
    public $host;
53
54
    /**
55
     * @var int
56
     */
57
    protected $recv_size;
58
59
    /**
60
     * @var int
61
     */
62
    protected $send_size;
63
64
    /**
65
     * @param resource $peer
66
     * @param string   $host
67
     * @param int      $recv_size
68
     * @param int      $send_size
69
     */
70 9
    public function __construct($peer, $host, $recv_size, $send_size)
71
    {
72 9
        $this->peer = $peer;
73 9
        $this->host = $host;
74 9
        $this->recv_size = $recv_size;
75 9
        $this->send_size = $send_size;
76 9
    }
77
78
    /**
79
     * @throws BrokenConnectionException
80
     */
81 117
    public function fwrite(string $data): ?int
82
    {
83 117
        $written = @fwrite($this->peer, $data);
84 117
        if ($written === false) {
85
            PjbProxyClient::unregisterAndThrowBrokenConnectionException(
86
                sprintf(
87
                    'Broken socket communication with the php-java-bridge while reading socket (%s).',
88
                    __METHOD__
89
                )
90
            );
91
        }
92
93 117
        return $written;
94
    }
95
96 117
    public function fread(int $size): ?string
97
    {
98 117
        $read = @fread($this->peer, $size);
99 117
        if ($read === false) {
100
            PjbProxyClient::unregisterAndThrowBrokenConnectionException(
101
                sprintf(
102
                    'Broken socket communication with the php-java-bridge while reading socket (%s).',
103
                    __METHOD__
104
                )
105
            );
106
        } else {
107 117
            return $read;
108
        }
109
    }
110
111
    public function shutdownBrokenConnection(?string $msg = ''): void
112
    {
113
        if (is_resource($this->peer)) {
114
            fflush($this->peer);
115
            fclose($this->peer);
116
        }
117
    }
118
}
119