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

PinEntry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 85
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getVersion() 0 3 1
A getPin() 0 27 5
A getPID() 0 3 1
A getInfo() 0 11 2
A setOption() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\PinEntry;
6
7
use BitWasp\PinEntry\Assuan\Assuan;
8
use BitWasp\PinEntry\Exception\PinEntryException;
9
use BitWasp\PinEntry\PinValidation\PinValidatorInterface;
10
use BitWasp\PinEntry\Process\ProcessInterface;
11
12
class PinEntry
13
{
14
    /**
15
     * @var ProcessInterface
16
     */
17
    private $process;
18
19
    /**
20
     * @var Assuan
21
     */
22
    private $assuan;
23
24
    public function __construct(ProcessInterface $process, Assuan $assuan = null)
25
    {
26
        $response = $process->recv();
27
        if ($response !== "OK Pleased to meet you\n") {
28
            var_dump($response);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($response) looks like debug code. Are you sure you do not want to remove it?
Loading history...
29
            throw new PinEntryException("First message from pinentry did not match expected value");
30
        }
31
        $this->process = $process;
32
        $this->assuan = $assuan ?: new Assuan();
33
    }
34
35
    /**
36
     * @param string $key
37
     * @param int|string $value
38
     * @return mixed
39
     */
40
    public function setOption(string $key, $value): Response
41
    {
42
        $this->process->send(Command::OPTION . " {$key} {$value}\n");
43
        $msg = $this->assuan->parseResponse($this->process);
44
        return $msg;
45
    }
46
47
    public function getPID(): int
48
    {
49
        return (int) $this->getInfo('pid');
50
    }
51
52
    public function getVersion(): string
53
    {
54
        return $this->getInfo('version');
55
    }
56
57
    public function getInfo(string $type): string
58
    {
59
        $this->process->send(Command::GETINFO . " {$type}\n");
60
        $response = $this->assuan->parseResponse($this->process);
61
62
        if (empty($response->getData())) {
63
            throw new \RuntimeException("expecting info in response");
64
        }
65
        list ($result) = $response->getData();
66
67
        return $result;
68
    }
69
70
    public function getPin(PinRequest $request, PinValidatorInterface $pinValidator): string
71
    {
72
        foreach ($request->getCommands() as $command => $param) {
73
            echo "send cmd\n";
74
            $this->assuan->send($this->process, $command, $param);
75
            echo "get response\n";
76
            $this->assuan->parseResponse($this->process);
77
        }
78
79
        $error = null;
80
        $pin = '';
81
        while (!$pinValidator->validate($pin, $error)) {
82
            if ($pin !== '') {
83
                $this->assuan->send($this->process, Command::SETERROR, $error);
84
                $this->assuan->parseResponse($this->process);
85
            }
86
87
            $this->assuan->send($this->process, Command::GETPIN);
88
            $response = $this->assuan->parseResponse($this->process);
89
            if (empty($response->getData())) {
90
                throw new \RuntimeException("expecting pin in response");
91
            }
92
93
            list ($pin) = $response->getData();
94
        }
95
96
        return $pin;
97
    }
98
}
99