Process   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 76.67%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 70
ccs 23
cts 30
cp 0.7667
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 7 2
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 1
    public function __construct(string $executable, array $overrideDescriptors = [])
27
    {
28 1
        $pipes = [];
29 1
        $process = proc_open($executable, IPC::buildDescriptors($overrideDescriptors), $pipes);
30 1
        if (!(is_resource($process) && get_resource_type($process) === "process")) {
31
            throw new Exception\PinEntryException("Failed to start process");
32
        }
33
34 1
        $this->ipc = new IPC($pipes);
35 1
        $this->process = $process;
36 1
    }
37
38 1
    public function __destruct()
39
    {
40 1
        $this->close();
41 1
    }
42
43 1
    public function close(): bool
44
    {
45 1
        if ($this->running) {
46 1
            $this->stopRunning();
47
        }
48 1
        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 1
    private function stopRunning()
56
    {
57 1
        $this->ipc->close();
58 1
        proc_close($this->process);
59 1
        $this->running = false;
60 1
    }
61
62 1
    public function getStatus(): array
63
    {
64 1
        $status = proc_get_status($this->process);
65 1
        if (false === $status) {
66
            throw new \RuntimeException("Failed to get status information about process");
67
        }
68 1
        return $status;
69
    }
70
71
    public function send(string $data)
72
    {
73
        $this->ipc->send($data);
74
    }
75
76
    public function recv(): string
77
    {
78
        return $this->ipc->readStdOut();
79
    }
80
}
81