Completed
Branch v1.x-dev (6fcd50)
by Benjamin
04:47
created

CreateAdminCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
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 34
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
27
            $this->io->section('User informations');
28
            $username = $this->askUsername();
29
            $email = $this->askEmail();
30
            $password = $this->askPassword();
31
            $active = $this->io->confirm('Activate the user (or send him an activation email)', false);
32
33
            $coach = (new Coach())
34
                ->setUsername($username)
35
                ->setEmail($email)
36
                ->setPassword($password)
37
                ->setActive($active)
38
                ->setLocale('en')
39
                ->setRoles([Roles::ADMIN])
40
                ;
41
            if(!$active) {
42
                $coach
43
                    ->setHash(hash('sha256', $coach->getEmail()));
44
                $registration = new RegisterCoachEvent($coach);
45
                $this->dispatch($registration, RegisterCoachEvent::NAME);
46
            }
47
            $this->saveCoach($coach);
48
            $this->io->success("The coach $username has been created !");
49
            return 1;
50
        }
51
        $this->io->text('Aborted.');
52
        return 0;
53
    }
54
}
55