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

Assuan::send()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\PinEntry\Assuan;
6
7
use BitWasp\PinEntry\Exception\RemotePinEntryException;
8
use BitWasp\PinEntry\Process\ProcessInterface;
9
use BitWasp\PinEntry\Response;
10
11
class Assuan
12
{
13
    public function parseResponse(ProcessInterface $process)
14
    {
15
        $data = [];
16
        $statuses = [];
17
        $comments = [];
18
        $okMsg = null;
19
20
        for (;;) {
21
            echo "call now\n";
22
            $buffer = $process->recv();
23
            var_dump("BUFFER", $buffer);
0 ignored issues
show
Security Debugging Code introduced by
var_dump('BUFFER', $buffer) looks like debug code. Are you sure you do not want to remove it?
Loading history...
24
            foreach (explode("\n", $buffer) as $piece) {
25
                if (substr($piece, 0, 4) === "ERR ") {
26
                    $c = explode(" ", $piece, 3);
27
                    // don't change state, it's only a single line
28
                    throw new RemotePinEntryException($c[2], (int)$c[1]);
29
                }
30
31
                $prefix = substr($piece, 0, 2);
32
                if ($prefix === "D ") {
33
                    $data[] = substr($piece, 2);
34
                } else if ($prefix === "S ") {
35
                    $statuses[] = substr($piece, 2);
36
                } else if ($prefix === "# ") {
37
                    // don't change state, it's only a single line
38
                    $comments[] = substr($piece, 2);
39
                } else if ($prefix === "OK") {
40
                    if (strlen($piece) > 2) {
41
                        $okMsg = substr($piece, 3);
42
                    }
43
                    break 2;
44
                }
45
            }
46
        }
47
48
        return new Response($data, $statuses, $comments, $okMsg);
49
    }
50
51
    public function send(ProcessInterface $process, string $command, string $params = null)
52
    {
53
        $process->send(sprintf(
54
            "%s%s",
55
            $command,
56
            $params ? " {$params}" : ""
57
        ));
58
    }
59
}
60