Completed
Push — master ( 9e4642...1f55bf )
by Nathan
02:13
created

CreateRawTransaction::__construct()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 7
nop 2
crap 5
1
<?php
2
3
namespace ZenCash\Rpc\Command\RawTransactions;
4
5
use ZenCash\Rpc\Command;
6
7
final class CreateRawTransaction implements Command
8
{
9
    private const METHOD = 'createrawtransaction';
10
    private $transactions;
11
    private $recipients;
12
13
    /**
14
     * @param RawTransaction[] $transactions
15
     * @param Recipient[] $recipients
16
     */
17 2
    public function __construct(array $transactions, array $recipients)
18
    {
19 2
        foreach ($transactions as $t) {
20 2
            if (!$t instanceof RawTransaction) {
21 2
                throw new \InvalidArgumentException("Invalid type within RawTransaction array: " . gettype($t));
22
            }
23
        }
24
25 2
        foreach ($recipients as $r) {
26 2
            if (!$r instanceof Recipient) {
27 2
                throw new \InvalidArgumentException("Invalid type within Recipient array: " . gettype($r));
28
            }
29
        }
30
31 2
        $this->transactions = $transactions;
32 2
        $this->recipients = $recipients;
33 2
    }
34
35 2
    public function jsonSerialize(): object
36
    {
37 2
        $addresses = [];
38
39 2
        foreach ($this->recipients as $recipient) {
40 2
            $addresses = array_merge($addresses, (array) $recipient->jsonSerialize());
41
        }
42
43
        return (object) [
44 2
            'jsonrpc' => Command::JSON_RPC_VERSION,
45
            'id'      => Command::ID,
46 2
            'method'  => self::METHOD,
47 2
            'params' => [ $this->transactions, (object) $addresses ]
48
        ];
49
    }
50
}
51