AssignRoleCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 63
ccs 0
cts 35
cp 0
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 38 6
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User\Console;
6
7
use App\User\User;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\ConfirmationQuestion;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Yiisoft\Rbac\Manager;
15
use Yiisoft\Rbac\Role;
16
use Yiisoft\Rbac\StorageInterface;
17
use Yiisoft\Yii\Console\ExitCode;
18
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
19
20
class AssignRoleCommand extends Command
21
{
22
    private CycleDependencyProxy $promise;
23
    private Manager $manager;
24
    private StorageInterface $storage;
25
26
    protected static $defaultName = 'user/assignRole';
27
28
    public function __construct(CycleDependencyProxy $promise, Manager $manager, StorageInterface $storage)
29
    {
30
        $this->promise = $promise;
31
        $this->manager = $manager;
32
        $this->storage = $storage;
33
        parent::__construct();
34
    }
35
36
    public function configure(): void
37
    {
38
        $this
39
            ->setDescription('Assign RBAC role to given user')
40
            ->setHelp('This command allows you to assign RBAC role to user')
41
            ->addArgument('role', InputArgument::REQUIRED, 'RBAC role')
42
            ->addArgument('userId', InputArgument::REQUIRED, 'User id');
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $io = new SymfonyStyle($input, $output);
48
49
        $roleName = $input->getArgument('role');
50
        $userId = $input->getArgument('userId');
51
52
        try {
53
            $orm = $this->promise->getORM();
54
            $userRepo = $orm->getRepository(User::class);
55
            $user = $userRepo->findByPK($userId);
56
            if (null === $user) {
57
                throw new \Exception('Can\'t find user');
58
            }
59
60
            $role = $this->storage->getRoleByName($roleName);
0 ignored issues
show
Bug introduced by
It seems like $roleName can also be of type null and string[]; however, parameter $name of Yiisoft\Rbac\StorageInterface::getRoleByName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            $role = $this->storage->getRoleByName(/** @scrutinizer ignore-type */ $roleName);
Loading history...
61
62
            if (null === $role) {
63
                $helper = $this->getHelper('question');
64
                $question = new ConfirmationQuestion('Role doesn\'t exist. Create new one? ', false);
65
66
                if (!$helper->ask($input, $output, $question)) {
67
                    return ExitCode::OK;
68
                }
69
70
                $role = new Role($roleName);
0 ignored issues
show
Bug introduced by
It seems like $roleName can also be of type null and string[]; however, parameter $name of Yiisoft\Rbac\Role::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
                $role = new Role(/** @scrutinizer ignore-type */ $roleName);
Loading history...
71
                $this->manager->addRole($role);
72
            }
73
74
            $this->manager->assign($role, $user->getId());
75
76
            $io->success('Role was assigned to given user');
77
        } catch (\Throwable $t) {
78
            $io->error($t->getMessage());
79
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
80
        }
81
82
        return ExitCode::OK;
83
    }
84
}
85