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

Command, allow to search User by Email #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
75 changes: 60 additions & 15 deletions src/Jwt/Command/JwtGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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'))
{
Copy link
Contributor

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.

$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()))
{
Copy link
Contributor

Choose a reason for hiding this comment

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

👀 same here

$user = $this->findOneByRole($input, $output, $users);
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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);
Expand All @@ -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;
}
}