CustomOrderTable
3 years ago
MetaToCustomTableMigrator.php
3 years ago
MetaToMetaTableMigrator.php
3 years ago
MigrationHelper.php
4 years ago
TableMigrator.php
3 years ago
TableMigrator.php
136 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base class for all the WP posts to order table migrator. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Database\Migrations; |
| 7 | |
| 8 | /** |
| 9 | * Base class for implementing WP posts to order tables migrations handlers. |
| 10 | * It mainly contains methods to deal with error handling. |
| 11 | * |
| 12 | * @package Automattic\WooCommerce\Database\Migrations\CustomOrderTable |
| 13 | */ |
| 14 | abstract class TableMigrator { |
| 15 | |
| 16 | /** |
| 17 | * An array of cummulated error messages. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | private $errors; |
| 22 | |
| 23 | /** |
| 24 | * Clear the error messages list. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | protected function clear_errors(): void { |
| 29 | $this->errors = array(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Add an error message to the errors list unless it's there already. |
| 34 | * |
| 35 | * @param string $error The error message to add. |
| 36 | * @return void |
| 37 | */ |
| 38 | protected function add_error( string $error ): void { |
| 39 | if ( ! in_array( $error, $this->errors, true ) ) { |
| 40 | $this->errors[] = $error; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the list of error messages added. |
| 46 | * |
| 47 | * @return array |
| 48 | */ |
| 49 | protected function get_errors(): array { |
| 50 | return $this->errors; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Run $wpdb->query and add the error, if any, to the errors list. |
| 55 | * |
| 56 | * @param string $query The SQL query to run. |
| 57 | * @return mixed Whatever $wpdb->query returns. |
| 58 | */ |
| 59 | protected function db_query( string $query ) { |
| 60 | $wpdb = WC()->get_global( 'wpdb' ); |
| 61 | |
| 62 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 63 | $result = $wpdb->query( $query ); |
| 64 | |
| 65 | if ( '' !== $wpdb->last_error ) { |
| 66 | $this->add_error( $wpdb->last_error ); |
| 67 | } |
| 68 | |
| 69 | return $result; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Run $wpdb->get_results and add the error, if any, to the errors list. |
| 74 | * |
| 75 | * @param string|null $query The SQL query to run. |
| 76 | * @param string $output Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants. |
| 77 | * @return mixed Whatever $wpdb->get_results returns. |
| 78 | */ |
| 79 | protected function db_get_results( string $query = null, string $output = OBJECT ) { |
| 80 | $wpdb = WC()->get_global( 'wpdb' ); |
| 81 | |
| 82 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 83 | $result = $wpdb->get_results( $query, $output ); |
| 84 | |
| 85 | if ( '' !== $wpdb->last_error ) { |
| 86 | $this->add_error( $wpdb->last_error ); |
| 87 | } |
| 88 | |
| 89 | return $result; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Migrate a batch of orders, logging any database error that could arise and the exception thrown if any. |
| 94 | * |
| 95 | * @param array $entity_ids Order ids to migrate. |
| 96 | * @return array An array containing the keys 'errors' (array of strings) and 'exception' (exception object or null). |
| 97 | */ |
| 98 | public function process_migration_batch_for_ids( array $entity_ids ): array { |
| 99 | $this->clear_errors(); |
| 100 | $exception = null; |
| 101 | |
| 102 | try { |
| 103 | $this->process_migration_batch_for_ids_core( $entity_ids ); |
| 104 | } catch ( \Exception $ex ) { |
| 105 | $exception = $ex; |
| 106 | } |
| 107 | |
| 108 | return array( |
| 109 | 'errors' => $this->get_errors(), |
| 110 | 'exception' => $exception, |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * The core method that actually performs the migration for the supplied batch of order ids. |
| 116 | * It doesn't need to deal with database errors nor with exceptions. |
| 117 | * |
| 118 | * @param array $entity_ids Order ids to migrate. |
| 119 | * @return void |
| 120 | */ |
| 121 | abstract protected function process_migration_batch_for_ids_core( array $entity_ids ): void; |
| 122 | |
| 123 | /** |
| 124 | * Check if the amount of processed database rows matches the amount of orders to process, and log an error if not. |
| 125 | * |
| 126 | * @param string $operation Operation performed, 'insert' or 'update'. |
| 127 | * @param array|bool $received_rows_count Value returned by @wpdb after executing the query. |
| 128 | * @return void |
| 129 | */ |
| 130 | protected function maybe_add_insert_or_update_error( string $operation, $received_rows_count ) { |
| 131 | if ( false === $received_rows_count ) { |
| 132 | $this->add_error( "$operation operation didn't complete, the database query failed" ); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |