Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

AssignRoleCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 42 7
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Console\User;
6
7
use App\Modules\User\User;
8
use InvalidArgumentException;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\ConfirmationQuestion;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
use Yiisoft\Rbac\ItemsStorageInterface;
16
use Yiisoft\Rbac\Manager;
17
use Yiisoft\Rbac\Role;
18
use Yiisoft\Yii\Console\ExitCode;
19
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
20
21
final class AssignRoleCommand extends Command
22
{
23
    protected static $defaultName = 'user/assignRole';
24
25
    public function __construct(
26
        private CycleDependencyProxy $promise,
27
        private Manager $manager,
28
        private ItemsStorageInterface $itemsStorage
29
    ) {
30
        parent::__construct();
31
    }
32
33
    public function configure(): void
34
    {
35
        $this
36
            ->setDescription('Assign RBAC role to given user')
37
            ->setHelp('This command allows you to assign RBAC role to user')
38
            ->addArgument('role', InputArgument::REQUIRED, 'RBAC role')
39
            ->addArgument('userId', InputArgument::REQUIRED, 'User id');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output): int
43
    {
44
        $io = new SymfonyStyle($input, $output);
45
46
        $roleName = $input->getArgument('role');
47
        $userId = $input->getArgument('userId');
48
49
        try {
50
            $orm = $this->promise->getORM();
51
            $userRepo = $orm->getRepository(User::class);
52
            /** @var User|null $user */
53
            $user = $userRepo->findByPK($userId);
54
            if (null === $user) {
55
                throw new InvalidArgumentException('Can\'t find user');
56
            }
57
            if (null === $user->getId()) {
58
                throw new InvalidArgumentException('User Id is NULL');
59
            }
60
61
            $role = $this->itemsStorage->getRole($roleName);
62
63
            if (null === $role) {
64
                $helper = $this->getHelper('question');
65
                $question = new ConfirmationQuestion('Role doesn\'t exist. Create new one? ', false);
66
67
                if (!$helper->ask($input, $output, $question)) {
68
                    return ExitCode::OK;
69
                }
70
71
                $role = new Role($roleName);
72
                $this->manager->addRole($role);
73
            }
74
75
            $this->manager->assign($roleName, $userId);
76
77
            $io->success('Role was assigned to given user');
78
        } catch (\Throwable $t) {
79
            $io->error($t->getMessage());
80
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
81
        }
82
83
        return ExitCode::OK;
84
    }
85
}
86