Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
28.57% covered (danger)
28.57%
4 / 14
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
WC_Bitcoin_Order
28.57% covered (danger)
28.57%
4 / 14
33.33% covered (danger)
33.33%
2 / 6
46.44
0.00% covered (danger)
0.00%
0 / 1
 __call
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_wc_order
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_address
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_last_checked_time
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_gateway
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * A ~facade on WC_Order that returns strongly typed data.
4 *
5 * I.e. to return its string meta address as a typed Bitcoin_Address etc.
6 *
7 * @package brianehnryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\Model;
11
12use BadMethodCallException;
13use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Wallet\Bitcoin_Address;
14use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Payments\Transaction_Interface;
15use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Exceptions\BH_WP_Bitcoin_Gateway_Exception;
16use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money;
17use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\Bitcoin_Gateway;
18use BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce\Order;
19use DateTimeInterface;
20use Psr\Log\LoggerAwareInterface;
21use Psr\Log\LoggerAwareTrait;
22use Psr\Log\LoggerInterface;
23use WC_Order;
24use WC_Payment_Gateways;
25
26/**
27 * @mixin WC_Order
28 */
29class WC_Bitcoin_Order implements LoggerAwareInterface {
30    use LoggerAwareTrait;
31
32    /**
33     * The Bitcoin payment gateway used for this order.
34     *
35     * @var Bitcoin_Gateway|null
36     */
37    protected ?Bitcoin_Gateway $gateway = null;
38
39    /**
40     * The number of confirmations the order needs for transactions.
41     *
42     * TODO: Different orders may have different number of confirmations. E.g. more expensive orders might want higher
43     * number of confirmations, orders that need near-er instant completion might trust mempool.
44     *
45     * @var int
46     */
47    protected int $confirmations;
48
49    /**
50     * Magic method to proxy method calls to the underlying WooCommerce order.
51     *
52     * @param string       $name The method name to call.
53     * @param array<mixed> $arguments The arguments to pass to the method.
54     *
55     * @throws BadMethodCallException When the method doesn't exist on the underlying WC_Order object.
56     */
57    public function __call( string $name, array $arguments ): mixed {
58        if ( is_callable( array( $this->wc_order, $name ) ) ) {
59            return $this->wc_order->$name( ...$arguments );
60        }
61        throw new BadMethodCallException();
62    }
63
64    /**
65     * Constructor.
66     *
67     * @param WC_Order                      $wc_order The WooCommerce order.
68     * @param Bitcoin_Address               $payment_address The address assigned to the order.
69     * @param ?array<Transaction_Interface> $transactions The known transactions for the address.
70     * @param LoggerInterface               $logger PSR logger.
71     *
72     * @throws BH_WP_Bitcoin_Gateway_Exception When the order has no Bitcoin address or the address cannot be retrieved.
73     */
74    public function __construct(
75        protected WC_Order $wc_order,
76        protected Bitcoin_Address $payment_address,
77        protected ?array $transactions,
78        LoggerInterface $logger,
79    ) {
80        $this->setLogger( $logger );
81    }
82
83    /**
84     * Get the original order object.
85     */
86    public function get_wc_order(): WC_Order {
87        return $this->wc_order;
88    }
89
90    /**
91     * Get the Bitcoin payment address associated with this order.
92     */
93    public function get_address(): Bitcoin_Address {
94        return $this->payment_address;
95    }
96
97    /**
98     * A ~proxy for the Bitcoin Address object's last checked time.
99     *
100     * Null when never changed
101     */
102    public function get_last_checked_time(): ?DateTimeInterface {
103        if ( is_null( $this->payment_address->get_tx_ids() ) ) {
104            return null;
105        }
106        return $this->payment_address->get_modified_time();
107    }
108
109    /**
110     * Get the order's gateway.
111     *
112     * Since the gateway id could change, particularly where there are multiple instances, it may happen that the id
113     * in the order does not match an existing gateway, => return null.
114     */
115    public function get_gateway(): ?Bitcoin_Gateway {
116        if ( ! isset( WC_Payment_Gateways::instance()->payment_gateways[ $this->wc_order->get_payment_method() ] ) ) {
117            return null;
118        }
119
120        if ( ! ( WC_Payment_Gateways::instance()->payment_gateways[ $this->wc_order->get_payment_method() ] instanceof Bitcoin_Gateway ) ) {
121            return null;
122        }
123
124        return WC_Payment_Gateways::instance()->payment_gateways[ $this->wc_order->get_payment_method() ];
125    }
126}