Passed
Push — master ( 8e610a...5c833b )
by Alexander
13:18
created

AssignRoleCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace App\Command\User;
4
5
use App\Entity\User;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
use Yiisoft\Rbac\Manager;
13
use Yiisoft\Rbac\Role;
14
use Yiisoft\Rbac\StorageInterface;
15
use Yiisoft\Yii\Console\ExitCode;
16
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
17
18
class AssignRoleCommand extends Command
19
{
20
    private CycleDependencyProxy $promise;
21
    private Manager $manager;
22
    private StorageInterface $storage;
23
24
    protected static $defaultName = 'user/assignRole';
25
26
    public function __construct(CycleDependencyProxy $promise, Manager $manager, StorageInterface $storage)
27
    {
28
        $this->promise = $promise;
29
        $this->manager = $manager;
30
        $this->storage = $storage;
31
        parent::__construct();
32
    }
33
34
    public function configure(): void
35
    {
36
        $this
37
            ->setDescription('Assign RBAC role to given user')
38
            ->setHelp('This command allows you to assign RBAC role to user')
39
            ->addArgument('role', InputArgument::REQUIRED, 'RBAC role')
40
            ->addArgument('userId', InputArgument::REQUIRED, 'User id');
41
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output): int
44
    {
45
        $io = new SymfonyStyle($input, $output);
46
47
        $roleName = $input->getArgument('role');
48
        $userId = $input->getArgument('userId');
49
50
        try {
51
            $orm = $this->promise->getORM();
52
            $userRepo = $orm->getRepository(User::class);
53
            $user = $userRepo->findByPK($userId);
54
            if (is_null($user)) {
55
                throw new \Exception('Can\'t find user');
56
            }
57
58
            $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

58
            $role = $this->storage->getRoleByName(/** @scrutinizer ignore-type */ $roleName);
Loading history...
59
60
            if (is_null($role)) {
61
                $helper = $this->getHelper('question');
62
                $question = new ConfirmationQuestion('Role doesn\'t exist. Create new one? ', false);
63
64
                if (!$helper->ask($input, $output, $question)) {
65
                    return ExitCode::OK;
66
                }
67
68
                $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

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