Passed
Branch main (b6a268)
by Iain
04:04
created

RemoveRoleCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 67
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A interact() 0 30 5
A __construct() 0 3 1
A execute() 0 15 1
A configure() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\User\Command;
16
17
use Parthenon\Common\LoggerAwareTrait;
18
use Parthenon\User\Repository\UserRepositoryInterface;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Question\Question;
24
25
final class RemoveRoleCommand extends Command
26
{
27
    use LoggerAwareTrait;
28
29
    protected static $defaultName = 'parthenon:user:remove-role';
30
31
    public function __construct(private UserRepositoryInterface $userRepository)
32
    {
33
        parent::__construct();
34
    }
35
36
    protected function configure()
37
    {
38
        $this->setName(static::$defaultName)
39
            ->setDescription('Remove role')
40
            ->addArgument('email', InputArgument::REQUIRED, 'The email address')
41
            ->addArgument('role', InputArgument::REQUIRED, 'The role');
42
    }
43
44
    protected function interact(InputInterface $input, OutputInterface $output)
45
    {
46
        if (!$input->getArgument('email')) {
47
            $emailQuestion = new Question('Please provide an email:');
48
            $emailQuestion->setValidator(function ($email) {
49
                if (empty($email)) {
50
                    throw new \Exception('Email can not be empty');
51
                }
52
53
                return $email;
54
            });
55
56
            $email = $this->getHelper('question')->ask($input, $output, $emailQuestion);
57
58
            $input->setArgument('email', $email);
59
        }
60
61
        if (!$input->getArgument('role')) {
62
            $roleQuestion = new Question('Please provide a role:');
63
            $roleQuestion->setValidator(function ($password) {
64
                if (empty($password)) {
65
                    throw new \Exception('role can not be empty');
66
                }
67
68
                return $password;
69
            });
70
            $roleQuestion->setHidden(false);
71
72
            $role = $this->getHelper('question')->ask($input, $output, $roleQuestion);
73
            $input->setArgument('role', $role);
74
        }
75
    }
76
77
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79
        $output->writeln('Remove role command');
80
        $this->getLogger()->info('Remove role  command');
81
82
        $email = $input->getArgument('email');
83
        $role = $input->getArgument('role');
84
85
        $user = $this->userRepository->findByEmail($email);
86
87
        $user->setRoles(array_filter($user->getRoles(), function ($v) use ($role) { return $v !== $role; }));
88
89
        $this->userRepository->save($user);
90
91
        return 0;
92
    }
93
}
94