PosixSignals::setDefault()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php namespace Comodojo\Daemon\Utils;
2
3
use \Comodojo\Foundation\DataAccess\IteratorTrait;
4
use \Comodojo\Foundation\DataAccess\CountableTrait;
5
use \Comodojo\Foundation\DataAccess\ArrayAccessTrait;
6
use \Iterator;
7
use \Countable;
8
use \ArrayAccess;
9
10
/**
11
 * @package     Comodojo Daemon
12
 * @author      Marco Giovinazzi <[email protected]>
13
 * @license     MIT
14
 *
15
 * LICENSE:
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
class PosixSignals implements Iterator, Countable, ArrayAccess {
27
28
    use IteratorTrait;
29
    use CountableTrait;
30
    use ArrayAccessTrait;
31
32
    protected $data = [
33
        SIGHUP => 'SIGHUP',
34
        SIGINT => 'SIGINT',
35
        SIGQUIT => 'SIGQUIT',
36
        SIGILL => 'SIGILL',
37
        SIGTRAP => 'SIGTRAP',
38
        SIGABRT => 'SIGABRT',
39
        SIGIOT => 'SIGIOT',
40
        SIGBUS => 'SIGBUS',
41
        SIGFPE => 'SIGFPE',
42
        // Noone can catch SIGKILL!
43
        // SIGKILL => 'SIGKILL',
44
        SIGUSR1 => 'SIGUSR1',
45
        SIGSEGV => 'SIGSEGV',
46
        SIGUSR2 => 'SIGUSR2',
47
        SIGPIPE => 'SIGPIPE',
48
        SIGALRM => 'SIGALRM',
49
        SIGTERM => 'SIGTERM',
50
        SIGCHLD => 'SIGCHLD',
51
        SIGCONT => 'SIGCONT',
52
        SIGTSTP => 'SIGTSTP',
53
        SIGTTIN => 'SIGTTIN',
54
        SIGTTOU => 'SIGTTOU',
55
        SIGURG => 'SIGURG',
56
        SIGXCPU => 'SIGXCPU',
57
        SIGXFSZ => 'SIGXFSZ',
58
        SIGVTALRM => 'SIGVTALRM',
59
        SIGPROF => 'SIGPROF',
60
        SIGWINCH => 'SIGWINCH',
61
        SIGSYS => 'SIGSYS',
62
        SIGBABY => 'SIGBABY'
63
    ];
64
65
    protected $pointer = [];
66
67 1
    public function __construct() {
68
69 1
        if ( defined('SIGPOLL') ) $this->data[SIGPOLL] = 'SIGPOLL';
70 1
        if ( defined('SIGPWR') ) $this->data[SIGPWR] = 'SIGPWR';
71 1
        if ( defined('SIGSTKFLT') ) $this->data[SIGSTKFLT] = 'SIGSTKFLT';
72
        // if ( defined('SIGSTOP') ) $this->data[SIGSTOP] = 'SIGSTOP';
73 1
        if ( defined('SIGIO') ) $this->data[SIGIO] = 'SIGIO';
74 1
        if ( defined('SIGCLD') ) $this->data[SIGCLD] = 'SIGCLD';
75
76 1
    }
77
78
    public function sigNo($signame) {
79
80
        $reverse_signals = array_flip($this->data);
81
82
        return array_key_exists($signame, $reverse_signals) ? $reverse_signals[$signame] : null;
83
84
    }
85
86
    public function sigName($signo) {
87
88
        return array_key_exists($signo, $this->data) ? $this->data[$signo] : null;
89
90
    }
91
92
    public function on(...$signals) {
93
94
        foreach ( $signals as $signal ) {
95
            if ( !isset($this[$signal]) ) throw new Exception("Signal $signal not supported");
0 ignored issues
show
Bug introduced by
The type Comodojo\Daemon\Utils\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
96
        }
97
98
        $this->pointer = $signals;
99
100
        return $this;
101
102
    }
103
104 1
    public function any() {
105
106 1
        $this->pointer = [];
107
108 1
        return $this;
109
110
    }
111
112 1
    public function call($callable) {
113
114 1
        if ( empty($this->pointer) ) {
115 1
            $result = [];
116 1
            foreach ( $this as $signo => $signame ) {
117 1
                $result[] = $re = pcntl_signal($signo, $callable);
0 ignored issues
show
Bug introduced by
It seems like $signo can also be of type string; however, parameter $signo of pcntl_signal() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

117
                $result[] = $re = pcntl_signal(/** @scrutinizer ignore-type */ $signo, $callable);
Loading history...
118 1
                if ( !$re ) echo "\n>>>Signal $signo (".$this->sigName($signo).") failed\n";
119
            }
120 1
            return !in_array(false, $result);
121
        }
122
123
        return array_map(function($signal) {
124
            return pcntl_signal($signal, $callable);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $callable seems to be never defined.
Loading history...
125
        }, $this->pointer);
126
127
    }
128
129
    public function setDefault() {
130
131
        if ( empty($this->pointer) ) {
132
            $result = [];
133
            foreach ( $this as $signo => $signame ) {
134
                $result[] = pcntl_signal($signo, SIG_DFL);
0 ignored issues
show
Bug introduced by
It seems like $signo can also be of type string; however, parameter $signo of pcntl_signal() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

134
                $result[] = pcntl_signal(/** @scrutinizer ignore-type */ $signo, SIG_DFL);
Loading history...
135
            }
136
            return !in_array(false, $result);
137
        }
138
139
        return array_map(function($signal) {
140
            return pcntl_signal($signal, SIG_DFL);
141
        }, $this->pointer);
142
143
    }
144
145
    public function mask() {
146
147
        if ( empty($this->pointer) ) {
148
            return pcntl_sigprocmask(SIG_SETMASK, (array) $this);
149
        }
150
151
        return pcntl_sigprocmask(SIG_BLOCK, $this->pointer);
152
153
    }
154
155
    public function unmask() {
156
157
        if ( empty($this->pointer) ) {
158
            return pcntl_sigprocmask(SIG_SETMASK, []);
159
        }
160
161
        return pcntl_sigprocmask(SIG_UNBLOCK, $this->pointer);
162
163
    }
164
165
}
166