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

CreateUserCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 18
loc 18
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
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 CreateUserCommand 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:create')
33
            ->setDescription('Create a user entity')
34
            ->addArgument(
35
                'username',
36
                InputArgument::REQUIRED,
37
                'The username of the user'
38
            )
39
            ->addArgument(
40
                'password',
41
                InputArgument::REQUIRED,
42
                'The password for the user'
43
            )
44
            ->addArgument(
45
                'displayName',
46
                InputArgument::REQUIRED,
47
                'The display name for the user'
48
            )
49
            ->addOption(
50
                'class',
51
                null,
52
                InputOption::VALUE_OPTIONAL,
53
                'The class off the user',
54
                'SumoCoders\FrameworkMultiUserBundle\User\User'
55
            )
56
        ;
57
    }
58
59 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
60
    {
61
        $userClass = $input->getOption('class');
62
63
        $repository = $this->getRepository($userClass);
64
65
        $handler = new CreateUserHandler($repository);
66
67
        $username = $input->getArgument('username');
68
        $password = $input->getArgument('password');
69
        $displayName = $input->getArgument('displayName');
70
71
        $command = new CreateUser($username, $password, $displayName);
72
73
        $handler->handle($command);
74
75
        $output->writeln($username . ' has been created');
76
    }
77
78
    /**
79
     * Get the repository for the user's Class.
80
     *
81
     * @param $userClass
82
     *
83
     * @return UserRepository
84
     */
85
    private function getRepository($userClass)
86
    {
87
        return $this->userRepositoryCollection->findRepositoryByClassName($userClass);
88
    }
89
}
90