-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
Blog #611
Blog #611
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,19 @@ | |
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Throwable; | ||
use Yiisoft\FormModel\FormHydrator; | ||
use Yiisoft\Rbac\Manager; | ||
use Yiisoft\Yii\Console\ExitCode; | ||
|
||
final class CreateCommand extends Command | ||
{ | ||
protected static $defaultName = 'user/create'; | ||
|
||
public function __construct(private SignupForm $signupForm, private Manager $manager) | ||
public function __construct( | ||
private readonly SignupForm $signupForm, | ||
private readonly Manager $manager, | ||
private readonly FormHydrator $formHydrator | ||
) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
@@ -41,25 +46,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
$login = (string) $input->getArgument('login'); | ||
$password = (string) $input->getArgument('password'); | ||
$isAdmin = (bool) $input->getArgument('isAdmin'); | ||
|
||
$this->signupForm->load([ | ||
'login' => $login, | ||
'password' => $password, | ||
'passwordVerify' => $password, | ||
], ''); | ||
|
||
try { | ||
$this->formHydrator->populate(model: $this->signupForm, data: [ | ||
'login' => $login, | ||
'password' => $password, | ||
'passwordVerify' => $password, | ||
], scope: ''); | ||
$user = $this->signupForm->signup(); | ||
} catch (Throwable $t) { | ||
$io->error($t->getMessage() . ' ' . $t->getFile() . ' ' . $t->getLine()); | ||
|
||
return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR; | ||
} | ||
|
||
if ($user === false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Psalm Level 1: Testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommended: if (!$user instanceof User) { |
||
$errors = $this->signupForm->getFormErrors()->getFirstErrors(); | ||
array_walk($errors, fn ($error, $attribute) => $io->error("$attribute: $error")); | ||
|
||
$errors = $this->signupForm->getValidationResult()?->getErrorMessagesIndexedByAttribute(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Psalm Level 1 Testing: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommended: Remove the question mark in getValidationResult? |
||
array_walk($errors, fn($error, $attribute) => $io->error("$attribute: " . implode("\n", (array) $error))); | ||
return ExitCode::DATAERR; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Psalm Level 1 Testing Recommendation: Add the following to support recommendation below:
use App\User\User;