Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve random customers data #95

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions includes/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,16 @@ public static function orders( $args, $assoc_args ) {
public static function customers( $args, $assoc_args ) {
list( $amount ) = $args;

$domain = '';

if ( ! empty( $assoc_args['email-domain'] ) ) {
$domain = $assoc_args['email-domain'];
}
Comment on lines +124 to +128
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be consolidated to one line:

Suggested change
$domain = '';
if ( ! empty( $assoc_args['email-domain'] ) ) {
$domain = $assoc_args['email-domain'];
}
$domain = $assoc_args['email-domain'] ?? '';


static::disable_emails();
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating customers', $amount );
for ( $i = 1; $i <= $amount; $i++ ) {
Generator\Customer::generate();
Generator\Customer::generate( true, $domain );
$progress->tick();
}
$progress->finish();
Expand Down Expand Up @@ -242,7 +248,21 @@ public static function coupons( $args, $assoc_args ) {
),
),
) );
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ) );
WP_CLI::add_command( 'wc generate customers', array( 'WC\SmoothGenerator\CLI', 'customers' ), array(
'synopsis' => array(
array(
'name' => 'amount',
'type' => 'positional',
'optional' => true,
'default' => 100,
),
array(
'name' => 'email-domain',
'type' => 'assoc',
'optional' => true,
),
),
) );

WP_CLI::add_command( 'wc generate coupons', array( 'WC\SmoothGenerator\CLI', 'coupons' ), array(
'synopsis' => array(
Expand Down
20 changes: 10 additions & 10 deletions includes/Generator/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ class Customer extends Generator {
/**
* Return a new customer.
*
* @param bool $save Save the object before returning or not.
* @param bool $save Save the object before returning or not.
* @param string $emailDomain An optional domain to be used for all customers' email addresses.
* @return \WC_Customer Customer object with data populated.
*/
public static function generate( $save = true ) {
public static function generate( $save = true, $emailDomain = '' ) {
self::init_faker();

// Make sure a unique username and e-mail are used.
do {
$username = self::$faker->userName();
} while ( username_exists( $username ) );
$safeEmailDomain = $emailDomain ? $emailDomain : self::$faker->safeEmailDomain();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second instance of the variable is redundant.

Suggested change
$safeEmailDomain = $emailDomain ? $emailDomain : self::$faker->safeEmailDomain();
$safeEmailDomain = $emailDomain ?: self::$faker->safeEmailDomain();


// Make sure a unique username and e-mail are used.
do {
$email = self::$faker->safeEmail();
} while ( email_exists( $email ) );
$firstname = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) );
$lastname = self::$faker->lastName();
$username = strtolower( "$firstname.$lastname" );
$email = "$username@$safeEmailDomain";
} while ( username_exists( $username ) || email_exists( $email ) );

$firstname = self::$faker->firstName( self::$faker->randomElement( array( 'male', 'female' ) ) );
$lastname = self::$faker->lastName();
$company = self::$faker->company();
$address1 = self::$faker->buildingNumber() . ' ' . self::$faker->streetName();
$address2 = self::$faker->streetAddress();
Expand Down