Passed
Push — fix_coverage_in_scrutinizer ( cd0379...a04ba4 )
by Herberto
13:22
created

DeleteUserCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Command;
13
14
use App\Entity\User;
15
use App\Repository\UserRepository;
16
use App\Utils\Validator;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Exception\RuntimeException;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Style\SymfonyStyle;
24
25
/**
26
 * A console command that deletes users from the database.
27
 *
28
 * To use this command, open a terminal window, enter into your project
29
 * directory and execute the following:
30
 *
31
 *     $ php bin/console app:delete-user
32
 *
33
 * Check out the code of the src/App/Command/AddUserCommand.php file for
34
 * the full explanation about Symfony commands.
35
 *
36
 * See https://symfony.com/doc/current/cookbook/console/console_command.html
37
 * For more advanced uses, commands can be defined as services too. See
38
 * https://symfony.com/doc/current/console/commands_as_services.html
39
 *
40
 * @author Oleg Voronkovich <[email protected]>
41
 */
42
class DeleteUserCommand extends Command
43
{
44
    protected static $defaultName = 'app:delete-user';
45
46
    /** @var SymfonyStyle */
47
    private $io;
48
    private $entityManager;
49
    private $validator;
50
    private $users;
51
52 View Code Duplication
    public function __construct(EntityManagerInterface $em, Validator $validator, UserRepository $users)
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...
53
    {
54
        parent::__construct();
55
56
        $this->entityManager = $em;
57
        $this->validator = $validator;
58
        $this->users = $users;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function configure()
65
    {
66
        $this
67
            ->setDescription('Deletes users from the database')
68
            ->addArgument('username', InputArgument::REQUIRED, 'The username of an existing user')
69
            ->setHelp(<<<'HELP'
70
The <info>%command.name%</info> command deletes users from the database:
71
72
  <info>php %command.full_name%</info> <comment>username</comment>
73
74
If you omit the argument, the command will ask you to
75
provide the missing value:
76
77
  <info>php %command.full_name%</info>
78
HELP
79
            );
80
    }
81
82
    protected function initialize(InputInterface $input, OutputInterface $output)
83
    {
84
        // SymfonyStyle is an optional feature that Symfony provides so you can
85
        // apply a consistent look to the commands of your application.
86
        // See https://symfony.com/doc/current/console/style.html
87
        $this->io = new SymfonyStyle($input, $output);
88
    }
89
90
    protected function interact(InputInterface $input, OutputInterface $output)
91
    {
92
        if (null !== $input->getArgument('username')) {
93
            return;
94
        }
95
96
        $this->io->title('Delete User Command Interactive Wizard');
97
        $this->io->text([
98
            'If you prefer to not use this interactive wizard, provide the',
99
            'arguments required by this command as follows:',
100
            '',
101
            ' $ php bin/console app:delete-user username',
102
            '',
103
            'Now we\'ll ask you for the value of all the missing command arguments.',
104
            '',
105
        ]);
106
107
        $username = $this->io->ask('Username', null, [$this->validator, 'validateUsername']);
108
        $input->setArgument('username', $username);
109
    }
110
111
    protected function execute(InputInterface $input, OutputInterface $output)
112
    {
113
        $username = $this->validator->validateUsername($input->getArgument('username'));
114
115
        /** @var User $user */
116
        $user = $this->users->findOneByUsername($username);
0 ignored issues
show
Documentation Bug introduced by
The method findOneByUsername does not exist on object<App\Repository\UserRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117
118
        if (null === $user) {
119
            throw new RuntimeException(sprintf('User with username "%s" not found.', $username));
120
        }
121
122
        // After an entity has been removed its in-memory state is the same
123
        // as before the removal, except for generated identifiers.
124
        // See http://docs.doctrine-project.org/en/latest/reference/working-with-objects.html#removing-entities
125
        $userId = $user->getId();
126
127
        $this->entityManager->remove($user);
128
        $this->entityManager->flush();
129
130
        $this->io->success(sprintf('User "%s" (ID: %d, email: %s) was successfully deleted.', $user->getUsername(), $userId, $user->getEmail()));
131
    }
132
}
133