Completed
Pull Request — master (#127)
by thomas
19:50
created

WalletV2Sweeper::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 8
dl 0
loc 19
ccs 10
cts 12
cp 0.8333
crap 3.0416
rs 9.6333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Blocktrail\SDK;
4
5
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
6
use BitWasp\Buffertools\Buffer;
7
use Blocktrail\CryptoJSAES\CryptoJSAES;
8
9
10
use Blocktrail\SDK\Exceptions\BlocktrailSDKException;
11
12
class WalletV2Sweeper extends WalletSweeper {
13
14
    /**
15
     * @param string              $encryptedPrimaryMnemonic
16
     * @param string              $encryptedSecretMnemonic
17
     * @param string              $backupMnemonic
18
     * @param string              $passphrase
19
     * @param array               $blocktrailPublicKeys
20
     * @param UnspentOutputFinder $utxoFinder
21
     * @param string              $network
22
     * @param bool                $testnet
23
     * @throws BlocktrailSDKException
24
     */
25 1
    public function __construct($encryptedPrimaryMnemonic, $encryptedSecretMnemonic, $backupMnemonic, $passphrase, array $blocktrailPublicKeys, UnspentOutputFinder $utxoFinder, $network = 'btc', $testnet = false) {
26
        // cleanup copy paste errors from mnemonics
27 1
        $encryptedPrimaryMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedPrimaryMnemonic))));
28 1
        $encryptedSecretMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($encryptedSecretMnemonic))));
29 1
        $backupMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($backupMnemonic))));
30
31 1
        $bip39 = MnemonicFactory::bip39();
32 1
        if (!($secret = CryptoJSAES::decrypt(base64_encode($bip39->mnemonicToEntropy($encryptedSecretMnemonic)->getBinary()), $passphrase))) {
33
            throw new BlocktrailSDKException("Failed to decret password encrypted secret");
34
        }
35
36 1
        if (!($primarySeed = CryptoJSAES::decrypt(base64_encode($bip39->mnemonicToEntropy($encryptedPrimaryMnemonic)->getBinary()), $secret))) {
37
            throw new BlocktrailSDKException("failed to decrypt encrypted primary seed! (weird!)");
38
        }
39
40 1
        $backupSeed = $bip39->mnemonicToEntropy($backupMnemonic);
41
42 1
        parent::__construct(new Buffer($primarySeed), $backupSeed, $blocktrailPublicKeys, $utxoFinder, $network, $testnet);
43 1
    }
44
}
45