Ipc   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A hang() 0 4 1
A free() 0 3 1
A close() 0 3 1
A read() 0 9 2
A init() 0 9 2
A write() 0 9 2
A __construct() 0 7 1
1
<?php namespace Comodojo\Extender\Components;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Foundation\Validation\DataFilter;
5
use \Exception;
6
7
/**
8
 * @package     Comodojo Extender
9
 * @author      Marco Giovinazzi <[email protected]>
10
 * @license     MIT
11
 *
12
 * LICENSE:
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
23
class Ipc {
24
25
    const READER = 0;
26
27
    const WRITER = 1;
28
29
    private $bytes;
30
31
    private $ipc = [];
32
33
    public function __construct(Configuration $configuration) {
34
35
        $this->bytes = DataFilter::filterInteger(
36
            $configuration->get('child-max-result-bytes'),
37
            1024,
0 ignored issues
show
Bug introduced by
1024 of type integer is incompatible with the type array expected by parameter $min of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            /** @scrutinizer ignore-type */ 1024,
Loading history...
38
            PHP_INT_MAX,
0 ignored issues
show
Bug introduced by
Comodojo\Extender\Components\PHP_INT_MAX of type integer is incompatible with the type array expected by parameter $max of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            /** @scrutinizer ignore-type */ PHP_INT_MAX,
Loading history...
39
            16384
0 ignored issues
show
Bug introduced by
16384 of type integer is incompatible with the type array expected by parameter $default of Comodojo\Foundation\Vali...Filter::filterInteger(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            /** @scrutinizer ignore-type */ 16384
Loading history...
40
        );
41
42
    }
43
44
    public function init($uid) {
45
46
        $this->ipc[$uid] = [];
47
48
        $socket = socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $this->ipc[$uid]);
49
50
        if ( $socket == false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error()));
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
51
52
        return $socket;
53
54
    }
55
56
    public function read($uid) {
57
58
        $reader = $this->ipc[$uid][self::READER];
59
60
        $result = socket_read($reader, $this->bytes, PHP_BINARY_READ);
61
62
        if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($reader)));
63
64
        return $result;
65
66
    }
67
68
    public function write($uid, $data) {
69
70
        $writer = $this->ipc[$uid][self::WRITER];
71
72
        $result = socket_write($writer, $data, strlen($data));
73
74
        if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($writer)));
75
76
        return $result;
77
78
    }
79
80
    public function close($uid, $handler) {
81
82
        socket_close($this->ipc[$uid][$handler]);
83
84
    }
85
86
    public function hang($uid) {
87
88
        socket_close($this->ipc[$uid][self::READER]);
89
        socket_close($this->ipc[$uid][self::WRITER]);
90
91
    }
92
93
    public function free() {
94
95
        $this->ipc = [];
96
97
    }
98
99
}
100