Skip to content

Commit

Permalink
Merge pull request #3276 from craftcms/feature/data-transfer-cli-eols
Browse files Browse the repository at this point in the history
Customer data transfer CLI messaging
  • Loading branch information
lukeholder committed Sep 13, 2023
2 parents a86952f + 9f55edb commit e08e474
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/console/controllers/TransferCustomerDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function options($actionID): array
*/
public function actionIndex(): int
{
$this->stdout("This command will transfer all commerce data from one user to another.\n");
$this->stdout('This command will transfer all commerce data from one user to another.' . PHP_EOL);

$this->fromUser = $this->prompt('Move Commerce data from user (email or username):', [
'required' => true,
Expand All @@ -61,36 +61,47 @@ public function actionIndex(): int
]);

if ($this->fromUser === '' || $this->toUser === '') {
$this->stderr("No 'fromUser' or 'toUser' specified.\n", Console::FG_RED);
$this->stderr('You must specify both a “to” and “from” user.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$fromUser = Craft::$app->getUsers()->getUserByUsernameOrEmail($this->fromUser);
$toUser = Craft::$app->getUsers()->getUserByUsernameOrEmail($this->toUser);

if ($fromUser === null) {
$this->stderr("No user with {$this->fromUser} found.\n", Console::FG_RED);
$this->stderr("No user found with a username or email of `{$this->fromUser}`" . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

if ($toUser === null) {
$this->stderr("No user with {$this->toUser} found.\n", Console::FG_RED);
$this->stderr("No user found with a username or email of `{$this->toUser}`" . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$confirm = $this->confirm('Are you sure you want to move all Commerce data from user: ' . $this->fromUser . ' to user: ' . $this->toUser . '? (y/n)');
// Make sure they're not the same!
if ($fromUser->id === $toUser->id) {
$this->stderr('The transfer must happen between different users.' . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$confirm = $this->confirm('Are you sure you want to move all Commerce data from user: ' . $this->fromUser . ' to user: ' . $this->toUser . '?');
if (!$confirm) {
$this->stdout('Aborting.');
$this->stdout('No data will be moved.', Console::FG_YELLOW);
return ExitCode::OK;
}

$this->stdout('Moving data... ');

try {
Plugin::getInstance()->getCustomers()->transferCustomerData($fromUser, $toUser);
} catch (Exception $e) {
$this->stderr($e->getMessage() . "\n", Console::FG_RED);
$this->stderr('failed!' . PHP_EOL, Console::FG_RED);
$this->stderr($e->getMessage() . PHP_EOL, Console::FG_RED);
return ExitCode::UNSPECIFIED_ERROR;
}

$this->stdout('done!' . PHP_EOL, Console::FG_GREEN);

return ExitCode::OK;
}
}

0 comments on commit e08e474

Please sign in to comment.