MCPcopy Create free account
hub / github.com/denho/faved / __invoke

Method __invoke

controllers/UserCreateController.php:21–57  ·  view source on GitHub ↗
(array $input)

Source from the content-addressed store, hash-verified

19class UserCreateController implements ControllerInterface
20{
21 public function __invoke(array $input): ResponseInterface
22 {
23 // Check if authentication is enabled (any user exists)
24 $repository = ServiceContainer::get(Repository::class);
25 $auth_enabled = $repository->userTableNotEmpty();
26
27 // If auth is enabled already and user exists, raise an error
28 if ($auth_enabled) {
29 return data([
30 'success' => false,
31 'message' => 'User has been created already.'
32 ], 400);
33 }
34
35 $username = trim($input['username'] ?? '');
36 validateUsername($username);
37
38 $password = $input['password'] ?? '';
39 $confirm_password = $input['confirm_password'] ?? '';
40 validatePassword($password);
41 validatePasswordConfirmation($password, $confirm_password);
42
43 $password_hash = password_hash($password, Config::getPasswordAlgo());
44 $user_id = $repository->createUser($username, $password_hash);
45
46 if (!$user_id) {
47 throw new DataWriteException('Failed to create user.');
48 }
49
50 loginUser($user_id);
51
52 $user = $repository->getUser($user_id);
53
54 return success('User created successfully.', [
55 'user' => buildPublicUserObject($user)
56 ], 201);
57 }
58}

Callers

nothing calls this directly

Calls 12

dataFunction · 0.85
validateUsernameFunction · 0.85
validatePasswordFunction · 0.85
loginUserFunction · 0.85
buildPublicUserObjectFunction · 0.85
getMethod · 0.80
userTableNotEmptyMethod · 0.80
getPasswordAlgoMethod · 0.80
createUserMethod · 0.80
getUserMethod · 0.80
successFunction · 0.50

Tested by

no test coverage detected