Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.67% covered (danger)
46.67%
7 / 15
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Payment_Gateways
46.67% covered (danger)
46.67%
7 / 15
33.33% covered (danger)
33.33%
1 / 3
6.43
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
 add_to_woocommerce
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 register_woocommerce_block_checkout_support
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Add the payment gateway to WooCommerce's list of gateways.
4 *
5 * @package    brianhenryie/bh-wp-bitcoin-gateway
6 */
7
8namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
9
10use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
11use BrianHenryIE\WP_Bitcoin_Gateway\API_Interface;
12use BrianHenryIE\WP_Bitcoin_Gateway\Settings_Interface;
13use Psr\Log\LoggerAwareTrait;
14use Psr\Log\LoggerInterface;
15use WC_Payment_Gateway;
16use WC_Payment_Gateways;
17
18/**
19 * Add the payment gateway's class name to WooCommerce's list of gateways it will
20 * later instantiate.
21 */
22class Payment_Gateways {
23    use LoggerAwareTrait;
24
25    /**
26     * Constructor
27     *
28     * @param API_Interface             $api The main plugin API, to be used by the gateway.
29     * @param API_WooCommerce_Interface $api_woocommerce To get the list of Bitcoin gateways to register with WooCommerce Blocks checkout.
30     * @param Settings_Interface        $settings Passed to {@see Bitcoin_Gateway_Blocks_Checkout_Support}.
31     * @param LoggerInterface           $logger A PSR logger.
32     */
33    public function __construct(
34        protected API_Interface $api,
35        protected API_WooCommerce_Interface $api_woocommerce,
36        protected Settings_Interface $settings,
37        LoggerInterface $logger
38    ) {
39        $this->setLogger( $logger );
40    }
41
42    /**
43     * Add the Gateway to WooCommerce.
44     *
45     * @hooked woocommerce_payment_gateways
46     *
47     * @param array<class-string|WC_Payment_Gateway> $gateways The payment gateways registered with WooCommerce.
48     *
49     * @return array<class-string|WC_Payment_Gateway>
50     * @see WC_Payment_Gateways::init()
51     */
52    public function add_to_woocommerce( array $gateways ): array {
53        $gateways[] = new Bitcoin_Gateway(
54            $this->api,
55            $this->api_woocommerce,
56            $this->settings,
57            $this->logger
58        );
59
60        return $gateways;
61    }
62
63    /**
64     * Registers the gateway with WooCommerce Blocks checkout integration.
65     *
66     * It seems the `woocommerce_payment_gateways` filter is not run before this.
67     *
68     * @hooked woocommerce_blocks_payment_method_type_registration
69     *
70     * @param PaymentMethodRegistry $payment_method_registry WooCommerce class which handles blocks checkout gateways.
71     */
72    public function register_woocommerce_block_checkout_support( PaymentMethodRegistry $payment_method_registry ): void {
73
74        foreach ( $this->api_woocommerce->get_bitcoin_gateways() as $gateway ) {
75
76            $support = new Bitcoin_Gateway_Blocks_Checkout_Support(
77                $gateway,
78                $this->api,
79                $this->settings
80            );
81            $payment_method_registry->register( $support );
82        }
83    }
84}