UserNewCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 35
c 1
b 0
f 0
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 39 3
A configure() 0 6 1
A __construct() 0 5 1
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Command;
20
21
use App\Entity\User;
22
use Doctrine\ORM\EntityManagerInterface;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
30
31
class UserNewCommand extends Command
32
{
33
    protected static $defaultName = 'app:user-new';
34
35
    protected $entityManager;
36
    protected $passwordEncoder;
37
38
    public function __construct(EntityManagerInterface $entityManager, UserPasswordHasherInterface $passwordEncoder)
39
    {
40
        parent::__construct(static::$defaultName);
41
        $this->entityManager = $entityManager;
42
        $this->passwordEncoder = $passwordEncoder;
43
    }
44
45
    protected function configure()
46
    {
47
        $this
48
            ->setDescription('Create a new user. Useful if no user is existing yet...')
49
            ->addArgument('username', InputArgument::REQUIRED, 'The username of the new user.')
50
            ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'The password of the new user.')
51
        ;
52
    }
53
54
    protected function execute(InputInterface $input, OutputInterface $output): int
55
    {
56
        $io = new SymfonyStyle($input, $output);
57
        $username = $input->getArgument('username');
58
59
        $io->confirm('You are about to create a new user with username: '.$username.' Continue?');
60
61
        $user = new User();
62
        $user->setUsername($username);
63
64
        $password = $input->getOption('password');
65
66
        while (empty($password)) {
67
            $password = $io->askHidden('Please enter a new password for the user! (Input is not shown)');
68
            if (empty($password)) {
69
                $io->warning('Password must not be empty!');
70
            }
71
        }
72
73
        $encoded = $this->passwordEncoder->hashPassword($user, $password);
74
        $user->setPassword($encoded);
75
76
        //Give user all roles
77
        $user->setRoles([
78
                            'ROLE_ADMIN',
79
                            'ROLE_EDIT_USER',
80
                            'ROLE_EDIT_ORGANISATIONS',
81
                            'ROLE_SHOW_PAYMENT_ORDERS',
82
                            'ROLE_EDIT_PAYMENT_ORDERS',
83
                            'ROLE_PO_FACTUALLY',
84
                            'ROLE_PO_MATHEMATICALLY',
85
                        ]);
86
87
        $this->entityManager->persist($user);
88
        $this->entityManager->flush();
89
90
        $io->success('User was created successfully.');
91
92
        return 0;
93
    }
94
}
95