Completed
Push — master ( 70433e...08bb6a )
by Alexander
19s queued 13s
created

CreateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Command\User;
4
5
use App\Entity\User;
6
use Cycle\ORM\ORMInterface;
7
use Cycle\ORM\Transaction;
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\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
class CreateCommand extends Command
16
{
17
    private const EXIT_CODE_FAILED_TO_PERSIST = 1;
18
19
    private $orm;
20
21
    protected static $defaultName = 'user/create';
22
23
    public function __construct(ORMInterface $orm)
24
    {
25
        parent::__construct();
26
        $this->orm = $orm;
27
    }
28
29
    public function configure(): void
30
    {
31
        $this
32
            ->setDescription('Creates a user')
33
            ->setHelp('This command allows you to create a user')
34
            ->addArgument('login', InputArgument::REQUIRED, 'Login')
35
            ->addArgument('password', InputArgument::REQUIRED, 'Password');
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $io = new SymfonyStyle($input, $output);
41
42
        $login = $input->getArgument('login');
43
        $password = $input->getArgument('password');
44
45
        $user = new User();
46
        $user->setLogin($login);
0 ignored issues
show
Bug introduced by
It seems like $login can also be of type null and string[]; however, parameter $login of App\Entity\User::setLogin() 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

46
        $user->setLogin(/** @scrutinizer ignore-type */ $login);
Loading history...
47
        $user->setPassword($password);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type null and string[]; however, parameter $password of App\Entity\User::setPassword() 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

47
        $user->setPassword(/** @scrutinizer ignore-type */ $password);
Loading history...
48
49
        try {
50
            $transaction = new Transaction($this->orm);
51
            $transaction->persist($user);
52
            $transaction->run();
53
            $io->success('User created');
54
        } catch (\Throwable $t) {
55
            $io->error($t->getMessage());
56
            return self::EXIT_CODE_FAILED_TO_PERSIST;
57
        }
58
    }
59
}
60