Completed
Branch v1.x-dev (ef54be)
by Benjamin
03:52
created

PromoteCoachCommand::execute()   B

Complexity

Conditions 11
Paths 8

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 36
c 1
b 1
f 0
dl 0
loc 53
ccs 0
cts 35
cp 0
rs 7.3166
cc 11
nc 8
nop 2
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class PromoteCoachCommand extends Command
15
{
16
    /** @var string */
17
    protected static $defaultName = 'obblm:coach:promote';
18
    /** @var EntityManagerInterface */
19
    private $em;
20
    /** @var SymfonyStyle */
21
    private $io;
22
    private $passwordEncoder;
0 ignored issues
show
introduced by
The private property $passwordEncoder is not used, and could be removed.
Loading history...
23
    private $dispatcher;
0 ignored issues
show
introduced by
The private property $dispatcher is not used, and could be removed.
Loading history...
24
25
    public function __construct(EntityManagerInterface $em)
26
    {
27
        $this->em = $em;
28
        parent::__construct();
29
    }
30
31
    protected function configure():void
32
    {
33
        $this->setDescription('Promotes an existing user to OBBLM Administrator.')
34
            ->setHelp('This command will promote an existing user to the administrator role.');
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->io = new SymfonyStyle($input, $output);
40
41
        $this->io->title('Promote a new OBBLM Administrator');
42
        $this->io->caution('Be carefull, this new user will have the highest right on the application');
43
        $continue = $this->io->confirm('Are you sure you want to continue ?', true);
44
        if($continue) {
45
            $coachRepository = $this->em->getRepository(Coach::class);
46
47
            $choice = $this->io->choice('Search him by login or email ', ['login', 'email']);
48
49
            if($choice !== 'login' && $choice !== 'email') {
50
                throw new \RuntimeException("Something went wrong.");
51
                return 0;
0 ignored issues
show
Unused Code introduced by
return 0 is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
52
            }
53
            elseif($choice === 'login') {
54
                $coach = $this->io->ask('User login', null, function ($username) use ($coachRepository) {
55
                    if (empty($username)) {
56
                        throw new \RuntimeException('The login cannot be empty.');
57
                    }
58
                    $coach = $coachRepository->findOneByUsername($username);
59
                    if (!$coach) {
60
                        throw new \RuntimeException("This login doesn't exist.");
61
                    }
62
                    return $coach;
63
                });
64
            }
65
            elseif ($choice === 'email') {
66
                $coach = $this->io->ask('User email', null, function ($email) use ($coachRepository) {
67
                    if (empty($email)) {
68
                        throw new \RuntimeException('The email cannot be empty.');
69
                    }
70
                    $coach = $coachRepository->findOneByEmail($email);
71
                    if (!$coach) {
72
                        throw new \RuntimeException("This email doesn't exist.");
73
                    }
74
                    return $coach;
75
                });
76
            }
77
            /** @var Coach $coach */
78
            $continue = $this->io->confirm("Are you sure you want to promote {$coach->getUsername()} ?", true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $coach does not seem to be defined for all execution paths leading up to this point.
Loading history...
79
            if($continue) {
80
                $coach
81
                    ->setRoles([Roles::ADMIN]);
82
                $this->em->persist($coach);
83
                $this->em->flush();
84
                $this->io->success("The coach {$coach->getUsername()} has been promoted !");
85
                return 1;
86
            }
87
        }
88
        $this->io->text('Aborted.');
89
        return 0;
90
    }
91
}
92