DeleteUserCommand::execute()   C
last analyzed

Complexity

Conditions 8
Paths 37

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 45
rs 5.3846
cc 8
eloc 27
nc 37
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Admin\User;
4
5
use Symfony\Component\Console\Helper\DialogHelper;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class DeleteUserCommand
13
 */
14
class DeleteUserCommand extends AbstractAdminUserCommand
15
{
16
    /**
17
     * Configure
18
     */
19
    protected function configure()
20
    {
21
        $this
22
            ->setName('admin:user:delete')
23
            ->addArgument('id', InputArgument::OPTIONAL, 'Username or Email')
24
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force')
25
            ->setDescription('Delete the account of a adminhtml user.')
26
        ;
27
    }
28
29
    /**
30
     * @param InputInterface $input
31
     * @param OutputInterface $output
32
     * @throws \Exception
33
     * @return int|void
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $this->detectMagento($output);
38
        if (!$this->initMagento()) {
39
            return;
40
        }
41
42
        /** @var $dialog DialogHelper */
43
        $dialog = $this->getHelper('dialog');
44
45
        // Username
46
        if (($id = $input->getArgument('id')) == null) {
47
            $id = $dialog->ask($output, '<question>Username or Email:</question>');
48
        }
49
50
        $user = $this->userModel->loadByUsername($id);
51
        if (!$user->getId()) {
52
            $user = $this->userModel->load($id, 'email');
53
        }
54
55
        if (!$user->getId()) {
56
            $output->writeln('<error>User was not found</error>');
57
            return;
58
        }
59
60
        $shouldRemove = $input->getOption('force');
61
        if (!$shouldRemove) {
62
            $shouldRemove = $dialog->askConfirmation(
63
                $output,
64
                '<question>Are you sure?</question> <comment>[n]</comment>: ',
65
                false
66
            );
67
        }
68
69
        if ($shouldRemove) {
70
            try {
71
                $user->delete();
72
                $output->writeln('<info>User was successfully deleted</info>');
73
            } catch (\Exception $e) {
74
                $output->writeln('<error>' . $e->getMessage() . '</error>');
75
            }
76
        } else {
77
            $output->writeln('<error>Aborting delete</error>');
78
        }
79
    }
80
}
81