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

Process   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 66
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 3 1
A recv() 0 3 1
A stopRunning() 0 5 1
A close() 0 6 2
A send() 0 3 1
A __destruct() 0 3 1
A __construct() 0 10 3
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