ChangeStatusCommand::execute()   C
last analyzed

Complexity

Conditions 11
Paths 133

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 133
nop 2
dl 0
loc 56
rs 6.5933
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Magento\Command\Admin\User;
4
5
use Exception;
6
use RuntimeException;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ChangeStatusCommand extends AbstractAdminUserCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('admin:user:change-status')
18
            ->addArgument('id', InputArgument::OPTIONAL, 'Username or Email')
19
            ->addOption('activate', null, InputOption::VALUE_NONE, 'Activate user')
20
            ->addOption('deactivate', null, InputOption::VALUE_NONE, 'Deactivate user')
21
            ->setDescription('Set active status of an adminhtml user. If no option is set the status will be toggled.')
22
        ;
23
    }
24
25
    /**
26
     * @param InputInterface  $input
27
     * @param OutputInterface $output
28
     *
29
     * @return int|void
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $this->detectMagento($output);
34
        if ($this->initMagento()) {
35
36
            // Username
37
38
            $id = $this->getOrAskForArgument('id', $input, $output, 'Username or Email');
39
            $user = $this->getUserModel()->loadByUsername($id);
40
            if (!$user->getId()) {
41
                $user = $this->getUserModel()->load($id, 'email');
42
            }
43
44
            if (!$user->getId()) {
45
                $output->writeln('<error>User was not found</error>');
46
                return;
47
            }
48
49
            try {
50
                $result = $user->validate();
51
52
                if (is_array($result)) {
53
                    throw new RuntimeException(implode(PHP_EOL, $result));
54
                }
55
56
                if ($input->getOption('activate')) {
57
                    $user->setIsActive(1);
58
                }
59
60
                if ($input->getOption('deactivate')) {
61
                    $user->setIsActive(0);
62
                }
63
64
                // toggle is_active
65
                if (!$input->getOption('activate') && !$input->getOption('deactivate')) {
66
                    $user->setIsActive(!$user->getIsActive()); // toggle
67
                }
68
69
                $user->save();
70
71
                if ($user->getIsActive() == 1) {
72
                    $output->writeln(
73
                        '<info>User <comment>' . $user->getUsername() . '</comment>' .
74
                        ' is now <comment>active</comment></info>'
75
                    );
76
                } else {
77
                    $output->writeln(
78
                        '<info>User <comment>' . $user->getUsername() . '</comment>' .
79
                        ' is now <comment>inactive</comment></info>'
80
                    );
81
                }
82
            } catch (Exception $e) {
83
                $output->writeln('<error>' . $e->getMessage() . '</error>');
84
            }
85
        }
86
    }
87
}
88