Process::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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