Completed
Push — master ( ad0611...abd676 )
by Tom
09:08 queued 04:30
created

DeleteUserCommand::execute()   C

Complexity

Conditions 8
Paths 37

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 41
rs 5.3846
cc 8
eloc 26
nc 37
nop 2
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
            $dialog = $this->getHelperSet()->get('dialog');
39
            // Username
40
            if (($id = $input->getArgument('id')) == null) {
41
                $id = $dialog->ask($output, '<question>Username or Email:</question>');
42
            }
43
44
            $user = $this->userModel->loadByUsername($id);
45
            if (!$user->getId()) {
46
                $user = $this->userModel->load($id, 'email');
47
            }
48
49
            if (!$user->getId()) {
50
                $output->writeln('<error>User was not found</error>');
51
                return;
52
            }
53
54
            $shouldRemove = $input->getOption('force');
55
            if (!$shouldRemove) {
56
                $shouldRemove = $dialog->askConfirmation(
57
                    $output,
58
                    '<question>Are you sure?</question> <comment>[n]</comment>: ',
59
                    false
60
                );
61
            }
62
63
            if ($shouldRemove) {
64
                try {
65
                    $user->delete();
66
                    $output->writeln('<info>User was successfully deleted</info>');
67
                } catch (\Exception $e) {
68
                    $output->writeln('<error>' . $e->getMessage() . '</error>');
69
                }
70
            } else {
71
                $output->writeln('<error>Aborting delete</error>');
72
            }
73
        }
74
    }
75
}
76