Conditions | 6 |
Paths | 13 |
Total Lines | 38 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 6.4859 |
Changes | 0 |
1 | <?php |
||
17 | protected function batch(Input $input): ?string |
||
18 | 16 | { |
|
19 | \assert($input->isArray()); |
||
20 | 16 | ||
21 | $children = []; |
||
22 | $responses = []; |
||
23 | foreach ($input->data() as $request) { |
||
24 | 16 | $pair = \stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); |
|
25 | |||
26 | 16 | $pid = \pcntl_fork(); |
|
27 | 16 | ||
28 | if (0 === $pid) { |
||
29 | 6 | \fclose($pair[0]); |
|
30 | |||
31 | 6 | \fwrite($pair[1], $this->single(Input::fromSafeData($request)) . "\n"); |
|
32 | |||
33 | 6 | \fclose($pair[1]); |
|
34 | 1 | ||
35 | exit(0); |
||
36 | } |
||
37 | 5 | ||
38 | 5 | \fclose($pair[1]); |
|
39 | 5 | ||
40 | 5 | $children[$pid] = $pair[0]; |
|
41 | } |
||
42 | 5 | ||
43 | while(-1 !== $pid = \pcntl_wait($status)) { |
||
44 | 5 | $socket = $children[$pid]; |
|
45 | |||
46 | if ('' !== $response = \trim(\fgets($socket))) { |
||
47 | $responses[] = $response; |
||
48 | } |
||
49 | |||
50 | \fclose($socket); |
||
51 | } |
||
52 | |||
53 | return empty($responses) ? |
||
54 | 5 | null : \sprintf('[%s]', \implode(',', $responses)); |
|
55 | } |
||
57 |