Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
28.57% |
4 / 14 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
Cron | |
28.57% |
4 / 14 |
|
40.00% |
2 / 5 |
31.32 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
add_cron_jon | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
check_address_for_single_order | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
check_address_for_multiple_orders | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
recheck_bad_address_orders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Since we call an external API, let's do everything in a background task. |
4 | */ |
5 | |
6 | namespace BrianHenryIE\WC_Address_Validation\WP_Includes; |
7 | |
8 | use BrianHenryIE\WC_Address_Validation\API_Interface; |
9 | use BrianHenryIE\WC_Address_Validation\Settings_Interface; |
10 | use Psr\Log\LoggerAwareTrait; |
11 | use Psr\Log\LoggerInterface; |
12 | use WC_Order; |
13 | |
14 | class Cron { |
15 | |
16 | use LoggerAwareTrait; |
17 | |
18 | const CHECK_SINGLE_ADDRESS_CRON_JOB = 'bh_wc_address_validation_check_one_address'; |
19 | const CHECK_MULTIPLE_ADDRESSES_CRON_JOB = 'bh_wc_address_validation_check_many_addresses'; |
20 | const RECHECK_BAD_ADDRESSES_CRON_JOB = 'bh_wc_address_validation_recheck_bad_addresses'; |
21 | |
22 | /** |
23 | * @var Settings_Interface |
24 | */ |
25 | protected $settings; |
26 | |
27 | /** |
28 | * @var API_Interface |
29 | */ |
30 | protected API_Interface $api; |
31 | |
32 | /** |
33 | * Cron constructor. |
34 | * |
35 | * @param API_Interface $api |
36 | * @param Settings_Interface $settings |
37 | * @param LoggerInterface $logger |
38 | */ |
39 | public function __construct( API_Interface $api, Settings_Interface $settings, LoggerInterface $logger ) { |
40 | |
41 | $this->logger = $logger; |
42 | $this->settings = $settings; |
43 | $this->api = $api; |
44 | } |
45 | |
46 | |
47 | /** |
48 | * Schedules or deletes the cron as per the settings. |
49 | * |
50 | * @see wp_get_schedules() |
51 | * |
52 | * @hooked plugins_loaded |
53 | */ |
54 | public function add_cron_jon(): void { |
55 | if ( ! wp_next_scheduled( self::RECHECK_BAD_ADDRESSES_CRON_JOB ) ) { |
56 | wp_schedule_event( time(), 'twicedaily', self::RECHECK_BAD_ADDRESSES_CRON_JOB ); |
57 | $this->logger->info( 'Cron job scheduled: ' . self::RECHECK_BAD_ADDRESSES_CRON_JOB ); |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * @hooked self::CHECK_ADDRESS_CRON_JOB |
63 | * |
64 | * @param int $order_id The order to check. |
65 | */ |
66 | public function check_address_for_single_order( int $order_id ): void { |
67 | |
68 | $order = wc_get_order( $order_id ); |
69 | |
70 | if ( ! $order instanceof WC_Order ) { |
71 | |
72 | $this->logger->error( 'Invalid order_id.', array( 'order_id' => $order_id ) ); |
73 | |
74 | return; |
75 | } |
76 | |
77 | $this->api->check_address_for_order( $order, false ); |
78 | } |
79 | |
80 | /** |
81 | * @see Cron::CHECK_MULTIPLE_ADDRESSES_CRON_JOB |
82 | * @hooked bh_wc_address_validation_check_many_addresses |
83 | * |
84 | * @param int[] $order_ids |
85 | */ |
86 | public function check_address_for_multiple_orders( array $order_ids ): void { |
87 | |
88 | foreach ( $order_ids as $order_id ) { |
89 | $this->check_address_for_single_order( $order_id ); |
90 | } |
91 | } |
92 | |
93 | /** |
94 | * Sometimes the check fails – e.g. sometimes the USPS API is offline. |
95 | */ |
96 | public function recheck_bad_address_orders(): void { |
97 | $this->api->recheck_bad_address_orders(); |
98 | } |
99 | } |