Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
10 / 14
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Payment_Gateways
71.43% covered (warning)
71.43%
10 / 14
50.00% covered (danger)
50.00%
2 / 4
9.49
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 add_to_woocommerce
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 register_woocommerce_block_checkout_support
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 add_logger_to_gateways
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
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\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     * Check can the server run the plugin math.
27     * Get the list of Bitcoin gateways to register with WooCommerce Blocks checkout.
28     */
29    protected API_Interface $api;
30
31    /**
32     * Passed to Bitcoin_Gateway_Blocks_Checkout_Support.
33     */
34    protected Settings_Interface $settings;
35
36    /**
37     * Constructor
38     *
39     * @param API_Interface      $api The main plugin functions.
40     * @param Settings_Interface $settings The plugin settings.
41     * @param LoggerInterface    $logger A PSR logger.
42     */
43    public function __construct( API_Interface $api, Settings_Interface $settings, LoggerInterface $logger ) {
44        $this->setLogger( $logger );
45        $this->settings = $settings;
46        $this->api      = $api;
47    }
48
49    /**
50     * Add the Gateway to WooCommerce.
51     *
52     * @hooked woocommerce_payment_gateways
53     *
54     * @param string[] $gateways The payment gateways registered with WooCommerce.
55     *
56     * @return string[]
57     * @see WC_Payment_Gateways::init()
58     */
59    public function add_to_woocommerce( array $gateways ): array {
60
61        if ( ! $this->api->is_server_has_dependencies() ) {
62            return $gateways;
63        }
64
65        $gateways[] = Bitcoin_Gateway::class;
66
67        return $gateways;
68    }
69
70    /**
71     * Registers the gateway with WooCommerce Blocks checkout integration.
72     *
73     * It seems the `woocommerce_payment_gateways` filter is not run before this.
74     *
75     * @hooked woocommerce_blocks_payment_method_type_registration
76     *
77     * @param PaymentMethodRegistry $payment_method_registry WooCommerce class which handles blocks checkout gateways.
78     */
79    public function register_woocommerce_block_checkout_support( PaymentMethodRegistry $payment_method_registry ): void {
80
81        foreach ( $this->api->get_bitcoin_gateways() as $gateway ) {
82
83            $support = new Bitcoin_Gateway_Blocks_Checkout_Support( $gateway, $this->settings );
84            $payment_method_registry->register( $support );
85        }
86    }
87
88    /**
89     * Set the PSR logger on each gateway instance.
90     *
91     * @hooked woocommerce_available_payment_gateways
92     *
93     * @param WC_Payment_Gateway[] $available_gateways The gateways to be displayed on the checkout.
94     *
95     * @return WC_Payment_Gateway[]
96     */
97    public function add_logger_to_gateways( array $available_gateways ): array {
98
99        foreach ( $available_gateways as $gateway ) {
100            if ( $gateway instanceof Bitcoin_Gateway ) {
101                $gateway->setLogger( $this->logger );
102            }
103        }
104
105        return $available_gateways;
106    }
107}