CreateUserCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\User\Command;
23
24
use Parthenon\Common\LoggerAwareTrait;
25
use Parthenon\User\Creator\UserCreatorInterface;
26
use Parthenon\User\Entity\UserInterface;
27
use Symfony\Component\Console\Attribute\AsCommand;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Input\InputArgument;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Symfony\Component\Console\Question\Question;
33
34
#[AsCommand(name: 'parthenon:user:create-user', description: 'Create user')]
35
final class CreateUserCommand extends Command
36
{
37
    use LoggerAwareTrait;
38
39
    public function __construct(private UserInterface $user, private UserCreatorInterface $userCreator)
40
    {
41
        parent::__construct();
42
    }
43
44
    protected function configure()
45
    {
46
        $this
47
            ->addArgument('email', InputArgument::REQUIRED, 'The email address')
48
            ->addArgument('password', InputArgument::REQUIRED, 'The password');
49
    }
50
51
    protected function interact(InputInterface $input, OutputInterface $output)
52
    {
53
        if (!$input->getArgument('email')) {
54
            $emailQuestion = new Question('Please provide an email:');
55
            $emailQuestion->setValidator(function ($email) {
56
                if (empty($email)) {
57
                    throw new \Exception('Email can not be empty');
58
                }
59
60
                return $email;
61
            });
62
63
            $email = $this->getHelper('question')->ask($input, $output, $emailQuestion);
64
            $input->setArgument('password', $email);
65
        }
66
67
        if (!$input->getArgument('password')) {
68
            $passwordQuestion = new Question('Please provide a password:');
69
            $passwordQuestion->setValidator(function ($password) {
70
                if (empty($password)) {
71
                    throw new \Exception('password can not be empty');
72
                }
73
74
                return $password;
75
            });
76
            $passwordQuestion->setHidden(true);
77
78
            $password = $this->getHelper('question')->ask($input, $output, $passwordQuestion);
79
            $input->setArgument('password', $password);
80
        }
81
    }
82
83
    protected function execute(InputInterface $input, OutputInterface $output): int
84
    {
85
        $output->writeln('Create user command');
86
        $this->getLogger()->info('Create user command');
87
88
        $email = $input->getArgument('email');
89
        $password = $input->getArgument('password');
90
91
        $userClass = get_class($this->user);
92
        $user = new $userClass();
93
        $user->setEmail($email);
94
        $user->setPassword($password);
95
96
        $this->userCreator->create($user);
97
98
        return 0;
99
    }
100
}
101