1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Command; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
13
|
|
|
|
14
|
|
|
final class DeleteUserCommand extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var UserRepositoryCollection |
18
|
|
|
*/ |
19
|
|
|
private $userRepositoryCollection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param UserRepositoryCollection $userRepositoryCollection |
23
|
|
|
*/ |
24
|
|
|
public function __construct(UserRepositoryCollection $userRepositoryCollection) |
25
|
|
|
{ |
26
|
|
|
parent::__construct(); |
27
|
|
|
$this->userRepositoryCollection = $userRepositoryCollection; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function configure() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->setName('sumocoders:multiuser:delete') |
34
|
|
|
->setDescription('Delete a user entity') |
35
|
|
|
->addArgument( |
36
|
|
|
'username', |
37
|
|
|
InputArgument::REQUIRED, |
38
|
|
|
'The username of the user' |
39
|
|
|
) |
40
|
|
|
->addOption( |
41
|
|
|
'class', |
42
|
|
|
null, |
43
|
|
|
InputOption::VALUE_OPTIONAL, |
44
|
|
|
'The class off the user', |
45
|
|
|
'SumoCoders\FrameworkMultiUserBundle\User\User' |
46
|
|
|
) |
47
|
|
|
; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$userClass = $input->getOption('class'); |
53
|
|
|
|
54
|
|
|
$availableUserClasses = $this->getAllValidUserClasses(); |
55
|
|
|
|
56
|
|
|
if (count($availableUserClasses) == 1) { |
57
|
|
|
$userClass = $availableUserClasses[0]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!isset($userClass)) { |
61
|
|
|
$helper = $this->getHelper('question'); |
62
|
|
|
$question = new ChoiceQuestion( |
63
|
|
|
'Please select the user class', |
64
|
|
|
$availableUserClasses, |
65
|
|
|
0 |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$userClass = $helper->ask($input, $output, $question); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$repository = $this->getRepository($userClass); |
72
|
|
|
|
73
|
|
|
$username = $input->getArgument('username'); |
74
|
|
|
$user = $repository->findByUsername($username); |
75
|
|
|
|
76
|
|
|
if (!$user) { |
77
|
|
|
$output->writeln('<error>'.$username.' doesn\'t exists'); |
78
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$command = new DeleteUser($user); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$handler = new DeleteUserHandler($repository); |
85
|
|
|
$handler->handle($command); |
86
|
|
|
|
87
|
|
|
$output->writeln($username . ' has been deleted'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws NoRepositoriesRegisteredException |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
private function getAllValidUserClasses() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if (count($this->userRepositoryCollection->all()) === 0) { |
98
|
|
|
throw new NoRepositoriesRegisteredException('No user repositories registered'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$validClasses = []; |
102
|
|
|
|
103
|
|
|
foreach ($this->userRepositoryCollection->all() as $repository) { |
104
|
|
|
$validClasses[] = $repository->getSupportedClass(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $validClasses; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the repository for the user's Class. |
112
|
|
|
* |
113
|
|
|
* @param $userClass |
114
|
|
|
* |
115
|
|
|
* @return UserRepository |
116
|
|
|
*/ |
117
|
|
|
private function getRepository($userClass) |
118
|
|
|
{ |
119
|
|
|
return $this->userRepositoryCollection->findRepositoryByClassName($userClass); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.