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

PingService   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 46
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C call() 0 44 14
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\Message;
10
use BitWasp\Trezor\Device\UserInput\CurrentPassphraseInputInterface;
11
use BitWasp\Trezor\Device\UserInput\CurrentPinInputInterface;
12
use BitWasp\TrezorProto\ButtonRequest;
13
use BitWasp\TrezorProto\ButtonRequestType;
14
use BitWasp\TrezorProto\PassphraseRequest;
15
use BitWasp\TrezorProto\Ping;
16
use BitWasp\TrezorProto\PinMatrixRequest;
17
use BitWasp\TrezorProto\Success;
18
19
class PingService extends DeviceService
20
{
21 8
    public function call(
22
        Session $session,
23
        Ping $ping,
24
        CurrentPinInputInterface $pinInput = null,
25
        CurrentPassphraseInputInterface $passphraseInput = null
26
    ): Success {
27 8
        if ($ping->hasPinProtection() && $ping->getPinProtection() && $pinInput === null) {
28 1
            throw new \InvalidArgumentException("Missing pin input");
29
        }
30
31 7
        if ($ping->hasPassphraseProtection() && $ping->getPassphraseProtection() && $passphraseInput === null) {
32 1
            throw new \InvalidArgumentException("Missing passphrase input");
33
        }
34
35 6
        $proto = $session->sendMessage(Message::ping($ping));
36 6
        if ($proto instanceof ButtonRequest) {
37
            // allow user to accept with the button
38 1
            $proto = $session->sendMessage($this->confirmWithButton($proto, ButtonRequestType::ButtonRequest_ProtectCall_VALUE));
39
        }
40
41 6
        if ($ping->hasPinProtection()) {
42
            // allow user to accept with their pin
43 6
            if ($proto instanceof PinMatrixRequest) {
44 1
                $proto = $session->sendMessage($this->provideCurrentPin($proto, $pinInput));
0 ignored issues
show
Bug introduced by
It seems like $pinInput can also be of type null; however, parameter $currentPinInput of BitWasp\Trezor\Device\Co...ce::provideCurrentPin() does only seem to accept BitWasp\Trezor\Device\Us...urrentPinInputInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
                $proto = $session->sendMessage($this->provideCurrentPin($proto, /** @scrutinizer ignore-type */ $pinInput));
Loading history...
45
            }
46
        }
47
48 6
        if ($ping->hasPassphraseProtection()) {
49
            // allow user to accept with their passphrase
50 6
            if ($proto instanceof PassphraseRequest) {
51 1
                $proto = $session->sendMessage($this->provideCurrentPassphrase($passphraseInput));
0 ignored issues
show
Bug introduced by
It seems like $passphraseInput can also be of type null; however, parameter $passphraseInput of BitWasp\Trezor\Device\Co...videCurrentPassphrase() does only seem to accept BitWasp\Trezor\Device\Us...assphraseInputInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
                $proto = $session->sendMessage($this->provideCurrentPassphrase(/** @scrutinizer ignore-type */ $passphraseInput));
Loading history...
52
            }
53
        }
54
55 6
        if (!($proto instanceof Success)) {
56 1
            throw new \RuntimeException("Unexpected response, expecting Success, got " . get_class($proto));
57
        }
58
59
        /** @var Success $proto */
60 5
        if (!hash_equals($proto->getMessage(), $ping->getMessage())) {
61 1
            throw new IncorrectNonceException("Nonce returned by device was incorrect");
62
        }
63
64 4
        return $proto;
65
    }
66
}
67