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

ProcOpen   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 34
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 25 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\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