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

CreateUserCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 42.86 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 8
c 7
b 0
f 2
lcom 1
cbo 5
dl 48
loc 112
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B configure() 0 28 1
B execute() 35 35 3
A getAllValidUserClasses() 13 13 2
A getRepository() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Command;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\NoRepositoriesRegisteredException;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserRepository;
7
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
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\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ChoiceQuestion;
14
15
final class CreateUserCommand extends Command
16
{
17
    /**
18
     * @var UserRepositoryCollection
19
     */
20
    private $userRepositoryCollection;
21
22
    /**
23
     * @param UserRepositoryCollection $userRepositoryCollection
24
     */
25
    public function __construct(UserRepositoryCollection $userRepositoryCollection)
26
    {
27
        parent::__construct();
28
        $this->userRepositoryCollection = $userRepositoryCollection;
29
    }
30
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('sumocoders:multiuser:create')
35
            ->setDescription('Create a user entity')
36
            ->addArgument(
37
                'username',
38
                InputArgument::REQUIRED,
39
                'The username of the user'
40
            )
41
            ->addArgument(
42
                'password',
43
                InputArgument::REQUIRED,
44
                'The password for the user'
45
            )
46
            ->addArgument(
47
                'displayName',
48
                InputArgument::REQUIRED,
49
                'The display name for the user'
50
            )
51
            ->addOption(
52
                'class',
53
                null,
54
                InputOption::VALUE_OPTIONAL,
55
                'The class off the user'
56
            )
57
        ;
58
    }
59
60 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...
61
    {
62
        $userClass = $input->getOption('class');
63
64
        $availableUserClasses = $this->getAllValidUserClasses();
65
66
        if (count($availableUserClasses) == 1) {
67
            $userClass = $availableUserClasses[0];
68
        }
69
70
        if (!isset($userClass)) {
71
            $helper = $this->getHelper('question');
72
            $question = new ChoiceQuestion(
73
                'Please select the user class',
74
                $availableUserClasses,
75
                0
76
            );
77
78
            $userClass = $helper->ask($input, $output, $question);
79
        }
80
81
        $repository = $this->getRepository($userClass);
82
83
        $handler = new CreateUserHandler($repository);
84
85
        $username = $input->getArgument('username');
86
        $password = $input->getArgument('password');
87
        $displayName = $input->getArgument('displayName');
88
89
        $command = new CreateUser($username, $password, $displayName);
90
91
        $handler->handle($command);
92
93
        $output->writeln($username . ' has been created');
94
    }
95
96
    /**
97
     * @throws NoRepositoriesRegisteredException
98
     *
99
     * @return array
100
     */
101 View Code Duplication
    private function getAllValidUserClasses()
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...
102
    {
103
        if (count($this->userRepositoryCollection->all()) === 0) {
104
            throw new NoRepositoriesRegisteredException('No user repositories registered');
105
        }
106
107
        return array_map(
108
            function (UserRepository $repository) {
109
                return $repository->getSupportedClass();
110
            },
111
            $this->userRepositoryCollection->all()
112
        );
113
    }
114
115
    /**
116
     * Get the repository for the user's Class.
117
     *
118
     * @param $userClass
119
     *
120
     * @return UserRepository
121
     */
122
    private function getRepository($userClass)
123
    {
124
        return $this->userRepositoryCollection->findRepositoryByClassName($userClass);
125
    }
126
}
127