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

DeleteUserCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 48.6 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 8
Bugs 0 Features 2
Metric Value
wmc 9
c 8
b 0
f 2
lcom 1
cbo 5
dl 52
loc 107
rs 10

5 Methods

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

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 DeleteUserCommand 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:delete')
35
            ->setDescription('Delete a user entity')
36
            ->addArgument(
37
                'username',
38
                InputArgument::REQUIRED,
39
                'The username of the user'
40
            )
41
            ->addOption(
42
                'class',
43
                null,
44
                InputOption::VALUE_OPTIONAL,
45
                'The class off the user',
46
                'SumoCoders\FrameworkMultiUserBundle\User\User'
47
            )
48
        ;
49
    }
50
51 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...
52
    {
53
        $userClass = $input->getOption('class');
54
55
        $availableUserClasses = $this->getAllValidUserClasses();
56
57
        if (count($availableUserClasses) == 1) {
58
            $userClass = $availableUserClasses[0];
59
        }
60
61
        if (!isset($userClass)) {
62
            $helper = $this->getHelper('question');
63
            $question = new ChoiceQuestion(
64
                'Please select the user class',
65
                $availableUserClasses,
66
                0
67
            );
68
69
            $userClass = $helper->ask($input, $output, $question);
70
        }
71
72
        $repository = $this->getRepository($userClass);
73
74
        $username = $input->getArgument('username');
75
        $user = $repository->findByUsername($username);
76
77
        if (!$user) {
78
            $output->writeln('<error>'.$username.' doesn\'t exists');
79
80
            return;
81
        }
82
83
        $command = new DeleteUser($user);
0 ignored issues
show
Compatibility introduced by
$user of type object<Symfony\Component...ore\User\UserInterface> is not a sub-type of object<SumoCoders\Framew...tiUserBundle\User\User>. It seems like you assume a concrete implementation of the interface Symfony\Component\Security\Core\User\UserInterface to be always present.

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.

Loading history...
84
85
        $handler = new DeleteUserHandler($repository);
86
        $handler->handle($command);
87
88
        $output->writeln($username . ' has been deleted');
89
    }
90
91
    /**
92
     * @throws NoRepositoriesRegisteredException
93
     *
94
     * @return array
95
     */
96 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...
97
    {
98
        if (count($this->userRepositoryCollection->all()) === 0) {
99
            throw new NoRepositoriesRegisteredException('No user repositories registered');
100
        }
101
102
        return array_map(
103
            function (UserRepository $repository) {
104
                return $repository->getSupportedClass();
105
            },
106
            $this->userRepositoryCollection->all()
107
        );
108
    }
109
110
    /**
111
     * Get the repository for the user's Class.
112
     *
113
     * @param $userClass
114
     *
115
     * @return UserRepository
116
     */
117
    private function getRepository($userClass)
118
    {
119
        return $this->userRepositoryCollection->findRepositoryByClassName($userClass);
120
    }
121
}
122