PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / TestData / Addons / Funds / FundCommand.php

FundCommand.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/TestData/Addons/Funds/FundCommand.php

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