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