Passed
Push — main ( e933de...5ef56a )
by Daniel
03:35
created

PopenWrapper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Io\Lib;
6
7
/**
8
 * Wraps php popen-command as it is tricky to test
9
 */
10
final class PopenWrapper implements PopenWrapperInterface
11
{
12
    /**
13
     * Runs a shell command
14
     *
15
     * If the execution succeeds, the resource will be closed on shutdown
16
     *
17
     * @param string $command Command to execute (ensure the arguments get escaped correctly!)
18
     *
19
     * @return resource|null
20
     *
21
     * @codeCoverageIgnore
22
     */
23
    public function run(string $command): mixed
24
    {
25
        $handle = popen('nice -n 19 '.$command, 'r');
26
27
        if ($handle === false) {
28
            return null;
29
        }
30
31
        register_shutdown_function(static fn () => fclose($handle));
32
33
        return $handle;
34
    }
35
}
36