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

RevokeCoachCommand::execute()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 37
c 1
b 1
f 0
dl 0
loc 55
ccs 0
cts 36
cp 0
rs 6.9666
cc 12
nc 11
nop 2
crap 156

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 RevokeCoachCommand extends Command
15
{
16
    /** @var string */
17
    protected static $defaultName = 'obblm:coach:revoke';
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('Remoke the administrator role to an existing user.')
34
            ->setHelp('This command will revoke the administrator role of an existing user.');
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->io = new SymfonyStyle($input, $output);
40
41
        $this->io->title('Revoke an OBBLM Administrator');
42
        $this->io->caution('Be carefull, this new user will not have anymore 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 revoke {$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
                if(in_array(Roles::ADMIN, $coach->getRoles())) {
81
                    $coach
82
                        ->setRoles([Roles::COACH]);
83
                    $this->em->persist($coach);
84
                    $this->em->flush();
85
                }
86
                $this->io->success("The coach {$coach->getUsername()} has been revoked !");
87
                return 1;
88
            }
89
        }
90
        $this->io->text('Aborted.');
91
        return 0;
92
    }
93
}
94