Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

ChangeStatusCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A inject() 0 5 1
A configure() 0 13 1
A execute() 0 24 5
A getUser() 0 18 2
A getNewStatusForUser() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace N98\Magento\Command\Admin\User;
6
7
use function is_string;
8
use Magento\User\Model\ResourceModel\User as UserResourceModel;
9
use Magento\User\Model\ResourceModel\User\CollectionFactory;
10
use Magento\User\Model\User;
11
use N98\Magento\Command\AbstractMagentoCommand;
12
use function sprintf;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class ToggleBlockCommand
20
 * @package N98\Magento\Command\Admin\User
21
 */
22
class ChangeStatusCommand extends AbstractMagentoCommand
23
{
24
    protected const USER_ARGUMENT = 'user';
25
    protected const ACTIVATE_OPTION = 'activate';
26
    protected const DEACTIVATE_OPTION = 'deactivate';
27
28
    protected $userResourceModel;
29
    protected $userCollectionFactory;
30
31
    public function inject(UserResourceModel $userResourceModel, CollectionFactory $userCollectionFactory): void
32
    {
33
        $this->userResourceModel = $userResourceModel;
34
        $this->userCollectionFactory = $userCollectionFactory;
35
    }
36
37
    protected function configure(): void
38
    {
39
        $this
40
            ->setName('admin:user:change-status')
41
            ->addArgument(self::USER_ARGUMENT, InputArgument::REQUIRED, 'Username or email for the admin user')
42
            ->addOption(self::ACTIVATE_OPTION, null, InputOption::VALUE_NONE, 'Activate the user')
43
            ->addOption(self::DEACTIVATE_OPTION, null, InputOption::VALUE_NONE, 'Deactivate the user')
44
            ->setDescription(
45
                'Set the status of an admin user, if no status is given the status will be toggled. 
46
                Note: the first found user is edited (since it is possible to use someone else\'s email as your 
47
                username, although you should try to avoid this scenario).'
48
            );
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output): void
52
    {
53
        $this->detectMagento($output, true);
54
        if (!$this->initMagento()) {
55
            return;
56
        }
57
58
        $username = $input->getArgument(self::USER_ARGUMENT);
59
        if (!is_string($username)) {
60
            $output->writeln('Please provide an username or email for the admin user. Use --help for more information.');
61
            return;
62
        }
63
64
        $user = $this->getUser($username);
65
        if ($user === null) {
66
            $output->writeln(sprintf('Could not find a user associated to <info>%s</info>.', $username));
67
            return;
68
        }
69
70
        $newStatus = $this->getNewStatusForUser($user, $input);
71
        $user->setIsActive($newStatus);
72
        $this->userResourceModel->save($user);
73
        $output->writeln(sprintf('User has been <info>%s</info>.', $newStatus ? 'activated' : 'deactivated'));
74
    }
75
76
    protected function getUser(string $username): ?User
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
77
    {
78
        $collection = $this->userCollectionFactory->create();
79
        // Get the user where either the username or the email matches.
80
        $collection->addFieldToFilter(
81
            [
82
                'username',
83
                'email',
84
            ],
85
            [
86
                $username,
87
                $username,
88
            ]
89
        );
90
        $collection->getItems();
91
        $user = $collection->getFirstItem();
92
        return $user->getUserId() !== null ? $user : null;
93
    }
94
95
    protected function getNewStatusForUser(User $user, InputInterface $input): bool
96
    {
97
        if ($input->getOption(self::ACTIVATE_OPTION) === true) {
98
            return true;
99
        }
100
101
        if ($input->getOption(self::DEACTIVATE_OPTION) === true) {
102
            return false;
103
        }
104
105
        // If no option is supplied, toggle the status.
106
        return !$user->getIsActive();
107
    }
108
}
109