Completed
Push — master ( d67956...a656c9 )
by Piotr
11s
created

ActivateUserCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 61
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
A interact() 0 15 3
A execute() 0 16 2
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
namespace FSi\Bundle\AdminSecurityBundle\Command;
11
12
use FSi\Bundle\AdminSecurityBundle\Event\ActivationEvent;
13
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
14
use FSi\Bundle\AdminSecurityBundle\Security\User\ActivableInterface;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class ActivateUserCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * @see Command
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('fsi:user:activate')
29
            ->setDescription('Activate a user.')
30
            ->setDefinition([
31
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
32
            ])
33
            ->setHelp(<<<EOT
34
The <info>fsi:user:activate</info> command activates the user
35
36
  <info>php app/console fsi:user:activate [email protected]</info>
37
38
EOT
39
            );
40
    }
41
42
    /**
43
     * @see Command
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $email = $input->getArgument('email');
48
49
        $userRepository = $this->getContainer()->get('admin_security.repository.user');
50
        $user = $userRepository->findUserByEmail($email);
51
        if (!($user instanceof ActivableInterface)) {
52
            throw new \InvalidArgumentException(sprintf('User with email "%s" cannot be found', $email));
53
        }
54
55
        $this->getContainer()->get('event_dispatcher')->dispatch(
56
            AdminSecurityEvents::ACTIVATION,
57
            new ActivationEvent($user)
58
        );
59
60
        $output->writeln(sprintf('User <comment>%s</comment> has been deactivated', $email));
61
    }
62
63
    /**
64
     * @see Command
65
     */
66
    protected function interact(InputInterface $input, OutputInterface $output)
67
    {
68
        if (!$input->getArgument('email')) {
69
            $email = $this->getHelper('dialog')->askAndValidate(
70
                $output,
71
                'Please choose an email:',
72
                function($email) {
73
                    if (empty($email)) {
74
                        throw new \Exception('Email can not be empty');
75
                    }
76
77
                    return $email;
78
                }
79
            );
80
            $input->setArgument('email', $email);
81
        }
82
    }
83
}
84