1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Command; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
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
|
|
|
|
13
|
|
|
final class DeleteUserCommand extends ContainerAwareCommand |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var UserRepositoryCollection |
17
|
|
|
*/ |
18
|
|
|
private $userRepositoryCollection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param UserRepositoryCollection $userRepositoryCollection |
22
|
|
|
*/ |
23
|
|
|
public function __construct(UserRepositoryCollection $userRepositoryCollection) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
$this->userRepositoryCollection = $userRepositoryCollection; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('sumocoders:multiuser:delete') |
33
|
|
|
->setDescription('Delete a user entity') |
34
|
|
|
->addArgument( |
35
|
|
|
'username', |
36
|
|
|
InputArgument::REQUIRED, |
37
|
|
|
'The username of the user' |
38
|
|
|
) |
39
|
|
|
->addOption( |
40
|
|
|
'class', |
41
|
|
|
null, |
42
|
|
|
InputOption::VALUE_OPTIONAL, |
43
|
|
|
'The class off the user', |
44
|
|
|
'SumoCoders\FrameworkMultiUserBundle\User\User' |
45
|
|
|
) |
46
|
|
|
; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$userClass = $input->getOption('class'); |
52
|
|
|
|
53
|
|
|
$repository = $this->getRepository($userClass); |
54
|
|
|
|
55
|
|
|
$username = $input->getArgument('username'); |
56
|
|
|
$user = $repository->findByUsername($username); |
57
|
|
|
|
58
|
|
|
if (!$user) { |
59
|
|
|
$output->writeln('<error>'.$username.' doesn\'t exists'); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$command = new DeleteUser($user); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$handler = new DeleteUserHandler($repository); |
66
|
|
|
$handler->handle($command); |
67
|
|
|
|
68
|
|
|
$output->writeln($username . ' has been deleted'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the repository for the user's Class. |
73
|
|
|
* |
74
|
|
|
* @param $userClass |
75
|
|
|
* |
76
|
|
|
* @return UserRepository |
77
|
|
|
*/ |
78
|
|
|
private function getRepository($userClass) |
79
|
|
|
{ |
80
|
|
|
return $this->userRepositoryCollection->findRepositoryByClassName($userClass); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.