Completed
Pull Request — master (#177)
by
unknown
04:33
created

DeleteUserCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 60
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
C execute() 0 38 8
1
<?php
2
3
namespace N98\Magento\Command\Admin\User;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
10
/**
11
 * Class DeleteUserCommand
12
 */
13
class DeleteUserCommand extends AbstractAdminUserCommand
14
{
15
    /**
16
     * Configure
17
     */
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('admin:user:delete')
22
            ->addArgument('id', InputArgument::OPTIONAL, 'Username or Email')
23
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force')
24
            ->setDescription('Delete the account of a adminhtml user.')
25
        ;
26
    }
27
28
    /**
29
     * @param InputInterface $input
30
     * @param OutputInterface $output
31
     * @throws \Exception
32
     * @return int|void
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $this->detectMagento($output);
37
        if ($this->initMagento()) {
38
39
            $dialog = $this->getHelperSet()->get('dialog');
40
            // Username
41
            if (($id = $input->getArgument('id')) == null) {
42
                $id = $dialog->ask($output, '<question>Username or Email:</question>');
43
            }
44
45
            $user = $this->userModel->loadByUsername($id);
46
            if (!$user->getId()) {
47
                $user = $this->userModel->load($id, 'email');
48
            }
49
50
            if (!$user->getId()) {
51
                $output->writeln('<error>User was not found</error>');
52
                return;
53
            }
54
55
            $shouldRemove = $input->getOption('force');
56
            if (!$shouldRemove) {
57
                $shouldRemove = $dialog->askConfirmation($output, '<question>Are you sure?</question> <comment>[n]</comment>: ', false);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
58
            }
59
60
            if ($shouldRemove) {
61
                try {
62
                    $user->delete();
63
                    $output->writeln('<info>User was successfully deleted</info>');
64
                } catch (\Exception $e) {
65
                    $output->writeln('<error>' . $e->getMessage() . '</error>');
66
                }
67
            } else {
68
                $output->writeln('<error>Aborting delete</error>');
69
            }
70
        }
71
    }
72
}