Issues (39)

src/User/Console/CreateCommand.php (3 issues)

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);
0 ignored issues
show
It seems like $password can also be of type null and string[]; however, parameter $password of App\User\User::__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

53
        $user = new User($login, /** @scrutinizer ignore-type */ $password);
Loading history...
It seems like $login can also be of type null and string[]; however, parameter $login of App\User\User::__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

53
        $user = new User(/** @scrutinizer ignore-type */ $login, $password);
Loading history...
54
        try {
55
            (new EntityWriter($this->promise->getORM()))->write([$user]);
56
57
            if ($isAdmin) {
58
                $this->manager->assign($this->storage->getRoleByName('admin'), $user->getId());
0 ignored issues
show
It seems like $this->storage->getRoleByName('admin') can also be of type null; however, parameter $item of Yiisoft\Rbac\Manager::assign() does only seem to accept Yiisoft\Rbac\Item, 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
                $this->manager->assign(/** @scrutinizer ignore-type */ $this->storage->getRoleByName('admin'), $user->getId());
Loading history...
59
            }
60
61
            $io->success('User created');
62
        } catch (\Throwable $t) {
63
            $io->error($t->getMessage());
64
            return $t->getCode() ?: ExitCode::UNSPECIFIED_ERROR;
65
        }
66
        return ExitCode::OK;
67
    }
68
}
69