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

PopenWrapper::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
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