Completed
Push — 2.0 ( dd3689...ee0168 )
by Marco
12:16
created

Ipc::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Comodojo\Extender\Components;
2
3
use \Comodojo\Dispatcher\Components\Configuration;
4
use \Exception;
5
6
/**
7
 * Basic IPC implementation
8
 *
9
 * @package     Comodojo extender
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     GPL-3.0+
12
 *
13
 * LICENSE:
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27
 */
28
29
class Ipc {
30
31
    const READER = 0;
32
33
    const WRITER = 1;
34
35
    private $bytes;
36
37
    private $ipc = array();
38
39
    public function __construct(Configuration $configuration) {
40
41
        $this->bytes = self::getBytes($configuration->get('child-max-result-bytes'));
42
43
    }
44
45
    public function init($uid) {
46
47
        $this->ipc[$uid] = array();
48
49
        $socket = socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $this->ipc[$uid]);
50
51
        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...
52
53
        return $socket;
54
55
    }
56
57 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...
58
59
        $reader = $this->ipc[$uid][self::READER];
60
61
        $result = socket_read($reader, $this->bytes, PHP_BINARY_READ);
62
63
        if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($reader)));
64
65
        return $result;
66
67
    }
68
69 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...
70
71
        $writer = $this->ipc[$uid][self::WRITER];
72
73
        $result = socket_write($writer, $data, strlen($data));
74
75
        if ( $result === false ) throw new Exception("Socket error: ".socket_strerror(socket_last_error($writer)));
76
77
        return $result;
78
79
    }
80
81
    public function close($uid, $handler) {
82
83
        socket_close($this->ipc[$uid][$handler]);
84
85
    }
86
87
    public function hang($uid) {
88
89
        socket_close($this->ipc[$uid][self::READER]);
90
        socket_close($this->ipc[$uid][self::WRITER]);
91
92
    }
93
94
    public function free() {
95
96
        $this->ipc = array();
97
98
    }
99
100 View Code Duplication
    private static function getBytes($bytes = null) {
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...
101
102
        return filter_var($bytes, FILTER_VALIDATE_INT, array(
103
            'options' => array(
104
                'default' => 2048,
105
                'min_range' => 1024
106
            )
107
        ));
108
109
    }
110
111
}
112