Conditions | 11 |
Paths | 16 |
Total Lines | 74 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
35 | { |
||
36 | $io = new SymfonyStyle($input, $output); |
||
37 | $helper = $this->getHelper('question'); |
||
38 | $email = $input->getArgument('email'); |
||
39 | $isEmailOk = false; |
||
40 | $questionMaxAttempts = self::QUESTION_MAX_ATTEMPTS; |
||
41 | if ($this->isTestEnv()) { |
||
42 | $questionMaxAttempts = 1; |
||
43 | } |
||
44 | |||
45 | while (false === $isEmailOk) { |
||
46 | if (null !== $email) { |
||
47 | $emailConstraint = new Assert\Email(); |
||
48 | $errors = $this->validator->validate($email, $emailConstraint); |
||
49 | if (0 < \count($errors)) { |
||
50 | $io->error($errors[0]->getMessage()); |
||
51 | |||
52 | return 1; |
||
53 | } |
||
54 | if (null !== $this->findUserByEmail($email)) { |
||
|
|||
55 | $io->error(sprintf('The email %s is already registered', $email)); |
||
56 | |||
57 | return 1; |
||
58 | } |
||
59 | $isEmailOk = true; |
||
60 | } else { |
||
61 | $question = new Question('Enter the email address of the new user: '); |
||
62 | $question->setValidator(function ($answer) { |
||
63 | if (null === $answer) { |
||
64 | throw new \Exception('The email address is required to create a new user'); |
||
65 | } |
||
66 | |||
67 | return $answer; |
||
68 | }); |
||
69 | $question->setMaxAttempts($questionMaxAttempts); |
||
70 | $email = $helper->ask($input, $output, $question); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | $question = new Question('Enter a password: '); |
||
75 | $question->setValidator(function ($answer) { |
||
76 | if (null === $answer || '' === trim($answer)) { |
||
77 | throw new \Exception('The password can\'t be empty'); |
||
78 | } |
||
79 | |||
80 | return $answer; |
||
81 | }); |
||
82 | $question->setMaxAttempts($questionMaxAttempts); |
||
83 | if (false === $this->isTestEnv()) { |
||
84 | $question->setHidden(true); |
||
85 | } |
||
86 | $password = $helper->ask($input, $output, $question); |
||
87 | |||
88 | $question = new Question('Enter the name of the new user: '); |
||
89 | $question->setValidator(function ($answer) { |
||
90 | if (null === $answer) { |
||
91 | throw new \Exception('The name field is required to create a new user'); |
||
92 | } |
||
93 | |||
94 | return $answer; |
||
95 | }); |
||
96 | $question->setMaxAttempts($questionMaxAttempts); |
||
97 | $name = $helper->ask($input, $output, $question); |
||
98 | |||
99 | $user = $this->userRepository->createUser(); |
||
100 | $user->setEmail($email); |
||
101 | $user->setName($name); |
||
102 | $user->setPassword($this->userRepository->encodePassword($password)); |
||
103 | $this->userRepository->persistAndFlush($user); |
||
104 | |||
105 | $io->success(sprintf('The new user with email %s, was successfully created', $user->getEmail())); |
||
106 | |||
107 | return 0; |
||
108 | } |
||
110 |