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

AbstractAdminCommand   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
eloc 59
c 1
b 0
f 0
dl 0
loc 125
ccs 0
cts 65
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A askPassword() 0 10 2
A askUsername() 0 11 3
A getChoice() 0 10 3
A configure() 0 4 1
A dispatch() 0 3 1
A __construct() 0 6 1
A execute() 0 3 1
A askEmail() 0 11 3
A confirmContinue() 0 3 1
A getCoachByLoginOrEmail() 0 26 6
A saveCoach() 0 4 1
1
<?php
2
3
namespace Obblm\Core\Command;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Obblm\Core\Entity\Coach;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
13
14
abstract class AbstractAdminCommand extends Command
15
{
16
    protected static $description = 'Command description.';
17
    protected static $help = 'Command help.';
18
19
    /** @var EntityManagerInterface */
20
    private $em;
21
    /** @var SymfonyStyle */
22
    protected $io;
23
    private $passwordEncoder;
24
    private $dispatcher;
25
26
    public function __construct(EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, EventDispatcherInterface $dispatcher)
27
    {
28
        $this->em = $em;
29
        $this->passwordEncoder = $passwordEncoder;
30
        $this->dispatcher = $dispatcher;
31
        parent::__construct();
32
    }
33
34
    protected function configure():void
35
    {
36
        $this->setDescription($this::$description)
37
            ->setHelp($this::$help);
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $this->io = new SymfonyStyle($input, $output);
43
    }
44
45
    protected function dispatch(object $event, string $eventName):object
46
    {
47
        return $this->dispatcher->dispatch($event, $eventName);
48
    }
49
50
    protected function confirmContinue():bool
51
    {
52
        return $this->io->confirm('Are you sure you want to continue ?', true);
53
    }
54
55
    protected function askUsername():string
56
    {
57
        $coachRepository = $this->em->getRepository(Coach::class);
58
        return $this->io->ask('User login', null, function ($username) use ($coachRepository) {
59
            if (empty($username)) {
60
                throw new \RuntimeException('The login cannot be empty.');
61
            }
62
            if ($coachRepository->findOneByUsername($username)) {
63
                throw new \RuntimeException('This login is allready used.');
64
            }
65
            return (string) $username;
66
        });
67
    }
68
69
    protected function askEmail():string
70
    {
71
        $coachRepository = $this->em->getRepository(Coach::class);
72
        return $this->io->ask('User email', null, function ($email) use ($coachRepository) {
73
            if (empty($email)) {
74
                throw new \RuntimeException('The email cannot be empty.');
75
            }
76
            if ($coachRepository->findOneByEmail($email)) {
77
                throw new \RuntimeException('This email is allready used.');
78
            }
79
            return (string) $email;
80
        });
81
    }
82
83
    protected function askPassword():string
84
    {
85
        $password = $this->io->askHidden('User password', function ($password) {
86
            if (empty($password)) {
87
                throw new \RuntimeException('Password cannot be empty.');
88
            }
89
90
            return $password;
91
        });
92
        return $this->passwordEncoder->encodePassword(new Coach(), $password);
93
    }
94
95
    protected function saveCoach(Coach $coach)
96
    {
97
        $this->em->persist($coach);
98
        $this->em->flush();
99
    }
100
101
    protected function getChoice():string
102
    {
103
        $choice = $this->io->choice('Search him by login or email ', ['login', 'email']);
104
105
        if($choice !== 'login' && $choice !== 'email') {
106
            throw new \RuntimeException("Something went wrong.");
107
            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...
108
        }
109
110
        return $choice;
111
    }
112
113
    protected function getCoachByLoginOrEmail():Coach
114
    {
115
        $choice = $this->getChoice();
116
117
        $coachRepository = $this->em->getRepository(Coach::class);
118
        if($choice === 'login') {
119
            return $this->io->ask('User login', null, function ($username) use ($coachRepository) {
120
                if (empty($username)) {
121
                    throw new \RuntimeException('The login cannot be empty.');
122
                }
123
                $coach = $coachRepository->findOneByUsername($username);
124
                if (!$coach) {
125
                    throw new \RuntimeException("This login doesn't exist.");
126
                }
127
                return $coach;
128
            });
129
        }
130
        return $this->io->ask('User email', null, function ($email) use ($coachRepository) {
131
            if (empty($email)) {
132
                throw new \RuntimeException('The email cannot be empty.');
133
            }
134
            $coach = $coachRepository->findOneByEmail($email);
135
            if (!$coach) {
136
                throw new \RuntimeException("This email doesn't exist.");
137
            }
138
            return $coach;
139
        });
140
    }
141
}
142
143