Completed
Push — 2.0 ( 76e968...4129d9 )
by Marco
13:01
created

Ipc::getBytes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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
 * Basic IPC implementation
9
 *
10
 * @package     Comodojo extender
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     GPL-3.0+
13
 *
14
 * LICENSE:
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 */
29
30
class Ipc {
31
32
    const READER = 0;
33
34
    const WRITER = 1;
35
36
    private $bytes;
37
38
    private $ipc = [];
39
40
    public function __construct(Configuration $configuration) {
41
42
        $this->bytes = DataFilter::filterInteger(
43
            $configuration->get('child-max-result-bytes'),
44
            1024,
45
            PHP_INT_MAX,
46
            2048
47
        );
48
49
    }
50
51
    public function init($uid) {
52
53
        $this->ipc[$uid] = [];
54
55
        $socket = socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $this->ipc[$uid]);
56
57
        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...
58
59
        return $socket;
60
61
    }
62
63 View Code Duplication
    public function read($uid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
65
        $reader = $this->ipc[$uid][self::READER];
66
67
        $result = socket_read($reader, $this->bytes, PHP_BINARY_READ);
68
69
        if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($reader)));
70
71
        return $result;
72
73
    }
74
75 View Code Duplication
    public function write($uid, $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
77
        $writer = $this->ipc[$uid][self::WRITER];
78
79
        $result = socket_write($writer, $data, strlen($data));
80
81
        if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($writer)));
82
83
        return $result;
84
85
    }
86
87
    public function close($uid, $handler) {
88
89
        socket_close($this->ipc[$uid][$handler]);
90
91
    }
92
93
    public function hang($uid) {
94
95
        socket_close($this->ipc[$uid][self::READER]);
96
        socket_close($this->ipc[$uid][self::WRITER]);
97
98
    }
99
100
    public function free() {
101
102
        $this->ipc = [];
103
104
    }
105
106
}
107