DeviceService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 36
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A provideCurrentPin() 0 8 1
A confirmWithButton() 0 8 2
A checkPinRequestType() 0 4 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\ButtonRequestType;
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 7
    protected function checkPinRequestType(PinMatrixRequest $pinRequest, PinMatrixRequestType $requestType)
21
    {
22 7
        if ($pinRequest->getType()->value() !== $requestType->value()) {
23 1
            throw new \RuntimeException("Unexpected pin matrix type (was {$pinRequest->getType()->name()}, not expected type {$requestType->name()})");
24
        }
25 6
    }
26
27 7
    protected function confirmWithButton(ButtonRequest $request, ButtonRequestType $buttonType): Message
28
    {
29 7
        $theirType = $request->getCode();
30 7
        if ($theirType->value() !== $buttonType->value()) {
31 1
            throw new \RuntimeException("Unexpected button request (expected: {$buttonType->name()}, got {$theirType->name()})");
32
        }
33
34 6
        return Message::buttonAck(new ButtonAck());
35
    }
36
37 7
    protected function provideCurrentPin(PinMatrixRequest $proto, CurrentPinInputInterface $currentPinInput): Message
38
    {
39 7
        $this->checkPinRequestType($proto, PinMatrixRequestType::PinMatrixRequestType_Current());
40
41 6
        $pinMatrixAck = new PinMatrixAck();
42 6
        $pinMatrixAck->setPin($currentPinInput->getPin());
43
44 6
        return Message::pinMatrixAck($pinMatrixAck);
45
    }
46
47
48 1
    protected function provideCurrentPassphrase(CurrentPassphraseInputInterface $passphraseInput): Message
49
    {
50 1
        $passphraseAck = new PassphraseAck();
51 1
        $passphraseAck->setPassphrase($passphraseInput->getPassphrase());
52
53 1
        return Message::passphraseAck($passphraseAck);
54
    }
55
}
56