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

DeleteUserCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 49.07 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 10
c 7
b 0
f 2
lcom 1
cbo 4
dl 53
loc 108
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 19 1
B execute() 39 39 4
A getAllValidUserClasses() 14 14 3
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\User\UserRepository;
6
use SumoCoders\FrameworkMultiUserBundle\User\UserRepositoryCollection;
7
use Symfony\Component\Console\Command\Command;
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
use Symfony\Component\Console\Question\ChoiceQuestion;
13
14
final class DeleteUserCommand extends Command
15
{
16
    /**
17
     * @var UserRepositoryCollection
18
     */
19
    private $userRepositoryCollection;
20
21
    /**
22
     * @param UserRepositoryCollection $userRepositoryCollection
23
     */
24
    public function __construct(UserRepositoryCollection $userRepositoryCollection)
25
    {
26
        parent::__construct();
27
        $this->userRepositoryCollection = $userRepositoryCollection;
28
    }
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('sumocoders:multiuser:delete')
34
            ->setDescription('Delete a user entity')
35
            ->addArgument(
36
                'username',
37
                InputArgument::REQUIRED,
38
                'The username of the user'
39
            )
40
            ->addOption(
41
                'class',
42
                null,
43
                InputOption::VALUE_OPTIONAL,
44
                'The class off the user',
45
                'SumoCoders\FrameworkMultiUserBundle\User\User'
46
            )
47
        ;
48
    }
49
50 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...
51
    {
52
        $userClass = $input->getOption('class');
53
54
        $availableUserClasses = $this->getAllValidUserClasses();
55
56
        if (count($availableUserClasses) == 1) {
57
            $userClass = $availableUserClasses[0];
58
        }
59
60
        if (!isset($userClass)) {
61
            $helper = $this->getHelper('question');
62
            $question = new ChoiceQuestion(
63
                'Please select the user class',
64
                $availableUserClasses,
65
                0
66
            );
67
68
            $userClass = $helper->ask($input, $output, $question);
69
        }
70
71
        $repository = $this->getRepository($userClass);
72
73
        $username = $input->getArgument('username');
74
        $user = $repository->findByUsername($username);
75
76
        if (!$user) {
77
            $output->writeln('<error>'.$username.' doesn\'t exists');
78
79
            return;
80
        }
81
82
        $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...
83
84
        $handler = new DeleteUserHandler($repository);
85
        $handler->handle($command);
86
87
        $output->writeln($username . ' has been deleted');
88
    }
89
90
    /**
91
     * @throws NoRepositoriesRegisteredException
92
     *
93
     * @return array
94
     */
95 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...
96
    {
97
        if (count($this->userRepositoryCollection->all()) === 0) {
98
            throw new NoRepositoriesRegisteredException('No user repositories registered');
99
        }
100
101
        $validClasses = [];
102
103
        foreach ($this->userRepositoryCollection->all() as $repository) {
104
            $validClasses[] = $repository->getSupportedClass();
105
        }
106
107
        return $validClasses;
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