Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.37% covered (danger)
47.37%
9 / 19
20.00% covered (danger)
20.00%
2 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Bitcoin_Order
47.37% covered (danger)
47.37%
9 / 19
20.00% covered (danger)
20.00%
2 / 10
32.99
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
66.67% covered (warning)
66.67%
4 / 6
0.00% covered (danger)
0.00%
0 / 1
2.15
 get_btc_total_price
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_btc_exchange_rate
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_last_checked_time
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_gateway
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_amount_received
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_amount_received
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Really just a wrapper for WC_Order.
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\API\Model;
11
12use BadMethodCallException;
13use BrianHenryIE\WP_Bitcoin_Gateway\API\Addresses\Bitcoin_Address;
14use BrianHenryIE\WP_Bitcoin_Gateway\API\Addresses\Bitcoin_Address_Factory;
15use BrianHenryIE\WP_Bitcoin_Gateway\WooCommerce\Bitcoin_Gateway;
16use BrianHenryIE\WP_Bitcoin_Gateway\WooCommerce\Order;
17use DateTimeInterface;
18use WC_Order;
19use WC_Payment_Gateways;
20
21/**
22 *
23 * @method get_id()
24 * @method get_status()
25 * @method get_date_created()
26 * @method add_order_note()
27 * @method payment_complete()
28 * @method is_paid()
29 * @method save()
30 * @method get_currency()
31 * @method get_date_paid()
32 */
33class Bitcoin_Order implements Bitcoin_Order_Interface {
34
35    protected WC_Order $wc_order;
36
37    protected Bitcoin_Address $address;
38
39    protected ?Bitcoin_Gateway $gateway;
40
41    /**
42     * The number of confirmations the order needs for transactions.
43     */
44    protected int $confirmations;
45
46    public function __call( $name, $arguments ) {
47        if ( is_callable( array( $this->wc_order, $name ) ) ) {
48            return call_user_func_array( array( $this->wc_order, $name ), $arguments );
49        }
50        throw new BadMethodCallException();
51    }
52
53    public function __construct( WC_Order $wc_order, Bitcoin_Address_Factory $bitcoin_address_factory ) {
54
55        $this->wc_order = $wc_order;
56
57        try {
58            $bitcoin_address         = $wc_order->get_meta( Order::BITCOIN_ADDRESS_META_KEY );
59            $bitcoin_address_post_id = $bitcoin_address_factory->get_post_id_for_address( $bitcoin_address );
60            $this->address           = $bitcoin_address_factory->get_by_post_id( $bitcoin_address_post_id );
61        } catch ( \Exception $exception ) {
62            // $this->logger->warning( "`shop_order:{$order->get_id()}` has no Bitcoin address.", array( 'order_id' => $order->get_id() ) );
63            throw new \Exception( 'Problem with order Bitcoin address.' );
64        }
65    }
66
67    /**
68     * The order price in Bitcoin at the time of purchase.
69     */
70    public function get_btc_total_price(): int {
71        return floatval( $this->wc_order->get_meta( Order::ORDER_TOTAL_BITCOIN_AT_TIME_OF_PURCHASE_META_KEY ) );
72    }
73
74    /**
75     * The Bitcoin exchange rate at the time of purchase.
76     */
77    public function get_btc_exchange_rate(): float {
78        return floatval( $this->wc_order->get_meta( Order::EXCHANGE_RATE_AT_TIME_OF_PURCHASE_META_KEY ) );
79    }
80
81    public function get_address(): Bitcoin_Address {
82        return $this->address;
83    }
84
85    public function get_last_checked_time(): DateTimeInterface {
86
87        // $last_checked_time = empty( $last_checked_time ) ? null : $last_checked_time;
88        return $this->wc_order->get_meta( Order::LAST_CHECKED_META_KEY );
89    }
90
91    public function set_last_checked_time( DateTimeInterface $last_checked_time ): void {
92        // @phpstan-ignore-next-line This works fine.
93        $this->wc_order->add_meta_data( Order::LAST_CHECKED_META_KEY, $last_checked_time, true );
94        $this->last_checked_time = $last_checked_time;
95    }
96
97    /**
98     * Get the order's gateway.
99     *
100     * Since the gateway id could change, particularly where there are multiple instances, it may happen that the id
101     * in the order does not match an existing gateway, => return null.
102     */
103    public function get_gateway(): ?Bitcoin_Gateway {
104        return WC_Payment_Gateways::instance()->payment_gateways[ $this->wc_order->get_payment_method() ] ?? null;
105    }
106
107    /**
108     * Get the total value with the required number of confirmations at the last checked time.
109     */
110    public function get_amount_received() {
111        return $this->amount_received;
112    }
113
114    public function set_amount_received( $updated_confirmed_value ): void {
115        $this->wc_order->add_meta_data( Order::BITCOIN_AMOUNT_RECEIVED_META_KEY, $updated_confirmed_value, true );
116        $this->amount_received = $updated_confirmed_value;
117    }
118}