Total Complexity | 7 |
Total Lines | 70 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
12 | class GracefulShutdown |
||
13 | { |
||
14 | /** |
||
15 | * @var int|null |
||
16 | */ |
||
17 | protected ?int $signal = null; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected array $signals; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected bool $debug; |
||
28 | |||
29 | /** |
||
30 | * GracefulShutdown constructor |
||
31 | * |
||
32 | * @param array $signals |
||
33 | * @param bool $debug |
||
34 | */ |
||
35 | public function __construct( |
||
36 | array $signals = [SIGTERM, SIGINT, SIGUSR1, SIGUSR2, SIGQUIT, SIGHUP], |
||
37 | bool $debug = false |
||
38 | ) { |
||
39 | $this->signals = $signals; |
||
40 | $this->debug = $debug; |
||
41 | $this->registerSignals(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param int $signal |
||
46 | */ |
||
47 | public function __invoke(int $signal) |
||
48 | { |
||
49 | $this->debug("##################### ---> Signal received: [{$signal}]"); |
||
50 | $this->signal = $signal; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Check if a signal was sent |
||
55 | * |
||
56 | * @return bool |
||
57 | */ |
||
58 | public function signalReceived(): bool |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Enable async signals and register |
||
65 | */ |
||
66 | protected function registerSignals(): void |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $string |
||
77 | */ |
||
78 | protected function debug(string $string): void |
||
82 | } |
||
83 | } |
||
85 |