1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the awurth/silex-user package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexis Wurth <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AWurth\Silex\User\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Question\Question; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Antoine Hérault <[email protected]> |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class ActivateUserCommand extends ContainerAwareCommand |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this |
30
|
|
|
->setName('silex-user:activate') |
31
|
|
|
->setDescription('Activate a user') |
32
|
|
|
->setDefinition([ |
33
|
|
|
new InputArgument('username', InputArgument::REQUIRED, 'The username') |
34
|
|
|
]) |
35
|
|
|
->setHelp(<<<'EOT' |
36
|
|
|
The <info>silex-user:activate</info> command activates a user (so they will be able to log in): |
37
|
|
|
|
38
|
|
|
<info>php %command.full_name% matthieu</info> |
39
|
|
|
EOT |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
$username = $input->getArgument('username'); |
49
|
|
|
|
50
|
|
|
$manipulator = $this->container['silex_user.util.user_manipulator']; |
51
|
|
|
$manipulator->activate($username); |
52
|
|
|
|
53
|
|
|
$output->writeln(sprintf('User "%s" has been activated.', $username)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
60
|
|
|
{ |
61
|
|
|
if (!$input->getArgument('username')) { |
62
|
|
|
$question = new Question('Please choose a username:'); |
63
|
|
|
$question->setValidator(function ($username) { |
64
|
|
|
if (empty($username)) { |
65
|
|
|
throw new \Exception('Username can not be empty'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $username; |
69
|
|
|
}); |
70
|
|
|
$answer = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$input->setArgument('username', $answer); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.