Completed
Pull Request — master (#10)
by
unknown
02:54
created

DeleteUserCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.3142
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class DeleteUserCommand.
12
 */
13
class DeleteUserCommand extends ContainerAwareCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('sumocoders_multiuser:delete')
19
            ->setDescription('Delete a user entity')
20
            ->addArgument(
21
                'username',
22
                InputArgument::REQUIRED,
23
                'The username of the user'
24
            )
25
        ;
26
    }
27
28
    protected function execute(InputInterface $input, OutputInterface $output)
29
    {
30
        $repository = $this->getContainer()->get('multi_user.user.repository');
31
32
        $handler = new DeleteUserHandler($repository);
33
34
        $username = $input->getArgument('username');
35
        $user = $repository->findByUsername($username);
36
37
        if (!$user) {
38
            $output->writeln('<error>'.$username.' doesn\'t exists');
39
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
40
        }
41
42
        $command = new DeleteUser();
43
        $command->user = $user;
44
45
        $handler->handle($command);
46
47
        $output->writeln($username . ' has been deleted');
48
    }
49
}
50