fsi-open /
admin-security-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * (c) FSi sp. z o.o. <[email protected]> |
||
| 5 | * |
||
| 6 | * For the full copyright and license information, please view the LICENSE |
||
| 7 | * file that was distributed with this source code. |
||
| 8 | */ |
||
| 9 | |||
| 10 | declare(strict_types=1); |
||
| 11 | |||
| 12 | namespace FSi\Bundle\AdminSecurityBundle\Command; |
||
| 13 | |||
| 14 | use Exception; |
||
| 15 | use FSi\Bundle\AdminSecurityBundle\Event\ActivationEvent; |
||
| 16 | use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents; |
||
| 17 | use FSi\Bundle\AdminSecurityBundle\Security\User\ActivableInterface; |
||
| 18 | use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface; |
||
| 19 | use InvalidArgumentException; |
||
| 20 | use Symfony\Component\Console\Command\Command; |
||
| 21 | use Symfony\Component\Console\Helper\QuestionHelper; |
||
| 22 | use Symfony\Component\Console\Input\InputArgument; |
||
| 23 | use Symfony\Component\Console\Input\InputInterface; |
||
| 24 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 25 | use Symfony\Component\Console\Question\Question; |
||
| 26 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
| 27 | |||
| 28 | class DeactivateUserCommand extends Command |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var UserRepositoryInterface |
||
| 32 | */ |
||
| 33 | private $userRepository; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var EventDispatcherInterface |
||
| 37 | */ |
||
| 38 | private $eventDispatcher; |
||
| 39 | |||
| 40 | public function __construct( |
||
| 41 | UserRepositoryInterface $userRepository, |
||
| 42 | EventDispatcherInterface $eventDispatcher, |
||
| 43 | $name = null |
||
| 44 | ) { |
||
| 45 | parent::__construct($name); |
||
| 46 | |||
| 47 | $this->userRepository = $userRepository; |
||
| 48 | $this->eventDispatcher = $eventDispatcher; |
||
| 49 | } |
||
| 50 | |||
| 51 | protected function configure(): void |
||
| 52 | { |
||
| 53 | $this |
||
| 54 | ->setName('fsi:user:deactivate') |
||
| 55 | ->setDescription('Deactivate a user.') |
||
| 56 | ->setDefinition([ |
||
| 57 | new InputArgument('email', InputArgument::REQUIRED, 'The email'), |
||
| 58 | ]) |
||
| 59 | ->setHelp(<<<EOT |
||
| 60 | The <info>fsi:user:deactivate</info> command deactivates the user |
||
| 61 | |||
| 62 | <info>php app/console fsi:user:deactivate [email protected]</info> |
||
| 63 | |||
| 64 | EOT |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | |||
| 68 | protected function execute(InputInterface $input, OutputInterface $output): void |
||
| 69 | { |
||
| 70 | $email = $input->getArgument('email'); |
||
| 71 | |||
| 72 | $user = $this->userRepository->findUserByEmail($email); |
||
| 73 | if (false === $user instanceof ActivableInterface) { |
||
| 74 | throw new InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email)); |
||
| 75 | } |
||
| 76 | $this->eventDispatcher->dispatch(AdminSecurityEvents::DEACTIVATION, new ActivationEvent($user)); |
||
|
0 ignored issues
–
show
|
|||
| 77 | |||
| 78 | $output->writeln(sprintf('User <comment>%s</comment> has been deactivated', $email)); |
||
| 79 | } |
||
| 80 | |||
| 81 | protected function interact(InputInterface $input, OutputInterface $output): void |
||
| 82 | { |
||
| 83 | if (!$input->getArgument('email')) { |
||
| 84 | $this->askEmail($input, $output); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | private function askEmail(InputInterface $input, OutputInterface $output): void |
||
| 89 | { |
||
| 90 | $question = new Question('Please choose an email:'); |
||
| 91 | $question->setValidator(function (string $email): string { |
||
| 92 | if (empty($email)) { |
||
| 93 | throw new Exception('Email can not be empty'); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $email; |
||
| 97 | }); |
||
| 98 | |||
| 99 | $email = $this->getQuestionHelper()->ask($input, $output, $question); |
||
| 100 | $input->setArgument('email', $email); |
||
| 101 | } |
||
| 102 | |||
| 103 | private function getQuestionHelper(): QuestionHelper |
||
| 104 | { |
||
| 105 | return $this->getHelper('question'); |
||
| 106 | } |
||
| 107 | } |
||
| 108 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.