| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\TestData\Commands; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\TestData\Factories\DonationFormFactory; |
| 7 |
use Give\TestData\Repositories\DonationFormRepository; |
| 8 |
use WP_CLI; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class FormSeedCommand |
| 12 |
* @package Give\TestData\Commands |
| 13 |
* |
| 14 |
* A WP-CLI command to generate Donation Forms |
| 15 |
*/ |
| 16 |
class FormSeedCommand { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var DonationFormFactory |
| 20 |
*/ |
| 21 |
private $donationFormFactory; |
| 22 |
/** |
| 23 |
* @var DonationFormRepository |
| 24 |
*/ |
| 25 |
private $donationFormRepository; |
| 26 |
|
| 27 |
public function __construct( |
| 28 |
DonationFormFactory $donationFormFactory, |
| 29 |
DonationFormRepository $donationFormRepository |
| 30 |
) { |
| 31 |
$this->donationFormFactory = $donationFormFactory; |
| 32 |
$this->donationFormRepository = $donationFormRepository; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Generate Donation Forms |
| 37 |
* |
| 38 |
* ## OPTIONS |
| 39 |
* |
| 40 |
* [--count=<count>] |
| 41 |
* : Number of donations to generate |
| 42 |
* default: 10 |
| 43 |
* |
| 44 |
* [--template=<template>] |
| 45 |
* : Form template |
| 46 |
* default: random |
| 47 |
* options: |
| 48 |
* - sequoia |
| 49 |
* - legacy |
| 50 |
* - random |
| 51 |
* |
| 52 |
* [--set-goal=<bool>] |
| 53 |
* : Set donation form goal |
| 54 |
* default: false |
| 55 |
* |
| 56 |
* [--set-terms=<bool>] |
| 57 |
* : Set donation form terms and conditions |
| 58 |
* default: false |
| 59 |
* |
| 60 |
* [--preview=<preview>] |
| 61 |
* : Preview generated data |
| 62 |
* default: false |
| 63 |
* |
| 64 |
* [--consistent=<consistent>] |
| 65 |
* : Generate consistent data |
| 66 |
* default: false |
| 67 |
* |
| 68 |
* ## EXAMPLES |
| 69 |
* |
| 70 |
* wp give test-donation-form --count=10 --template=legacy --set-goal=true --set-terms=true |
| 71 |
* |
| 72 |
* @when after_wp_load |
| 73 |
*/ |
| 74 |
public function __invoke( $args, $assocArgs ) { |
| 75 |
global $wpdb; |
| 76 |
|
| 77 |
// Get CLI args |
| 78 |
$count = WP_CLI\Utils\get_flag_value( $assocArgs, 'count', $default = 10 ); |
| 79 |
$preview = WP_CLI\Utils\get_flag_value( $assocArgs, 'preview', $default = false ); |
| 80 |
$template = WP_CLI\Utils\get_flag_value( $assocArgs, 'template', $default = 'random' ); |
| 81 |
$setGoal = WP_CLI\Utils\get_flag_value( $assocArgs, 'set-goal', $default = false ); |
| 82 |
$setTerms = WP_CLI\Utils\get_flag_value( $assocArgs, 'set-terms', $default = false ); |
| 83 |
$consistent = WP_CLI\Utils\get_flag_value( $assocArgs, 'consistent', $default = false ); |
| 84 |
|
| 85 |
// Check form template |
| 86 |
if ( ! $this->donationFormFactory->checkFormTemplate( $template ) ) { |
| 87 |
WP_CLI::error( |
| 88 |
WP_CLI::colorize( "Unsupported form template: %g{$template}%n" ) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
// Factory config |
| 93 |
$this->donationFormFactory->setFormTemplate( $template ); |
| 94 |
$this->donationFormFactory->setDonationFormGoal( $setGoal ); |
| 95 |
$this->donationFormFactory->setTermsAndConditions( $setTerms ); |
| 96 |
|
| 97 |
// Generate donation forms |
| 98 |
$forms = $this->donationFormFactory->consistent( $consistent )->make( $count ); |
| 99 |
|
| 100 |
if ( $preview ) { |
| 101 |
WP_CLI\Utils\format_items( |
| 102 |
'table', |
| 103 |
$forms, |
| 104 |
array_keys( $this->donationFormFactory->definition() ) |
| 105 |
); |
| 106 |
} else { |
| 107 |
$progress = WP_CLI\Utils\make_progress_bar( 'Generating donation forms', $count ); |
| 108 |
|
| 109 |
// Start DB transaction |
| 110 |
$wpdb->query( 'START TRANSACTION' ); |
| 111 |
|
| 112 |
try { |
| 113 |
|
| 114 |
foreach ( $forms as $form ) { |
| 115 |
$this->donationFormRepository->insertDonationForm( $form ); |
| 116 |
$progress->tick(); |
| 117 |
} |
| 118 |
|
| 119 |
$wpdb->query( 'COMMIT' ); |
| 120 |
|
| 121 |
$progress->finish(); |
| 122 |
|
| 123 |
} catch ( Exception $e ) { |
| 124 |
$wpdb->query( 'ROLLBACK' ); |
| 125 |
|
| 126 |
WP_CLI::error( $e->getMessage() ); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|