From 9d3a87359a3cdfc0744426d0cc6dee76beaa80f5 Mon Sep 17 00:00:00 2001 From: August Miller Date: Tue, 12 Sep 2023 17:39:38 -0700 Subject: [PATCH 1/2] Use PHP_EOL, check for to/from difference, improve messaging --- .../TransferCustomerDataController.php | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/console/controllers/TransferCustomerDataController.php b/src/console/controllers/TransferCustomerDataController.php index 9d8a65417e..05702edd7b 100644 --- a/src/console/controllers/TransferCustomerDataController.php +++ b/src/console/controllers/TransferCustomerDataController.php @@ -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, @@ -61,7 +61,7 @@ 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; } @@ -69,28 +69,39 @@ public function actionIndex(): int $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!', Console::FG_GREEN); + return ExitCode::OK; } } From 9f55edb50960d67e9a4c7d5f84b74b14993da0e0 Mon Sep 17 00:00:00 2001 From: August Miller Date: Tue, 12 Sep 2023 17:42:47 -0700 Subject: [PATCH 2/2] One more PHP_EOL! --- src/console/controllers/TransferCustomerDataController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/console/controllers/TransferCustomerDataController.php b/src/console/controllers/TransferCustomerDataController.php index 05702edd7b..125623b309 100644 --- a/src/console/controllers/TransferCustomerDataController.php +++ b/src/console/controllers/TransferCustomerDataController.php @@ -100,7 +100,7 @@ public function actionIndex(): int return ExitCode::UNSPECIFIED_ERROR; } - $this->stdout('done!', Console::FG_GREEN); + $this->stdout('done!' . PHP_EOL, Console::FG_GREEN); return ExitCode::OK; }