Completed
Pull Request — master (#2)
by thomas
02:17
created

DeviceService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 38
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A provideCurrentPin() 0 8 1
A confirmWithButton() 0 9 2
A checkPinRequestType() 0 5 2
A provideCurrentPassphrase() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Trezor\Device\Command;
6
7
use BitWasp\Trezor\Device\Message;
8
use BitWasp\Trezor\Device\UserInput\CurrentPassphraseInputInterface;
9
use BitWasp\Trezor\Device\UserInput\CurrentPinInputInterface;
10
use BitWasp\TrezorProto\ButtonAck;
11
use BitWasp\TrezorProto\ButtonRequest;
12
use BitWasp\TrezorProto\MessageType;
13
use BitWasp\TrezorProto\PassphraseAck;
14
use BitWasp\TrezorProto\PinMatrixAck;
15
use BitWasp\TrezorProto\PinMatrixRequest;
16
use BitWasp\TrezorProto\PinMatrixRequestType;
17
18
abstract class DeviceService
19
{
20 1
    protected function checkPinRequestType(PinMatrixRequest $pinRequest, int $expectedType)
21
    {
22 1
        if ($pinRequest->getType()->value() !== $expectedType) {
23
            $ourType = PinMatrixRequestType::valueOf($expectedType);
24
            throw new \Exception("Unexpected pin matrix type (was {$pinRequest->getType()->name()}, not expected type {$ourType->name()}");
25
        }
26 1
    }
27
28 1
    protected function confirmWithButton(ButtonRequest $request, int $expectType): Message
29
    {
30 1
        $theirType = $request->getCode();
31 1
        if ($theirType->value() !== $expectType) {
32
            $ourType = MessageType::valueOf($expectType)->name();
33
            throw new \RuntimeException("Unexpected button request (expected: {$ourType}, got {$theirType->name()}");
34
        }
35
36 1
        return Message::buttonAck(new ButtonAck());
37
    }
38
39 1
    protected function provideCurrentPin(PinMatrixRequest $proto, CurrentPinInputInterface $currentPinInput): Message
40
    {
41 1
        $this->checkPinRequestType($proto, PinMatrixRequestType::PinMatrixRequestType_Current_VALUE);
42
43 1
        $pinMatrixAck = new PinMatrixAck();
44 1
        $pinMatrixAck->setPin($currentPinInput->getPin());
45
46 1
        return Message::pinMatrixAck($pinMatrixAck);
47
    }
48
49
50 1
    protected function provideCurrentPassphrase(CurrentPassphraseInputInterface $passphraseInput): Message
51
    {
52 1
        $passphraseAck = new PassphraseAck();
53 1
        $passphraseAck->setPassphrase($passphraseInput->getPassphrase());
54
55 1
        return Message::passphraseAck($passphraseAck);
56
    }
57
}
58