PingService::call()   B
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 32
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 13
cts 13
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 12
nc 24
nop 4
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Trezor\Device\Command;
6
7
use BitWasp\Trezor\Bridge\Session;
8
use BitWasp\Trezor\Device\Exception\IncorrectNonceException;
9
use BitWasp\Trezor\Device\Exception\UnexpectedResultException;
10
use BitWasp\Trezor\Device\Message;
11
use BitWasp\Trezor\Device\UserInput\CurrentPassphraseInputInterface;
12
use BitWasp\Trezor\Device\UserInput\CurrentPinInputInterface;
13
use BitWasp\TrezorProto\ButtonRequest;
14
use BitWasp\TrezorProto\ButtonRequestType;
15
use BitWasp\TrezorProto\PassphraseRequest;
16
use BitWasp\TrezorProto\Ping;
17
use BitWasp\TrezorProto\PinMatrixRequest;
18
use BitWasp\TrezorProto\Success;
19
20
class PingService extends DeviceService
21
{
22 6
    public function call(
23
        Session $session,
24
        Ping $ping,
25
        CurrentPinInputInterface $pinInput,
26
        CurrentPassphraseInputInterface $passphraseInput
27
    ): Success {
28 6
        $proto = $session->sendMessage(Message::ping($ping));
29 6
        if ($proto instanceof ButtonRequest) {
30
            // allow user to accept with the button
31 1
            $proto = $session->sendMessage($this->confirmWithButton($proto, ButtonRequestType::ButtonRequest_ProtectCall()));
32
        }
33
34
        // allow user to accept with their pin
35 6
        if ($proto instanceof PinMatrixRequest) {
36 1
            $proto = $session->sendMessage($this->provideCurrentPin($proto, $pinInput));
37
        }
38
39
        // allow user to accept with their passphrase
40 6
        if ($proto instanceof PassphraseRequest) {
41 1
            $proto = $session->sendMessage($this->provideCurrentPassphrase($passphraseInput));
42
        }
43
44 6
        if (!($proto instanceof Success)) {
45 1
            throw new UnexpectedResultException("Unexpected response, expecting Success, got " . get_class($proto));
46
        }
47
48
        /** @var Success $proto */
49 5
        if (!hash_equals($proto->getMessage(), $ping->getMessage())) {
50 1
            throw new IncorrectNonceException("Nonce returned by device was incorrect");
51
        }
52
53 4
        return $proto;
54
    }
55
}
56