Completed
Push — master ( 31741c...acf815 )
by Benjamin
11:01 queued 06:10
created

CreateAdminCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 26
c 1
b 1
f 0
dl 0
loc 33
ccs 0
cts 26
cp 0
rs 9.504
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Obblm\Core\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Obblm\Core\Entity\Coach;
7
use Obblm\Core\Event\RegisterCoachEvent;
8
use Obblm\Core\Security\Roles;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class CreateAdminCommand extends AbstractAdminCommand
13
{
14
    /** @var string */
15
    protected static $defaultName = 'obblm:coach:create-admin';
16
    protected static $description = 'Creates a new OBBLM Administrator.';
17
    protected static $help = 'This command will add a new administrator.';
18
19
    protected function execute(InputInterface $input, OutputInterface $output)
20
    {
21
        parent::execute($input, $output);
22
        $this->io->title('Create a new OBBLM Administrator');
23
        $this->io->caution('Be carefull, this new user will have the highest right on the application');
24
25
        if ($this->confirmContinue()) {
26
            $this->io->section('User informations');
27
            $username = $this->askUsername();
28
            $email = $this->askEmail();
29
            $password = $this->askPassword();
30
            $active = $this->io->confirm('Activate the user (or send him an activation email)', false);
31
32
            $coach = (new Coach())
33
                ->setUsername($username)
34
                ->setEmail($email)
35
                ->setPassword($password)
36
                ->setActive($active)
37
                ->setLocale('en')
38
                ->setRoles([Roles::ADMIN])
39
                ;
40
            if (!$active) {
41
                $coach
42
                    ->setHash(hash('sha256', $coach->getEmail()));
43
                $registration = new RegisterCoachEvent($coach);
44
                $this->dispatch($registration, RegisterCoachEvent::NAME);
45
            }
46
            $this->saveCoach($coach);
47
            $this->io->success("The coach $username has been created !");
48
            return 1;
49
        }
50
        $this->io->text('Aborted.');
51
        return 0;
52
    }
53
}
54