Completed
Pull Request — master (#10)
by
unknown
03:16
created

DeleteUserCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
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\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 DeleteUserCommand extends UserCommand
13
{
14
    /**
15
     * @var UserRepositoryCollection
16
     */
17
    private $userRepositoryCollection;
18
19
    /**
20
     * @var DeleteUserHandler
21
     */
22
    private $handler;
23
24
    /**
25
     * DeleteUserCommand constructor.
26
     *
27
     * @param UserRepositoryCollection $userRepositoryCollection
28
     * @param DeleteUserHandler $handler
29
     */
30
    public function __construct(UserRepositoryCollection $userRepositoryCollection, DeleteUserHandler $handler)
31
    {
32
        parent::__construct();
33
        $this->userRepositoryCollection = $userRepositoryCollection;
34
        $this->handler = $handler;
35
    }
36
37
    protected function configure()
38
    {
39
        $this
40
            ->setName('sumocoders:multiuser:delete')
41
            ->setDescription('Delete a user entity')
42
            ->addArgument(
43
                'username',
44
                InputArgument::REQUIRED,
45
                'The username of the user'
46
            )
47
            ->addOption(
48
                'class',
49
                null,
50
                InputOption::VALUE_OPTIONAL,
51
                'The class off the user',
52
                'SumoCoders\FrameworkMultiUserBundle\User\User'
53
            )
54
        ;
55
    }
56
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $availableUserClasses = $this->getAllValidUserClasses($this->userRepositoryCollection);
60
61
        $userClass = $this->getUserClass($input, $output, $availableUserClasses);
62
63
        $repository = $this->getRepository($userClass);
64
65
        $username = $input->getArgument('username');
66
        $user = $repository->findByUsername($username);
67
68
        if (!$user) {
69
            $output->writeln('<error>'.$username.' doesn\'t exists');
70
71
            return;
72
        }
73
74
        $command = new DeleteUser($user);
75
76
        $this->handler->handle($command);
77
78
        $output->writeln($username . ' has been deleted');
79
    }
80
81
    /**
82
     * Get the repository for the user's Class.
83
     *
84
     * @param $userClass
85
     *
86
     * @return UserRepository
87
     */
88
    private function getRepository($userClass)
89
    {
90
        return $this->userRepositoryCollection->findRepositoryByClassName($userClass);
91
    }
92
}
93