Completed
Push — master ( 2e789b...0960c9 )
by Pierre
03:22
created

Process::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0354

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 3
nop 0
dl 0
loc 23
ccs 16
cts 19
cp 0.8421
crap 3.0354
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace App\Component\Console;
4
5
class Process
6
{
7
8
    /**
9
     * error
10
     *
11
     * @var Boolean
12
     */
13
    private $error;
14
15
    /**
16
     * error message
17
     *
18
     * @var String
19
     */
20
    private $errorMesage;
21
22
    /**
23
     * command to execute
24
     *
25
     * @var String
26
     */
27
    private $command;
28
29
    /**
30
     * execute ouput
31
     *
32
     * @var String
33
     */
34
    private $result;
35
36
37
    /**
38
     * instanciate
39
     */
40 8
    public function __construct()
41
    {
42 8
        $this->reset();
43
    }
44
45
    /**
46
     * set command to be executed
47
     *
48
     * @param string $command
49
     * @return Process
50
     */
51 2
    public function setCommand(string $command): Process
52
    {
53 2
        $this->reset();
54 2
        $this->command = $command;
55 2
        return $this;
56
    }
57
58
    /**
59
     * return string result for a given system command
60
     *
61
     * @param string $command
62
     * @return string
63
     */
64 1
    public function run(): Process
65
    {
66 1
        if (!\function_exists('proc_open')) {
67
            $this->error = true;
68
            $this->errorMesage = 'undefined function proc_open';
69
            return null;
70
        }
71 1
        $this->error = true;
72 1
        $this->errorMesage = 'process is not a resource';
73 1
        $process = proc_open(
74 1
            $this->command,
75 1
            $this->getDescriptors(),
76 1
            $pipes
77
        );
78 1
        if (is_resource($process)) {
79 1
            $this->result = stream_get_contents($pipes[1]);
80 1
            $this->errorMesage = stream_get_contents($pipes[2]);
81 1
            $this->error = !empty($this->errorMesage);
82 1
            fclose($pipes[1]);
83 1
            fclose($pipes[2]);
84 1
            proc_close($process);
85
        }
86 1
        return $this;
87
    }
88
89
    /**
90
     * return true if error
91
     *
92
     * @return boolean
93
     */
94 2
    public function isError(): bool
95
    {
96 2
        return $this->error === true;
97
    }
98
99
    /**
100
     * returns error message
101
     *
102
     * @return string
103
     */
104 1
    public function getErrorMessage(): string
105
    {
106 1
        return $this->errorMesage;
107
    }
108
109
    /**
110
     * return execute result as string
111
     *
112
     * @return string
113
     */
114 2
    public function __toString()
115
    {
116 2
        return $this->result;
117
    }
118
119
    /**
120
     * reset errors
121
     *
122
     * @return Process
123
     */
124 1
    protected function reset(): Process
125
    {
126 1
        $this->error = false;
127 1
        $this->errorMesage = '';
128 1
        $this->result = '';
129 1
        return $this;
130
    }
131
132
    /**
133
     * return pipes descriptors
134
     *
135
     * @return array
136
     */
137 1
    protected function getDescriptors(): array
138
    {
139
        return [
140 1
            ['pipe', 'r'], // stdin
141
            ['pipe', 'w'], // stdout
142
            ['pipe', 'w'], // stderr
143
        ];
144
    }
145
}
146