|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Console; |
|
4
|
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Command\DeleteUserHandler; |
|
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\Form\BaseUser; |
|
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
final class DeleteUserCommand extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var UserRepository |
|
17
|
|
|
*/ |
|
18
|
|
|
private $userRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var DeleteUserHandler |
|
22
|
|
|
*/ |
|
23
|
|
|
private $handler; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* DeleteUserCommand constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param UserRepository $userRepository |
|
29
|
|
|
* @param DeleteUserHandler $handler |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(UserRepository $userRepository, DeleteUserHandler $handler) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
$this->userRepository = $userRepository; |
|
35
|
|
|
$this->handler = $handler; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function configure() |
|
39
|
|
|
{ |
|
40
|
|
|
$this |
|
41
|
|
|
->setName('sumocoders:multiuser:delete') |
|
42
|
|
|
->setDescription('Delete a user entity') |
|
43
|
|
|
->addArgument( |
|
44
|
|
|
'username', |
|
45
|
|
|
InputArgument::REQUIRED, |
|
46
|
|
|
'The username of the user' |
|
47
|
|
|
) |
|
48
|
|
|
; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
52
|
|
|
{ |
|
53
|
|
|
$username = $input->getArgument('username'); |
|
54
|
|
|
$user = $this->userRepository->findByUsername($username); |
|
55
|
|
|
|
|
56
|
|
|
if (!$user) { |
|
57
|
|
|
$output->writeln('<error>'.$username.' doesn\'t exists'); |
|
58
|
|
|
|
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$baseUserTransferObject = BaseUser::fromUser($user); |
|
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
$this->handler->handle($baseUserTransferObject); |
|
65
|
|
|
|
|
66
|
|
|
$output->writeln($username . ' has been deleted'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.