Completed
Push — master ( 53afc4...fbd21f )
by Sebastian
01:40
created

ProcOpen::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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
47 3
        $stdOut = stream_get_contents($pipes[1]);
48 3
        fclose($pipes[1]);
49
50 3
        $stdErr = stream_get_contents($pipes[2]);
51 3
        fclose($pipes[2]);
52
53 3
        $code = proc_close($process);
54 3
        error_reporting($old);
55
56 3
        return new Result($cmd, $code, $stdOut, $stdErr, '', $acceptableExitCodes);
57
    }
58
}
59