Completed
Pull Request — master (#3)
by thomas
17:44
created

Process::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\PinEntry\Process;
6
7
use BitWasp\PinEntry\Exception;
8
9
class Process implements ProcessInterface
10
{
11
    /**
12
     * @var resource A 'process' resource
13
     */
14
    private $process;
15
16
    /**
17
     * @var IPC -
18
     */
19
    private $ipc;
20
21
    /**
22
     * @var bool
23
     */
24
    private $running = true;
25
26
    public function __construct(string $executable, array $overrideDescriptors = [])
27
    {
28
        $pipes = [];
29
        $process = proc_open($executable, IPC::buildDescriptors($overrideDescriptors), $pipes);
30
        if (!(is_resource($process) && get_resource_type($process) === "process")) {
31
            throw new Exception\PinEntryException("Failed to start process");
32
        }
33
34
        $this->ipc = new IPC($pipes);
35
        $this->process = $process;
36
    }
37
38
    public function __destruct()
39
    {
40
        $this->close();
41
    }
42
43
    public function close(): bool
44
    {
45
        if ($this->running) {
46
            $this->stopRunning();
47
        }
48
        return true;
49
    }
50
51
    /**
52
     * Called during shutdown routine. Per documentation,
53
     * file handles should be closed before we call proc_close
54
     */
55
    private function stopRunning()
56
    {
57
        $this->ipc->close();
58
        proc_close($this->process);
59
        $this->running = false;
60
    }
61
62
    public function getStatus(): array
63
    {
64
        return proc_get_status($this->process);
0 ignored issues
show
Bug Best Practice introduced by
The expression return proc_get_status($this->process) could return the type false which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
65
    }
66
67
    public function send(string $data)
68
    {
69
        $this->ipc->send($data);
70
    }
71
72
    public function recv(): string
73
    {
74
        return $this->ipc->readStdOut();
75
    }
76
}
77