Create::doCreate()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
nc 9
nop 4
dl 0
loc 16
rs 9.2222
c 1
b 0
f 0
1
<?php
2
namespace Appino\Blockchain\Classes;
3
4
use Appino\Blockchain\Classes\Blockchain;
5
use Appino\Blockchain\Exception\ParameterError;
6
use Appino\Blockchain\Objects\WalletResponse;
7
8
class Create{
9
10
    protected $blockchain;
11
    const URL = '/api/v2/create';
12
13
    public function __construct(Blockchain $blockchain){
14
        $this->blockchain = $blockchain;
15
    }
16
17
    /**
18
     * Create new Wallet
19
     *
20
     * @param $password string
21
     * @param string|null $email
22
     * @param string|null $label
23
     * @return WalletResponse
24
     * @throws ParameterError
25
     */
26
    public function create($password, $email=null, $label=null) {
27
        $response = $this->doCreate($password, null, $email, $label);
28
        return new WalletResponse($response);
29
    }
30
31
    /**
32
     * Create new Wallet with specific private key
33
     *
34
     * @param string $password
35
     * @param string $privKey
36
     * @param string|null $email
37
     * @param string|null $label
38
     * @return WalletResponse
39
     * @throws ParameterError
40
     */
41
    public function createWithKey($password, $privKey, $email=null, $label=null) {
42
        if(!isset($privKey) || is_null($privKey))
43
            throw new ParameterError("Private Key required.");
44
45
        return new WalletResponse($this->doCreate($password, $privKey, $email, $label));
46
    }
47
48
    /**
49
     * Create new Wallet
50
     *
51
     * @param string $password
52
     * @param string|null $priv
53
     * @param string|null $label
54
     * @param string|null $email
55
     * @return array
56
     * @throws ParameterError
57
     */
58
    protected function doCreate($password, $priv = null, $label = null, $email = null){
59
        if(!isset($password) || empty($password))
60
            throw new ParameterError("Password required.");
61
62
        $params = array(
63
            'password'=>$password,
64
            'hd'=>true
65
        );
66
        if(!is_null($priv))
67
            $params['priv'] = $priv;
68
        if(!is_null($email))
69
            $params['email'] = $email;
70
        if(!is_null($label))
71
            $params['label'] = $label;
72
73
        return $this->blockchain->Request(Blockchain::POST,self::URL,$params);
74
    }
75
76
}
77