| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Cli\Commands; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
use JFB_Modules\Form_Record\Models\Record_Model; |
| 12 |
use JFB_Modules\Gateways\Base_Gateway; |
| 13 |
use JFB_Modules\Gateways\Db_Models\Payer_Model; |
| 14 |
use JFB_Modules\Gateways\Db_Models\Payer_Shipping_Model; |
| 15 |
use JFB_Modules\Gateways\Db_Models\Payment_Model; |
| 16 |
use JFB_Modules\Gateways\Db_Models\Payment_To_Payer_Shipping_Model; |
| 17 |
use JFB_Modules\Gateways\Db_Models\Payment_To_Record; |
| 18 |
use JFB_Modules\Cli\Cli_Tools; |
| 19 |
|
| 20 |
class Seed_Payments implements Base_Command_It { |
| 21 |
|
| 22 |
public function rep_item_id() { |
| 23 |
return 'seed-payments'; |
| 24 |
} |
| 25 |
|
| 26 |
public function condition(): bool { |
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @param $args |
| 32 |
* @param $assoc_args |
| 33 |
* |
| 34 |
* @throws \WP_CLI\ExitException |
| 35 |
*/ |
| 36 |
public function do_command( $args, $assoc_args ) { |
| 37 |
$form = Cli_Tools::resolve_form( $assoc_args ); |
| 38 |
$limit = absint( $assoc_args['limit'] ?? 1000000 ); |
| 39 |
|
| 40 |
if ( 0 >= $limit ) { |
| 41 |
$limit = 1; |
| 42 |
} |
| 43 |
|
| 44 |
/** @var \WP_User $user */ |
| 45 |
list( $user ) = get_users( |
| 46 |
array( |
| 47 |
'number' => 1, |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
$record_id = 0; |
| 52 |
try { |
| 53 |
$record_id = $this->get_record_id(); |
| 54 |
} catch ( \Exception $exception ) { |
| 55 |
\WP_CLI::error( $exception->getMessage() ); |
| 56 |
} |
| 57 |
|
| 58 |
$current = 0; |
| 59 |
|
| 60 |
foreach ( range( 0, $limit ) as $current ) { |
| 61 |
$payment_id = ( new Payment_Model() )->insert_soft( |
| 62 |
array( |
| 63 |
'transaction_id' => wp_unique_id( 'cli-' ), |
| 64 |
'form_id' => $form->ID, |
| 65 |
'user_id' => $user->ID, |
| 66 |
'gateway_id' => 'paypal', |
| 67 |
'scenario' => 'PAY_NOW', |
| 68 |
'amount_value' => random_int( 1, 500 ), |
| 69 |
'amount_code' => 'USD', |
| 70 |
'type' => Base_Gateway::PAYMENT_TYPE_INITIAL, |
| 71 |
'status' => 'COMPLETED', |
| 72 |
) |
| 73 |
); |
| 74 |
|
| 75 |
( new Payment_To_Record() )->insert_soft( |
| 76 |
array( |
| 77 |
'payment_id' => $payment_id, |
| 78 |
'record_id' => $record_id, |
| 79 |
) |
| 80 |
); |
| 81 |
|
| 82 |
$payer_id = ( new Payer_Model() )->insert_soft( |
| 83 |
array( |
| 84 |
'user_id' => $user->ID, |
| 85 |
'payer_id' => wp_unique_id( 'payer-' ), |
| 86 |
'first_name' => 'John', |
| 87 |
'last_name' => 'Doe', |
| 88 |
'email' => '[email protected]', |
| 89 |
) |
| 90 |
); |
| 91 |
|
| 92 |
$shipping_id = ( new Payer_Shipping_Model() )->insert_soft( |
| 93 |
array( |
| 94 |
'payer_id' => $payer_id, |
| 95 |
'full_name' => 'John Doe', |
| 96 |
'address_line_1' => '1 Main St', |
| 97 |
'admin_area_2' => 'San Jose', |
| 98 |
'admin_area_1' => 'CA', |
| 99 |
'postal_code' => 95131, |
| 100 |
'country_code' => 'US', |
| 101 |
) |
| 102 |
); |
| 103 |
|
| 104 |
( new Payment_To_Payer_Shipping_Model() )->insert_soft( |
| 105 |
array( |
| 106 |
'payment_id' => $payment_id, |
| 107 |
'payer_shipping_id' => $shipping_id, |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
if ( 0 === $current % 5000 ) { |
| 112 |
\WP_CLI::log( 'Reached: ' . $current ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
\WP_CLI::success( "Executed successfully ($current)" ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @return int |
| 121 |
* @throws \Exception |
| 122 |
*/ |
| 123 |
protected function get_record_id(): int { |
| 124 |
global $wpdb; |
| 125 |
|
| 126 |
// phpcs:disable WordPress.DB |
| 127 |
$record = $wpdb->get_row( |
| 128 |
$wpdb->prepare( 'SELECT MAX(id) as max, MIN(id) as min FROM %i;', Record_Model::table() ) |
| 129 |
); |
| 130 |
// phpcs:enable WordPress.DB |
| 131 |
|
| 132 |
if ( empty( $record ) ) { |
| 133 |
throw new \Exception( 'empty_records_table' ); |
| 134 |
} |
| 135 |
|
| 136 |
return random_int( $record->min, $record->max ); |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|