Passed
Push — master ( def3b9...9746d2 )
by Daniel
06:04
created

UserCreateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Command;
15
16
use Exception;
17
use Silverback\ApiComponentsBundle\Factory\User\UserFactory;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Question\Question;
24
25
/**
26
 * From FOSUserBundle: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Command/CreateUserCommand.php.
27
 */
28
class UserCreateCommand extends Command
29
{
30
    protected static $defaultName = 'silverback:api-components:user:create';
31
    private UserFactory $userFactory;
32
    private array $questions = [];
33
34 5
    public function __construct(UserFactory $userFactory)
35
    {
36 5
        parent::__construct();
37 5
        $this->userFactory = $userFactory;
38 5
    }
39
40 5
    protected function configure(): void
41
    {
42
        $this
43 5
            ->setDescription('Create a user.')
44 5
            ->setDefinition([
45 5
                new InputArgument('username', InputArgument::REQUIRED, 'The username'),
46 5
                new InputArgument('email', InputArgument::REQUIRED, 'The email'),
47 5
                new InputArgument('password', InputArgument::REQUIRED, 'The password'),
48 5
                new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
49 5
                new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
50 5
                new InputOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite the user if they already exist'),
51
            ])
52 5
            ->setHelp(<<<EOT
53 5
                The <info>silverback:api-components:user:create</info> command creates a user:
54
                  <info>php %command.full_name% daniel</info>
55
                This interactive shell will ask you for an email and then a password.
56
                You can alternatively specify the email and password as the second and third arguments:
57
                  <info>php %command.full_name% daniel [email protected] mypassword</info>
58
                You can create a super admin via the super-admin flag:
59
                  <info>php %command.full_name% admin --super-admin</info>
60
                You can create an inactive user (will not be able to log in):
61
                  <info>php %command.full_name% disabled_user --inactive</info>
62
                You can overwrite a user if they already exist:
63
                  <info>php %command.full_name% existing_username --overwrite</info>
64
                EOT);
65 5
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 2
    protected function execute(InputInterface $input, OutputInterface $output): int
71
    {
72 2
        $username = (string) $input->getArgument('username');
73 2
        $email = (string) $input->getArgument('email');
74 2
        $password = (string) $input->getArgument('password');
75 2
        $inactive = $input->getOption('inactive');
76 2
        $superadmin = $input->getOption('super-admin');
77 2
        $overwrite = $input->getOption('overwrite');
78
79 2
        $this->userFactory->create($username, $password, $email, $inactive, $superadmin, $overwrite);
80
81 2
        $output->writeln(sprintf('Created user: <comment>%s</comment>', $username));
82
83 2
        return 0;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 5
    protected function interact(InputInterface $input, OutputInterface $output): void
90
    {
91 5
        $this->checkUsernameQuestion($input);
92 5
        $this->checkEmailQuestion($input);
93 5
        $this->checkPasswordQuestion($input);
94
95 5
        foreach ($this->questions as $name => $question) {
96 4
            $answer = $this->getHelper('question')->ask($input, $output, $question);
97 1
            $input->setArgument($name, $answer);
98
        }
99 2
    }
100
101 5
    private function checkUsernameQuestion(InputInterface $input): void
102
    {
103 5
        if (!$input->getArgument('username')) {
104 2
            $question = new Question('Please choose a username:');
105 2
            $question->setValidator(self::getNotEmptyValidator('Username'));
106 2
            $this->questions['username'] = $question;
107
        }
108 5
    }
109
110 5
    private function checkPasswordQuestion(InputInterface $input): void
111
    {
112 5
        if (!$input->getArgument('password')) {
113 4
            $question = new Question('Please choose a password:');
114 4
            $question->setValidator(self::getNotEmptyValidator('Password'));
115 4
            $this->questions['password'] = $question;
116
        }
117 5
    }
118
119 5
    private function checkEmailQuestion(InputInterface $input): void
120
    {
121 5
        if (!$input->getArgument('email')) {
122 3
            $question = new Question('Please choose an email (leave blank to use same as username):');
123 3
            $this->questions['email'] = $question;
124
        }
125 5
    }
126
127 4
    private static function getNotEmptyValidator(string $label): callable
128
    {
129
        return static function (string $string) use ($label) {
130 1
            if (empty($string)) {
131
                throw new Exception($label . ' can not be empty');
132
            }
133
134 1
            return $string;
135 4
        };
136
    }
137
}
138