| Conditions | 4 |
| Paths | 3 |
| Total Lines | 41 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 28 |
| CRAP Score | 4 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 33 | 4 | public function create( |
|
| 34 | Interactor $io, |
||
| 35 | string $username, |
||
| 36 | ): void { |
||
| 37 | 4 | $user = $this->userRepository->findOneBy([ |
|
| 38 | 4 | 'name' => $username, |
|
| 39 | 4 | ]); |
|
| 40 | |||
| 41 | 4 | if ($user !== null) { |
|
| 42 | 1 | $io->error('A user with that name already exists', true); |
|
| 43 | 1 | return; |
|
| 44 | } |
||
| 45 | |||
| 46 | 3 | $validator = static function (mixed $password): string { |
|
| 47 | 2 | $password = trim((string) $password); |
|
| 48 | 2 | if (mb_strlen($password) < PasswordVerificator::PASSWORD_MIN_LENGTH) { |
|
| 49 | 1 | throw new InvalidArgumentException('Password too short'); |
|
| 50 | } |
||
| 51 | |||
| 52 | 1 | return $password; |
|
| 53 | 3 | }; |
|
| 54 | |||
| 55 | 3 | $password = $io->promptHidden('Password', $validator, self::PASSWORD_RETRIES); |
|
| 56 | |||
| 57 | 2 | if ($password === '') { |
|
| 58 | 1 | $io->error('Too many retries - aborting', true); |
|
| 59 | 1 | return; |
|
| 60 | } |
||
| 61 | |||
| 62 | 1 | $user = $this->userCreator->create( |
|
| 63 | 1 | $username, |
|
| 64 | 1 | $password |
|
| 65 | 1 | ); |
|
| 66 | |||
| 67 | 1 | $io->info( |
|
| 68 | 1 | sprintf( |
|
| 69 | 1 | 'Created user `%s` with id `%d`', |
|
| 70 | 1 | $username, |
|
| 71 | 1 | $user->getId() |
|
| 72 | 1 | ), |
|
| 73 | 1 | true |
|
| 74 | 1 | ); |
|
| 77 |