| 1 | <?php namespace Modules\User\Services; |
||
| 7 | class UserRegistration |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var Authentication |
||
| 11 | */ |
||
| 12 | private $auth; |
||
| 13 | /** |
||
| 14 | * @var RoleRepository |
||
| 15 | */ |
||
| 16 | private $role; |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $input; |
||
| 21 | |||
| 22 | public function __construct(Authentication $auth, RoleRepository $role) |
||
| 23 | { |
||
| 24 | $this->auth = $auth; |
||
| 25 | $this->role = $role; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param array $input |
||
| 30 | * @return mixed |
||
| 31 | */ |
||
| 32 | public function register(array $input) |
||
| 33 | { |
||
| 34 | $this->input = $input; |
||
| 35 | |||
| 36 | $user = $this->createUser(); |
||
| 37 | |||
| 38 | if ($this->hasProfileData()) { |
||
| 39 | $this->createProfileForUser($user); |
||
| 40 | } |
||
| 41 | |||
| 42 | $this->assignUserToUsersGroup($user); |
||
| 43 | |||
| 44 | event(new UserHasRegistered($user)); |
||
| 45 | |||
| 46 | return $user; |
||
| 47 | } |
||
| 48 | |||
| 49 | private function createUser() |
||
| 53 | |||
| 54 | private function assignUserToUsersGroup($user) |
||
| 55 | { |
||
| 56 | $role = $this->role->findByName('User'); |
||
| 57 | |||
| 58 | $this->auth->assignRole($user, $role); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Check if the request input has a profile key |
||
| 63 | * @return bool |
||
| 64 | */ |
||
| 65 | private function hasProfileData() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create a profile for the given user |
||
| 72 | * @param $user |
||
| 73 | */ |
||
| 74 | private function createProfileForUser($user) |
||
| 79 | } |
||
| 80 |