FundCommand.php
5 years ago
FundFactory.php
5 years ago
FundRepository.php
5 years ago
ServiceProvider.php
5 years ago
FundCommand.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\TestData\Addons\Funds; |
| 4 | |
| 5 | use WP_CLI; |
| 6 | use Throwable; |
| 7 | |
| 8 | /** |
| 9 | * Class FundCommand |
| 10 | * @package Give\TestData\Funds |
| 11 | * |
| 12 | * A WP-CLI command for seeding funds. |
| 13 | */ |
| 14 | class FundCommand { |
| 15 | /** |
| 16 | * @var FundFactory |
| 17 | */ |
| 18 | private $fundFactory; |
| 19 | /** |
| 20 | * @var FundRepository |
| 21 | */ |
| 22 | private $fundsRepository; |
| 23 | |
| 24 | /** |
| 25 | * @param FundFactory $fundFactory |
| 26 | * @param FundRepository $fundsRepository |
| 27 | */ |
| 28 | public function __construct( FundFactory $fundFactory, FundRepository $fundsRepository ) { |
| 29 | $this->fundFactory = $fundFactory; |
| 30 | $this->fundsRepository = $fundsRepository; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Generates Funds |
| 35 | * |
| 36 | * ## OPTIONS |
| 37 | * [--count=<count>] |
| 38 | * : Number of funds to generate |
| 39 | * default: 5 |
| 40 | * |
| 41 | * [--preview=<preview>] |
| 42 | * : Preview generated data |
| 43 | * default: false |
| 44 | * |
| 45 | * [--consistent=<consistent>] |
| 46 | * : Generate consistent data |
| 47 | * default: false |
| 48 | * |
| 49 | * ## EXAMPLES |
| 50 | * |
| 51 | * wp give test-funds --count=5 --preview=true |
| 52 | * |
| 53 | * @when after_wp_load |
| 54 | */ |
| 55 | public function __invoke( $args, $assocArgs ) { |
| 56 | global $wpdb; |
| 57 | // Get CLI args |
| 58 | $count = WP_CLI\Utils\get_flag_value( $assocArgs, 'count', $default = 5 ); |
| 59 | $preview = WP_CLI\Utils\get_flag_value( $assocArgs, 'preview', $default = false ); |
| 60 | $consistent = WP_CLI\Utils\get_flag_value( $assocArgs, 'consistent', $default = null ); |
| 61 | |
| 62 | $funds = $this->fundFactory->consistent( $consistent )->make( $count ); |
| 63 | |
| 64 | if ( $preview ) { |
| 65 | WP_CLI\Utils\format_items( |
| 66 | 'table', |
| 67 | $funds, |
| 68 | array_keys( $this->fundFactory->definition() ) |
| 69 | ); |
| 70 | } else { |
| 71 | $progress = WP_CLI\Utils\make_progress_bar( 'Generating funds', $count ); |
| 72 | |
| 73 | try { |
| 74 | |
| 75 | foreach ( $funds as $fund ) { |
| 76 | $this->fundsRepository->insertFund( $fund ); |
| 77 | $progress->tick(); |
| 78 | } |
| 79 | |
| 80 | $wpdb->query( 'COMMIT' ); |
| 81 | |
| 82 | $progress->finish(); |
| 83 | |
| 84 | } catch ( Throwable $e ) { |
| 85 | $wpdb->query( 'ROLLBACK' ); |
| 86 | |
| 87 | WP_CLI::error( $e->getMessage() ); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 |