GermaniaKG /
UserProfiles
| 1 | <?php |
||
| 2 | namespace Germania\UserProfiles; |
||
| 3 | |||
| 4 | use Germania\UserProfiles\Exceptions\RegisterUserException; |
||
| 5 | use Germania\UserProfiles\Exceptions\LoginNameNotAvailableException; |
||
| 6 | use Germania\UserProfiles\Exceptions\InsertUserException; |
||
| 7 | use Germania\UserProfiles\Exceptions\SetPasswordException; |
||
| 8 | use Germania\UserProfiles\Exceptions\SetApiKeyException; |
||
| 9 | use Psr\Log\LoggerInterface; |
||
| 10 | use Psr\Log\NullLogger; |
||
| 11 | |||
| 12 | |||
| 13 | /** |
||
| 14 | * Wraps all required tasks for registering a new user, without marking him `active` or assigning user roles. |
||
| 15 | */ |
||
| 16 | class RegisterUserWrapper |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Callable |
||
| 21 | */ |
||
| 22 | public $check_username; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Callable |
||
| 26 | */ |
||
| 27 | public $insert_user; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Callable |
||
| 31 | */ |
||
| 32 | public $set_password; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Callable |
||
| 36 | */ |
||
| 37 | public $set_api_key; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Callable |
||
| 41 | */ |
||
| 42 | public $assign_role; |
||
| 43 | |||
| 44 | |||
| 45 | public $users_table = 'users'; |
||
| 46 | |||
| 47 | |||
| 48 | /** |
||
| 49 | * @param PDO $pdo |
||
| 50 | * @param Callable $hasher |
||
| 51 | * @param Callable $randomizer |
||
| 52 | * @param string $users_table |
||
| 53 | * @param LoggerInterface $logger Optional: PSR-3 Logger |
||
| 54 | */ |
||
| 55 | 10 | public function __construct(\PDO $pdo, Callable $hasher, Callable $randomizer, $users_table = null, LoggerInterface $logger = null) |
|
| 56 | { |
||
| 57 | 10 | $this->logger = $logger ?: new NullLogger; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 58 | 10 | $this->users_table = $users_table ?: $this->users_table; |
|
| 59 | |||
| 60 | 10 | $this->check_username = new PdoUsernameChecker( $pdo, $logger, $users_table); |
|
| 61 | 10 | $this->insert_user = new PdoInsertNewUser( $pdo, $logger, $users_table); |
|
| 62 | 10 | $this->set_password = new PdoPasswordSetter( $pdo, $hasher, $logger, $users_table); |
|
| 63 | 10 | $this->set_api_key = new PdoApiKeySetter( $pdo, $randomizer, $logger, $users_table); |
|
| 64 | 10 | } |
|
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * Performs common actions to register a new user, including: |
||
| 69 | * |
||
| 70 | * - Check if a username is available |
||
| 71 | * - Insert new User |
||
| 72 | * - Set Password for new user ID |
||
| 73 | * - Set API key for new user ID |
||
| 74 | * |
||
| 75 | * The user_data array must contain these elements: |
||
| 76 | * |
||
| 77 | * - first_name |
||
| 78 | * - last_name |
||
| 79 | * - display_name |
||
| 80 | |||
| 81 | * - login |
||
| 82 | * - password |
||
| 83 | * |
||
| 84 | * @param array $user_data |
||
| 85 | * @return int New User ID |
||
| 86 | * @throws UserProfileExceptionInterface |
||
| 87 | */ |
||
| 88 | 10 | public function __invoke( array $user_data ) |
|
| 89 | { |
||
| 90 | 10 | if (!array_key_exists("login", $user_data) |
|
| 91 | 10 | or !array_key_exists("password", $user_data)): |
|
| 92 | 5 | throw new RegisterUserException("Missing fields 'login' and/or 'password'"); |
|
| 93 | endif; |
||
| 94 | |||
| 95 | // |
||
| 96 | // 1. Is login name available? |
||
| 97 | // (may throw LoginNameNotAvailableException) |
||
| 98 | // |
||
| 99 | 5 | $check_username = $this->check_username; |
|
| 100 | 5 | $check_username( $user_data['login'] ); |
|
| 101 | |||
| 102 | |||
| 103 | // |
||
| 104 | // 2. Insert new User |
||
| 105 | // (may throw InsertUserException) |
||
| 106 | // |
||
| 107 | 5 | $insert_user = $this->insert_user; |
|
| 108 | 5 | $new_user_id = $insert_user( $user_data ); |
|
| 109 | |||
| 110 | |||
| 111 | // |
||
| 112 | // 3. Set password |
||
| 113 | // (may throw SetPasswordException) |
||
| 114 | // |
||
| 115 | 5 | $set_password = $this->set_password; |
|
| 116 | 5 | $set_password( $new_user_id, $user_data[ 'password' ] ); |
|
| 117 | |||
| 118 | |||
| 119 | // |
||
| 120 | // 4. Create API Key |
||
| 121 | // (may throw SetApiKeyException) |
||
| 122 | // |
||
| 123 | 5 | $set_api_key = $this->set_api_key; |
|
| 124 | 5 | $set_api_key( $new_user_id ); |
|
| 125 | |||
| 126 | |||
| 127 | // |
||
| 128 | // Alles klar bis hier? |
||
| 129 | // |
||
| 130 | 5 | return $new_user_id; |
|
| 131 | |||
| 132 | } |
||
| 133 | } |
||
| 134 |