Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Update_Address_Transactions_Result
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_new_transactions
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 is_updated
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_unused
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * @package brianhenryie/bh-wp-bitcoin-gateway
5 */
6
7namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Results;
8
9use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Address;
10use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_Interface;
11
12/**
13 * @used-by API::update_address_transactions()
14 */
15readonly class Update_Address_Transactions_Result {
16
17    /**
18     * Constructor
19     *
20     * @param Bitcoin_Address                   $queried_address The checked (unmodified) address details/object.
21     * @param array<int, string>                $known_tx_ids_before <post_id, tx_id>.
22     * @param array<int, Transaction_Interface> $all_transactions <post_id, Transaction_Interface>.
23     */
24    public function __construct(
25        public Bitcoin_Address $queried_address,
26        public ?array $known_tx_ids_before,
27        public array $all_transactions,
28    ) {
29    }
30
31    /**
32     * Filters all known transactions to those that new since this update was run.
33     *
34     * @return array<int, Transaction_Interface>
35     */
36    public function get_new_transactions(): array {
37        // If there were none before, all transactions are new transactions (potentially empty array).
38        if ( empty( $this->known_tx_ids_before ) ) {
39            return $this->all_transactions;
40        }
41        $new_transactions = array();
42        foreach ( $this->all_transactions as $post_id => $transaction ) {
43            if ( in_array( $post_id, array_keys( $this->known_tx_ids_before ), true ) ) {
44                continue;
45            }
46            $new_transactions[ $post_id ] = $transaction;
47        }
48        return $new_transactions;
49    }
50
51    /**
52     * Are there any new transactions?
53     */
54    public function is_updated(): bool {
55        return ! empty( $this->get_new_transactions() );
56    }
57
58    /**
59     * After checking for transactions, an address is unused if no transactions were found.
60     */
61    public function is_unused(): bool {
62        return empty( $this->all_transactions );
63    }
64}