Completed
Push — develop ( a21284...ba4fc7 )
by Christian
02:54
created

ChangeStatusCommand::getUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace N98\Magento\Command\Admin\User;
6
7
use Magento\User\Model\User;
8
use Magento\User\Model\ResourceModel\User as UserResourceModel;
9
use Magento\User\Model\ResourceModel\User\CollectionFactory;
10
use N98\Magento\Command\AbstractMagentoCommand;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
use function is_string;
17
use function sprintf;
18
19
/**
20
 * Class ToggleBlockCommand
21
 * @package N98\Magento\Command\Admin\User
22
 */
23
class ChangeStatusCommand extends AbstractMagentoCommand
24
{
25
    protected const USER_ARGUMENT = 'user';
26
    protected const ACTIVATE_OPTION = 'activate';
27
    protected const DEACTIVATE_OPTION = 'deactivate';
28
29
    protected $userResourceModel;
30
    protected $userCollectionFactory;
31
    
32
    public function inject(UserResourceModel $userResourceModel, CollectionFactory $userCollectionFactory): void
33
    {
34
        $this->userResourceModel = $userResourceModel;
35
        $this->userCollectionFactory = $userCollectionFactory;
36
    }
37
    
38
    protected function configure(): void
39
    {
40
        $this
41
            ->setName('admin:user:change-status')
42
            ->addArgument(self::USER_ARGUMENT, InputArgument::REQUIRED, 'Username or email for the admin user')
43
            ->addOption(self::ACTIVATE_OPTION, null, InputOption::VALUE_NONE, 'Activate the user')
44
            ->addOption(self::DEACTIVATE_OPTION, null, InputOption::VALUE_NONE, 'Deactivate the user')
45
            ->setDescription(
46
                'Set the status of an admin user, if no status is given the status will be toggled. 
47
                Note: the first found user is edited (since it is possible to use someone else\'s email as your 
48
                username, although you should try to avoid this scenario).'
49
            );
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output): void
53
    {
54
        $this->detectMagento($output, true);
55
        if (!$this->initMagento()) {
56
            return;
57
        }
58
59
        $username = $input->getArgument(self::USER_ARGUMENT);
60
        if (!is_string($username)) {
61
            $output->writeln('Please provide an username or email for the admin user. Use --help for more information.');
62
            return;
63
        }
64
65
        $user = $this->getUser($username);
66
        if ($user === null) {
67
            $output->writeln(sprintf('Could not find a user associated to <info>%s</info>.', $username));
68
            return;
69
        }
70
71
        $newStatus = $this->getNewStatusForUser($user, $input);
72
        $user->setIsActive($newStatus);
73
        $this->userResourceModel->save($user);
74
        $output->writeln(sprintf('User has been <info>%s</info>.', $newStatus ? 'activated' : 'deactivated'));
75
    }
76
77
    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...
78
    {
79
        $collection = $this->userCollectionFactory->create();
80
        // Get the user where either the username or the email matches.
81
        $collection->addFieldToFilter(
82
            [
83
                'username',
84
                'email',
85
            ],
86
            [
87
                $username,
88
                $username,
89
            ]
90
        );
91
        $collection->getItems();
92
        $user = $collection->getFirstItem();
93
        return $user->getUserId() !== null ? $user : null;
94
    }
95
96
    protected function getNewStatusForUser(User $user, InputInterface $input): bool
97
    {
98
        if ($input->getOption(self::ACTIVATE_OPTION) === true) {
99
            return true;
100
        }
101
102
        if($input->getOption(self::DEACTIVATE_OPTION) === true) {
103
            return false;
104
        }
105
106
        // If no option is supplied, toggle the status.
107
        return !$user->getIsActive();
108
    }
109
}
110