Completed
Push — master ( f2ddd5...5f7cf8 )
by Sebastian
01:56
created

ProcOpen   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 36
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 26 2
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Cli.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Cli\Processor;
11
12
use RuntimeException;
13
use SebastianFeldmann\Cli\Command\Result;
14
use SebastianFeldmann\Cli\Processor;
15
16
/**
17
 * Class ProcOpen
18
 *
19
 * @package SebastianFeldmann\Cli
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/sebastianfeldmann/cli
22
 * @since   Class available since Release 0.9.0
23
 */
24
class ProcOpen implements Processor
25
{
26
    /**
27
     * Execute the command
28
     *
29
     * @param  string $cmd
30
     * @param  int[]  $acceptableExitCodes
31
     * @return \SebastianFeldmann\Cli\Command\Result
32
     */
33 3
    public function run(string $cmd, array $acceptableExitCodes = [0]) : Result
34
    {
35 3
        $old            = error_reporting(0);
36
        $descriptorSpec = [
37 3
            ['pipe', 'r'],
38
            ['pipe', 'w'],
39
            ['pipe', 'w'],
40
        ];
41
42 3
        $process = proc_open($cmd, $descriptorSpec, $pipes);
43 3
        if (!is_resource($process)) {
44
            throw new RuntimeException('can\'t execute \'proc_open\'');
45
        }
46 3
        fclose($pipes[0]);
47
48 3
        $stdOut = stream_get_contents($pipes[1]);
49 3
        fclose($pipes[1]);
50
51 3
        $stdErr = stream_get_contents($pipes[2]);
52 3
        fclose($pipes[2]);
53
54 3
        $code = proc_close($process);
55 3
        error_reporting($old);
56
57 3
        return new Result($cmd, $code, $stdOut, $stdErr, '', $acceptableExitCodes);
58
    }
59
}
60