SignalHandler::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Resque;
6
7
use Resque\Dispatchers\Noop;
8
use Resque\Interfaces\Dispatcher;
9
use Resque\Tasks\BeforeSignalsRegister;
10
11
class SignalHandler
12
{
13
    private $worker = null;
14
    private $dispatcher;
15
16
    private $signals = [
17
        SIGTERM => 'shutdown',
18
        SIGINT => 'shutdown',
19
        SIGQUIT => 'shutdown',
20
        SIGUSR1 => 'pause',
21
        SIGCONT => 'continue',
22
        SIGUSR2 => 'forceShutdown',
23
    ];
24
25 3
    public function __construct(array $signals = [])
26
    {
27 3
        foreach ($signals as $type => $signalCallback) {
28 1
            if (in_array($type, array_keys($this->signals)) && is_callable($signalCallback)) {
29 1
                $this->signals[$type] = $signalCallback;
30
            }
31
        }
32 3
        $this->dispatcher = new Noop();
33 3
    }
34
35 2
    public function setDispatcher(Dispatcher $dispatcher): void
36
    {
37 2
        $this->dispatcher = $dispatcher;
38 2
    }
39
40 10
    public function setWorker(Worker $worker): void
41
    {
42 10
        $this->worker = $worker;
43 10
    }
44
45 2
    public function register(): void
46
    {
47 2
        $payload = $this->dispatcher->dispatch(BeforeSignalsRegister::class, ['signals' => $this->signals]);
48 2
        pcntl_async_signals(true);
49 2
        foreach ($payload['signals'] as $signalType => $signalHandler) {
50 2
            pcntl_signal($signalType, [$this->worker, $signalHandler]);
51
        }
52
53 2
        register_shutdown_function([$this, 'unregister']);
54 2
    }
55
56 1
    public function unregister(): void
57
    {
58 1
        pcntl_async_signals(false);
59 1
    }
60
}
61