Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.75% covered (danger)
43.75%
7 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
My_Account_View_Order
43.75% covered (danger)
43.75%
7 / 16
50.00% covered (danger)
50.00%
1 / 2
6.85
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 print_status_instructions
40.00% covered (danger)
40.00%
6 / 15
0.00% covered (danger)
0.00%
0 / 1
4.94
1<?php
2/**
3 * Add the payment instructions to the order page.
4 *
5 * @see woocommerce/templates/myaccount/view-order.php
6 *
7 * @package    brianhenryie/bh-wp-bitcoin-gateway
8 */
9
10namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
11
12use Exception;
13use Psr\Log\LoggerAwareTrait;
14use Psr\Log\LoggerInterface;
15use WC_Order;
16
17/**
18 * Get the order details and pass them to the my-account order ui template.
19 */
20class My_Account_View_Order {
21    use LoggerAwareTrait;
22
23    const string TEMPLATE_NAME = 'myaccount/view-order-bitcoin-instructions-status.php';
24
25    /**
26     * Constructor
27     *
28     * @param API_WooCommerce_Interface $api Check is it a Bitcoin order; get the order details.
29     * @param LoggerInterface           $logger A PSR logger.
30     */
31    public function __construct(
32        protected API_WooCommerce_Interface $api,
33        LoggerInterface $logger
34    ) {
35        $this->setLogger( $logger );
36    }
37
38    /**
39     * If it is a Bitcoin order being displayed, print the payment details, via template.
40     *
41     * @hooked woocommerce_view_order
42     *
43     * @param int $order_id The id of the order being viewed.
44     */
45    public function print_status_instructions( int $order_id ): void {
46
47        if ( ! $this->api->is_order_has_bitcoin_gateway( $order_id ) ) {
48            return;
49        }
50
51        /**
52         * This is definitely an order object. For it to be a Bitcoin order, it first must be an order.
53         *
54         * @var WC_Order $order
55         */
56        $order = wc_get_order( $order_id );
57
58        try {
59            $template_args = $this->api->get_formatted_order_details( $order );
60        } catch ( Exception $exception ) {
61            // Exception occurs when an order has no Bitcoin address, e.g. if there was a problem fetching one as the
62            // order was created.
63            $this->logger->warning(
64                "Failed to get `shop_order:{$order_id}` details for my-account template: {$exception->getMessage()}",
65                array(
66                    'order_id'  => $order_id,
67                    'exception' => $exception,
68                )
69            );
70            return;
71        }
72
73        $template_args['template'] = self::TEMPLATE_NAME;
74
75        wc_get_template( self::TEMPLATE_NAME, $template_args );
76    }
77}