SignalHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 22
dl 0
loc 48
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setWorker() 0 3 1
A register() 0 9 2
A __construct() 0 8 4
A setDispatcher() 0 3 1
A unregister() 0 3 1
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