Completed
Push — master ( e2a90b...0f1023 )
by Sebastian
09:28
created

Process::getRedirectPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace phpbu\App\Cli;
3
4
use phpbu\App\Exception;
5
6
/**
7
 * Cli Process Runner
8
 *
9
 * @package    phpbu
10
 * @subpackage Backup
11
 * @author     Sebastian Feldmann <[email protected]>
12
 * @copyright  Sebastian Feldmann <[email protected]>
13
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
14
 * @link       http://phpbu.de/
15
 * @since      Class available since Release 2.1.0
16
 */
17
class Process
18
{
19
    /**
20
     * List of system commands to execute.
21
     *
22
     * @var array<\phpbu\Backup\Cli\Cmd>
23
     */
24
    private $commands = [];
25
26
    /**
27
     * Redirect the output
28
     *
29
     * @var string
30
     */
31
    private $redirectOutput;
32
33
    /**
34
     * Redirect the stdOut.
35
     *
36
     * @param string $path
37
     */
38 12
    public function redirectOutputTo($path)
39
    {
40 12
        $this->redirectOutput = $path;
41 12
    }
42
43
    /**
44
     * Should the output be redirected.
45
     *
46
     * @return boolean
47
     */
48 1
    public function isOutputRedirected()
49
    {
50 1
        return !empty($this->redirectOutput);
51
    }
52
53
    /**
54
     * Redirect getter.
55
     *
56
     * @return string
57
     */
58 1
    public function getRedirectPath()
59
    {
60 1
        return $this->redirectOutput;
61
    }
62
63
    /**
64
     * Adds a cli command to the command list.
65
     *
66
     * @param \phpbu\App\Cli\Cmd $cmd
67
     */
68 99
    public function addCommand(Cmd $cmd)
69
    {
70 99
        $this->commands[] = $cmd;
71 99
    }
72
73
    /**
74
     * Generates the system command.
75
     *
76
     * @return string
77
     * @throws \phpbu\App\Exception
78
     */
79 97
    public function getCommandLine()
80
    {
81 97
        $amount = count($this->commands);
82 97
        if ($amount < 1) {
83 1
            throw new Exception('no command to execute');
84
        }
85 96
        $cmd = ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0])
86 96
             . (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : '');
87
88 96
        return $cmd;
89
    }
90
91
    /**
92
     * Executes the commands.
93
     *
94
     * @return \phpbu\App\Cli\Result
95
     * @throws \phpbu\App\Exception
96
     */
97 1
    public function run()
98
    {
99 1
        $cmd            = $this->getCommandLine();
100 1
        $old            = error_reporting(0);
101 1
        $descriptorSpec = [
102 1
            ['pipe', 'r'],
103 1
            ['pipe', 'w'],
104 1
            ['pipe', 'w'],
105
        ];
106 1
107
        $process = proc_open($cmd, $descriptorSpec, $pipes);
108
        if (!is_resource($process)) {
109
            throw new Exception('can\'t execute \'proc_open\'');
110
        }
111
112
        $stdOut = stream_get_contents($pipes[1]);
113
        fclose($pipes[1]);
114
115
        $stdErr = stream_get_contents($pipes[2]);
116
        fclose($pipes[2]);
117
118
        $code = proc_close($process);
119
        //exec($cmd, $output, $code);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
        error_reporting($old);
121
122
        return new Result($cmd, $code, $stdOut, $stdErr);
123
    }
124
}
125