1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Command; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
final class CreateUserCommand extends ContainerAwareCommand |
13
|
|
|
{ |
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName('sumocoders:multiuser:create') |
18
|
|
|
->setDescription('Create a user entity') |
19
|
|
|
->addArgument( |
20
|
|
|
'username', |
21
|
|
|
InputArgument::REQUIRED, |
22
|
|
|
'The username of the user' |
23
|
|
|
) |
24
|
|
|
->addArgument( |
25
|
|
|
'password', |
26
|
|
|
InputArgument::REQUIRED, |
27
|
|
|
'The password for the user' |
28
|
|
|
) |
29
|
|
|
->addArgument( |
30
|
|
|
'displayName', |
31
|
|
|
InputArgument::REQUIRED, |
32
|
|
|
'The display name for the user' |
33
|
|
|
) |
34
|
|
|
->addOption( |
35
|
|
|
'class', |
36
|
|
|
null, |
37
|
|
|
InputOption::VALUE_OPTIONAL, |
38
|
|
|
'The class off the user', |
39
|
|
|
'SumoCoders\FrameworkMultiUserBundle\User\User' |
40
|
|
|
) |
41
|
|
|
; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$userClass = $input->getOption('class'); |
47
|
|
|
|
48
|
|
|
$repository = $this->getRepository($userClass); |
49
|
|
|
|
50
|
|
|
$handler = new CreateUserHandler($repository); |
51
|
|
|
|
52
|
|
|
$username = $input->getArgument('username'); |
53
|
|
|
$password = $input->getArgument('password'); |
54
|
|
|
$displayName = $input->getArgument('displayName'); |
55
|
|
|
|
56
|
|
|
$command = new CreateUser($username, $password, $displayName); |
57
|
|
|
|
58
|
|
|
$handler->handle($command); |
59
|
|
|
|
60
|
|
|
$output->writeln($username . ' has been created'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the repository for the user's Class. |
65
|
|
|
* |
66
|
|
|
* @param $userClass |
67
|
|
|
* |
68
|
|
|
* @return UserRepository |
69
|
|
|
*/ |
70
|
|
|
private function getRepository($userClass) |
71
|
|
|
{ |
72
|
|
|
return $this->getContainer()->get('multi_user.user_repository.collection')->findRepositoryByClassName($userClass); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.