ActivateUserCommand::askEmail()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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 ActivateUserCommand 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:activate')
55
            ->setDescription('Activate a user.')
56
            ->setDefinition([
57
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
58
            ])
59
            ->setHelp(<<<EOT
60
The <info>fsi:user:activate</info> command activates the user
61
62
  <info>php app/console fsi:user:activate [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
        $user = $this->userRepository->findUserByEmail($email);
0 ignored issues
show
Bug introduced by
It seems like $email can also be of type null and string[]; however, parameter $email of FSi\Bundle\AdminSecurity...face::findUserByEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $user = $this->userRepository->findUserByEmail(/** @scrutinizer ignore-type */ $email);
Loading history...
72
        if (false === $user instanceof ActivableInterface) {
73
            throw new InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email));
0 ignored issues
show
Bug introduced by
It seems like $email can also be of type string[]; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            throw new InvalidArgumentException(sprintf('User with email "%s" cannot be found', /** @scrutinizer ignore-type */ $email));
Loading history...
74
        }
75
76
        $this->eventDispatcher->dispatch(AdminSecurityEvents::ACTIVATION, new ActivationEvent($user));
0 ignored issues
show
Bug introduced by
FSi\Bundle\AdminSecurity...urityEvents::ACTIVATION of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ AdminSecurityEvents::ACTIVATION, new ActivationEvent($user));
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with new FSi\Bundle\AdminSecu...\ActivationEvent($user). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
77
                                dispatch(AdminSecurityEvents::ACTIVATION, new ActivationEvent($user));

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.

Loading history...
77
        $output->writeln(sprintf('User <comment>%s</comment> has been activated', $email));
78
    }
79
80
    protected function interact(InputInterface $input, OutputInterface $output): void
81
    {
82
        if (!$input->getArgument('email')) {
83
            $this->askEmail($input, $output);
84
        }
85
    }
86
87
    private function askEmail(InputInterface $input, OutputInterface $output): void
88
    {
89
        $question = new Question('Please choose an email:');
90
        $question->setValidator(function (string $email): string {
91
            if (empty($email)) {
92
                throw new Exception('Email can not be empty');
93
            }
94
95
            return $email;
96
        });
97
98
        $email = $this->getQuestionHelper()->ask($input, $output, $question);
99
        $input->setArgument('email', $email);
100
    }
101
102
    private function getQuestionHelper(): QuestionHelper
103
    {
104
        return $this->getHelper('question');
105
    }
106
}
107