Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Blockchain_Info_Api_Transaction_Adapter | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| adapt | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| map_t_in | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| map_v_out | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Adapter for Blockchain.info transaction objects. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Clients\Blockchain\Adapters; |
| 9 | |
| 10 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction; |
| 11 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_Interface; |
| 12 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_VIn; |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_VOut; |
| 14 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Services\Exchange_Rate_Service; |
| 15 | use BrianHenryIE\WP_Bitcoin_Gateway\BlockchainInfo\Model\Transaction as BlockchainInfo_Transaction; |
| 16 | use BrianHenryIE\WP_Bitcoin_Gateway\BlockchainInfo\Model\TransactionInput; |
| 17 | use BrianHenryIE\WP_Bitcoin_Gateway\BlockchainInfo\Model\TransactionOut; |
| 18 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money; |
| 19 | use DateTimeImmutable; |
| 20 | use DateTimeZone; |
| 21 | |
| 22 | /** |
| 23 | * Adapter that converts Blockchain.info transaction objects to internal Transaction_Interface. |
| 24 | */ |
| 25 | class Blockchain_Info_Api_Transaction_Adapter { |
| 26 | |
| 27 | /** |
| 28 | * Adapt a Blockchain.info transaction to the internal transaction interface. |
| 29 | * |
| 30 | * @param BlockchainInfo_Transaction $transaction The Blockchain.info transaction object. |
| 31 | * @return Transaction_Interface The adapted transaction. |
| 32 | */ |
| 33 | public function adapt( BlockchainInfo_Transaction $transaction ): Transaction_Interface { |
| 34 | return new Transaction( |
| 35 | tx_id: $transaction->getHash(), |
| 36 | block_time: new DateTimeImmutable( '@' . $transaction->getTime(), new DateTimeZone( 'UTC' ) ), |
| 37 | version: $transaction->getVer(), |
| 38 | v_in: array_map( $this->map_t_in( ... ), $transaction->getInputs() ), |
| 39 | v_out: array_map( $this->map_v_out( ... ), $transaction->getOut() ), |
| 40 | block_height: $transaction->getBlockHeight(), |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Map a Blockchain.info transaction vector input to internal Transaction_VIn. |
| 46 | * |
| 47 | * @param TransactionInput $transaction_input The transaction input from Blockchain.info. |
| 48 | * @return Transaction_VIn The mapped transaction input. |
| 49 | */ |
| 50 | protected function map_t_in( TransactionInput $transaction_input ): Transaction_VIn { |
| 51 | return new Transaction_VIn( |
| 52 | sequence: $transaction_input->getSequence(), |
| 53 | scriptsig: $transaction_input->getScript(), |
| 54 | address: $transaction_input->getPrevOut()->getAddr(), |
| 55 | prevout_scriptpubkey: $transaction_input->getPrevOut()->getScript(), |
| 56 | value: Money::of( $transaction_input->getPrevOut()->getValue() / Exchange_Rate_Service::SATOSHI_RATE, 'BTC' ), |
| 57 | prev_out_n: $transaction_input->getPrevOut()->getN(), |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Map a Blockchain.info transaction vector output to internal Transaction_VOut. |
| 63 | * |
| 64 | * @param TransactionOut $out The transaction output from Blockchain.info. |
| 65 | * @return Transaction_VOut The mapped transaction output. |
| 66 | */ |
| 67 | protected function map_v_out( TransactionOut $out ): Transaction_VOut { |
| 68 | return new Transaction_VOut( |
| 69 | value: Money::of( $out->getValue(), 'BTC' )->dividedBy( Exchange_Rate_Service::SATOSHI_RATE ), |
| 70 | scriptpubkey_address: $out->getAddr(), |
| 71 | ); |
| 72 | } |
| 73 | } |