-
Notifications
You must be signed in to change notification settings - Fork 2
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
Command, allow to search User by Email #4
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -35,6 +35,7 @@ protected function configure() | |
->setName('biig:jwt:generate') | ||
->setDescription('Generate the first user JWT Token.') | ||
->addOption('role', 'r', InputOption::VALUE_OPTIONAL, 'Specify what role you want') | ||
->addOption('email', null, InputOption::VALUE_OPTIONAL, 'Specify what email the user should have') | ||
->setHelp(<<<HELP | ||
This command is only to use in the context of the tests. | ||
|
||
|
@@ -62,23 +63,20 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
|
||
$user = reset($users); | ||
|
||
if ($input->hasOption('role') | ||
if ($input->hasOption('email') | ||
&& null !== $input->getOption('email')) | ||
{ | ||
$user = $this->findByEmail($input->getOption('email'), $output, $user, $users); | ||
} | ||
else if ($input->hasOption('role') | ||
&& null !== $input->getOption('role') | ||
&& !in_array($input->getOption('role'), $user->getRoles()) | ||
) { | ||
$find = false; | ||
foreach ($users as $item) { | ||
if (in_array($input->getOption('role'), $item->getRoles())) { | ||
$user = $item; | ||
$find = true; | ||
} | ||
} | ||
|
||
if (!$find) { | ||
$output->writeln(sprintf('<error>User with role "%s" doesn\'t exist.<error>', $input->getOption('role'))); | ||
&& !in_array($input->getOption('role'), $user->getRoles())) | ||
{ | ||
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. 👀 same here |
||
$user = $this->findOneByRole($input, $output, $users); | ||
} | ||
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. If we specify email and role, role will be ignored. This is due to refactoring in splitted methods. You probably should consider to add an error message or drop this refactoring and use a single (fat, I admit) condition. 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. I'm assuming that if you use the 'email' flag you must know which user you want and therefore you need to know which role this user have |
||
|
||
return; | ||
} | ||
if ($user === null) { | ||
return ; | ||
} | ||
|
||
$token = $this->generator->create($user); | ||
|
@@ -89,4 +87,51 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
'Token: ' . $token, | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $email | ||
* @param OutputInterface $output | ||
* @param $user | ||
* @param array $users | ||
* | ||
* @return mixed | ||
*/ | ||
private function findByEmail(string $email, OutputInterface $output, $user, array $users) | ||
{ | ||
if (!method_exists($user, 'getEmail')) { | ||
$output->writeln('<error>User Class does not have a getEmail() method<error>'); | ||
|
||
return null; | ||
} | ||
|
||
foreach ($users as $item) { | ||
if ($email === $item->getEmail()) { | ||
return $item; | ||
} | ||
} | ||
|
||
$output->writeln(sprintf('<error>User with email "%s" doesn\'t exist.<error>', $input->getOption('email'))); | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param string $role | ||
* @param OutputInterface $output | ||
* @param array $users | ||
* | ||
* @return mixed | ||
*/ | ||
protected function findOneByRole(string $role, OutputInterface $output, array $users) | ||
{ | ||
foreach ($users as $item) { | ||
if (in_array($role, $item->getRoles())) { | ||
return $item; | ||
} | ||
} | ||
|
||
$output->writeln(sprintf('<error>User with role "%s" doesn\'t exist.<error>', $role)); | ||
|
||
return null; | ||
} | ||
} |
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.
wow, how did it happen like that? Please fix indentation.