Passed
Push — master ( 55960a...0fd984 )
by Matt
05:05 queued 53s
created

AdminCreateAdminCommand::execute()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 25
c 2
b 0
f 0
nc 8
nop 2
dl 0
loc 33
rs 9.52
1
<?php
2
3
namespace App\Command;
4
5
use App\Entity\User;
6
use App\Repository\UserRepository;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\Question;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
17
18
class AdminCreateAdminCommand extends Command
19
{
20
    protected static $defaultName = 'admin:create-admin';
21
    protected static $defaultDescription = 'Create your admin user.';
22
23
24
    /** @var UserRepository */
25
    private $userRepository;
26
27
    /** @var UserPasswordHasherInterface */
28
    private $passwordHasher;
29
30
    /** @var EntityManagerInterface */
31
    private $entityManager;
32
33
    public function __construct(
34
        UserRepository $userRepository,
35
        UserPasswordHasherInterface $passwordHasher,
36
        EntityManagerInterface $entityManager
37
    ) {
38
        $this->userRepository = $userRepository;
39
        $this->passwordHasher = $passwordHasher;
40
        $this->entityManager = $entityManager;
41
        parent::__construct();
42
    }
43
44
    protected function configure(): void
45
    {
46
        $this->addArgument('username', InputArgument::REQUIRED, 'Username');
47
        //     ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
48
        // ;
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output): int
52
    {
53
        $io = new SymfonyStyle($input, $output);
54
        $username = $input->getArgument('username');
55
56
        $user = $this->userRepository->createQueryBuilder('u')
57
            ->setMaxResults(1)
58
            ->getQuery()
59
            ->getOneOrNullResult();
60
61
        if ($user !== null) {
62
            $io->warning("A valid user ({$user->getUsername()}) already exists.");
63
            return Command::FAILURE;
64
        }
65
        $question = new Question("Please enter a password for new user '{$username}': ", 'AcmeDemoBundle');
66
        $question->setHidden(true);
67
        $helper = $this->getHelper('question');
68
        $password = $helper->ask($input, $output, $question);
69
70
        try {
71
            $user = new User();
72
            $user->setUsername($username);
73
            $user->setPassword($this->passwordHasher->hashPassword($user, $password));
74
            $user->setRoles(['ROLE_ADMIN']);
75
            $this->entityManager->persist($user);
76
            $this->entityManager->flush();
77
        } catch (\Exception $e) {
78
            $io->error("Error creating user: {$e->getMessage()}");
79
            return Command::FAILURE;
80
        }
81
        $io->success('Successfully created new user.');
82
83
        return Command::SUCCESS;
84
    }
85
}
86