Passed
Push — master ( 2d441d...8955fe )
by Alexander
27:46 queued 23:59
created

CreateCommand::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 33
ccs 0
cts 20
cp 0
crap 42
rs 8.9777
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\Style\SymfonyStyle;
13
use Yiisoft\Rbac\Manager;
14
use Yiisoft\Rbac\StorageInterface;
15
use Yiisoft\Yii\Console\ExitCode;
16
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
17
use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter;
18
19
class CreateCommand extends Command
20
{
21
    private CycleDependencyProxy $promise;
22
    private Manager $manager;
23
    private StorageInterface $storage;
24
25
    protected static $defaultName = 'user/create';
26
27
    public function __construct(CycleDependencyProxy $promise, Manager $manager, StorageInterface $storage)
28
    {
29
        $this->promise = $promise;
30
        $this->manager = $manager;
31
        $this->storage = $storage;
32
        parent::__construct();
33
    }
34
35
    public function configure(): void
36
    {
37
        $this
38
            ->setDescription('Creates a user')
39
            ->setHelp('This command allows you to create a user')
40
            ->addArgument('login', InputArgument::REQUIRED, 'Login')
41
            ->addArgument('password', InputArgument::REQUIRED, 'Password')
42
            ->addArgument('isAdmin', InputArgument::OPTIONAL, 'Create user as admin');
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $io = new SymfonyStyle($input, $output);
48
49
        $login = $input->getArgument('login');
50
        $password = $input->getArgument('password');
51
        $isAdmin = (bool)$input->getArgument('isAdmin');
52
53
        $user = new User($login, $password);
54
        try {
55
            (new EntityWriter($this->promise->getORM()))->write([$user]);
56
57
            if ($isAdmin) {
58
                $role = $this->storage->getRoleByName('admin');
59
                $userId = $user->getId();
60
61
                if ($role === null){
62
                    throw new \Exception('Role admin is NULL');
63
                }
64
65
                if ($userId === null) {
66
                    throw new \Exception('User Id is NULL');
67
                }
68
69
                $this->manager->assign($role, $userId);
70
            }
71
72
            $io->success('User created');
73
        } catch (\Throwable $t) {
74
            $io->error($t->getMessage());
75
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
76
        }
77
        return ExitCode::OK;
78
    }
79
}
80