AssignRoleCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 19

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 19
nop 2
dl 0
loc 43
rs 8.5866
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User\Console;
6
7
use App\User\User;
8
use InvalidArgumentException;
9
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\Console\Input\InputArgument;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputArgument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\InputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\OutputInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Console\Question\ConfirmationQuestion;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Consol...on\ConfirmationQuestion was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Symfony\Component\Console\Style\SymfonyStyle;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Style\SymfonyStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Throwable;
16
use Yiisoft\Rbac\ItemsStorageInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Rbac\ItemsStorageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Yiisoft\Rbac\Manager;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Rbac\Manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Yiisoft\Rbac\Role;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Rbac\Role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Yiisoft\Yii\Console\ExitCode;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Console\ExitCode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Cycle\Command\CycleDependencyProxy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
final class AssignRoleCommand extends Command
23
{
24
    protected static $defaultName = 'user/assignRole';
25
26
    public function __construct(
27
        private CycleDependencyProxy $promise,
28
        private Manager $manager,
29
        private ItemsStorageInterface $itemsStorage
30
    ) {
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
            /** @var User|null $user */
54
            $user = $userRepo->findByPK($userId);
55
            if (null === $user) {
56
                throw new InvalidArgumentException('Can\'t find user');
57
            }
58
            if (null === $user->getId()) {
59
                throw new InvalidArgumentException('User Id is NULL');
60
            }
61
62
            $role = $this->itemsStorage->getRole($roleName);
63
64
            if (null === $role) {
65
                $helper = $this->getHelper('question');
66
                $question = new ConfirmationQuestion('Role doesn\'t exist. Create new one? ', false);
67
68
                if (!$helper->ask($input, $output, $question)) {
69
                    return ExitCode::OK;
70
                }
71
72
                $role = new Role($roleName);
73
                $this->manager->addRole($role);
74
            }
75
76
            $this->manager->assign($roleName, $userId);
77
78
            $io->success('Role was assigned to given user');
79
        } catch (Throwable $t) {
80
            $io->error($t->getMessage());
81
82
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
83
        }
84
85
        return ExitCode::OK;
86
    }
87
}
88