CreateCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 35
rs 8.8977
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User\Console;
6
7
use App\Auth\Form\SignupForm;
8
use LogicException;
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\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...
14
use Throwable;
15
use Yiisoft\FormModel\FormHydrator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\FormModel\FormHydrator 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...
16
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...
17
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...
18
19
final class CreateCommand extends Command
20
{
21
    protected static $defaultName = 'user/create';
22
23
    public function __construct(
24
        private readonly SignupForm $signupForm,
25
        private readonly Manager $manager,
26
        private readonly FormHydrator $formHydrator
27
    ) {
28
        parent::__construct();
29
    }
30
31
    protected function configure(): void
32
    {
33
        $this
34
            ->setDescription('Creates a user')
35
            ->setHelp('This command allows you to create a user')
36
            ->addArgument('login', InputArgument::REQUIRED, 'Login')
37
            ->addArgument('password', InputArgument::REQUIRED, 'Password')
38
            ->addArgument('isAdmin', InputArgument::OPTIONAL, 'Create user as admin');
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43
        $io = new SymfonyStyle($input, $output);
44
45
        $login = (string) $input->getArgument('login');
46
        $password = (string) $input->getArgument('password');
47
        $isAdmin = (bool) $input->getArgument('isAdmin');
48
        try {
49
            $this->formHydrator->populate(model: $this->signupForm, data: [
50
                'login' => $login,
51
                'password' => $password,
52
                'passwordVerify' => $password,
53
            ], scope: '');
54
            $user = $this->signupForm->signup();
55
        } catch (Throwable $t) {
56
            $io->error($t->getMessage() . ' ' . $t->getFile() . ' ' . $t->getLine());
57
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
58
        }
59
60
        if ($user === false) {
0 ignored issues
show
introduced by
The condition $user === false is always false.
Loading history...
61
            $errors = $this->signupForm->getValidationResult()?->getErrorMessagesIndexedByProperty();
62
            array_walk($errors, fn($error, $attribute) => $io->error("$attribute: " . implode("\n", (array) $error)));
63
            return ExitCode::DATAERR;
64
        }
65
66
        if ($isAdmin) {
67
            $userId = $user->getId();
68
            if ($userId === null) {
69
                throw new LogicException('User Id is NULL');
70
            }
71
            $this->manager->assign('admin', $userId);
72
        }
73
        $io->success('User created');
74
75
        return ExitCode::OK;
76
    }
77
}
78