CycleStateService.php
10 months ago
JobManager.php
10 months ago
NotificationsProcessor.php
4 weeks ago
CycleStateService.php
148 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\AsyncTasks\JobManager; |
| 8 | |
| 9 | /** |
| 10 | * The service for managing a product's send cycle state. |
| 11 | */ |
| 12 | class CycleStateService { |
| 13 | |
| 14 | /** |
| 15 | * State option prefix. |
| 16 | */ |
| 17 | public const STATE_OPTION_PREFIX = 'wc_stock_notifications_cycle_state_'; |
| 18 | |
| 19 | /** |
| 20 | * The logger instance. |
| 21 | * |
| 22 | * @var \WC_Logger_Interface |
| 23 | */ |
| 24 | private $logger; |
| 25 | |
| 26 | /** |
| 27 | * Constructor. |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | $this->logger = \wc_get_logger(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Parse the cycle state for a product. |
| 35 | * |
| 36 | * @param int $product_id The product ID. |
| 37 | * @return array |
| 38 | * @throws \Exception If the cycle state is invalid. |
| 39 | */ |
| 40 | public function get_or_initialize_cycle_state( int $product_id ): array { |
| 41 | |
| 42 | if ( $product_id <= 0 ) { |
| 43 | throw new \Exception( 'Product ID is required.' ); |
| 44 | } |
| 45 | |
| 46 | $default_state = array( |
| 47 | 'cycle_start_time' => time(), |
| 48 | 'product_ids' => array( $product_id ), |
| 49 | 'total_count' => 0, |
| 50 | 'skipped_count' => 0, |
| 51 | 'sent_count' => 0, |
| 52 | 'failed_count' => 0, |
| 53 | 'duration' => 0, |
| 54 | ); |
| 55 | |
| 56 | $cycle_state = $this->get_raw_cycle_state( $product_id ); |
| 57 | if ( empty( $cycle_state ) ) { |
| 58 | return $default_state; |
| 59 | } |
| 60 | |
| 61 | if ( array_diff_key( $default_state, $cycle_state ) || empty( $cycle_state['cycle_start_time'] ) || ! is_numeric( $cycle_state['cycle_start_time'] ) ) { |
| 62 | throw new \Exception( 'Invalid cycle state.' ); |
| 63 | } |
| 64 | |
| 65 | $cycle_state = wp_parse_args( $cycle_state, $default_state ); |
| 66 | |
| 67 | return $cycle_state; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the raw cycle state. |
| 72 | * |
| 73 | * @param int $product_id The product ID. |
| 74 | * @return array |
| 75 | */ |
| 76 | private function get_raw_cycle_state( int $product_id ): array { |
| 77 | $cycle_state = get_option( $this->get_option_name( $product_id ), false ); |
| 78 | if ( ! is_array( $cycle_state ) ) { |
| 79 | return array(); |
| 80 | } |
| 81 | |
| 82 | return $cycle_state; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Complete the cycle. |
| 87 | * |
| 88 | * @param int|string $product_id The product ID. |
| 89 | * @param array $cycle_state The cycle state. |
| 90 | * @return void |
| 91 | */ |
| 92 | public function complete_cycle( int $product_id, array $cycle_state ): void { |
| 93 | |
| 94 | $cycle_state['duration'] = time() - $cycle_state['cycle_start_time']; |
| 95 | |
| 96 | $this->logger->info( |
| 97 | sprintf( 'Completed cycle for product %d. Sent: %d, Skipped: %d, Failed: %d, Duration: %d seconds. Total notifications processed: %d', $product_id, $cycle_state['sent_count'], $cycle_state['skipped_count'], $cycle_state['failed_count'], $cycle_state['duration'], $cycle_state['total_count'] ), |
| 98 | array( 'source' => 'wc-customer-stock-notifications' ) |
| 99 | ); |
| 100 | |
| 101 | $this->save_cycle_state( $product_id, array() ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Save the cycle state. |
| 106 | * |
| 107 | * @param int $product_id The product ID. |
| 108 | * @param array $cycle_state The cycle state. |
| 109 | * @return bool Whether the state was saved. |
| 110 | */ |
| 111 | public function save_cycle_state( int $product_id, array $cycle_state ): bool { |
| 112 | if ( $product_id <= 0 ) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | $current_cycle_state = $this->get_raw_cycle_state( $product_id ); |
| 117 | if ( $current_cycle_state === $cycle_state ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if ( empty( $cycle_state ) ) { |
| 122 | $result = delete_option( $this->get_option_name( $product_id ) ); |
| 123 | } else { |
| 124 | $result = update_option( $this->get_option_name( $product_id ), $cycle_state, false ); |
| 125 | } |
| 126 | |
| 127 | if ( ! $result ) { |
| 128 | $this->logger->error( sprintf( 'Failed to save cycle state for product %d. Cycle state: %s', $product_id, wc_print_r( $cycle_state, true ) ), array( 'source' => 'wc-customer-stock-notifications' ) ); |
| 129 | } |
| 130 | |
| 131 | return $result; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the option name. |
| 136 | * |
| 137 | * @param int $product_id The product ID. |
| 138 | * @return string |
| 139 | */ |
| 140 | private function get_option_name( int $product_id ): string { |
| 141 | if ( $product_id <= 0 ) { |
| 142 | return ''; |
| 143 | } |
| 144 | |
| 145 | return self::STATE_OPTION_PREFIX . $product_id; |
| 146 | } |
| 147 | } |
| 148 |