Completed
Push — master ( 5735e1...ecb9c5 )
by Sebastian
02:27
created

ProcOpen::run()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0017

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 13
cp 0.9231
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 2
nop 1
crap 2.0017
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\Process;
11
12
use RuntimeException;
13
use SebastianFeldmann\Cli\Command\Result;
14
use SebastianFeldmann\Cli\Process;
15
16
/**
17
 * Class ProcOpen
18
 *
19
 * @package SebastianFeldmann\Cli
20
 */
21
class ProcOpen implements Process
22
{
23
    /**
24
     * Execute the command.
25
     *
26
     * @param  string $cmd
27
     * @return \SebastianFeldmann\Cli\Command\Result
28
     */
29 3
    public function run(string $cmd) : Result
30
    {
31 3
        $old            = error_reporting(0);
32
        $descriptorSpec = [
33 3
            ['pipe', 'r'],
34
            ['pipe', 'w'],
35
            ['pipe', 'w'],
36
        ];
37
38 3
        $process = proc_open($cmd, $descriptorSpec, $pipes);
39 3
        if (!is_resource($process)) {
40
            throw new RuntimeException('can\'t execute \'proc_open\'');
41
        }
42
43 3
        $stdOut = stream_get_contents($pipes[1]);
44 3
        fclose($pipes[1]);
45
46 3
        $stdErr = stream_get_contents($pipes[2]);
47 3
        fclose($pipes[2]);
48
49 3
        $code = proc_close($process);
50 3
        error_reporting($old);
51
52 3
        return new Result($cmd, $code, $stdOut, $stdErr);
53
    }
54
}
55