Last active
September 1, 2017 15:47
-
-
Save martinbean/3d87c6fecbb267f887cd to your computer and use it in GitHub Desktop.
Laravel 5 registrar service, improved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Services; | |
use App\Contracts\UserRepository; | |
use App\User; | |
use Illuminate\Contracts\Auth\Registrar as RegistrarContract; | |
use Illuminate\Contracts\Hashing\Hasher as HasherContract; | |
use Illuminate\Contracts\Validation\Factory as ValidationFactory; | |
use Illuminate\Contracts\Validation\Validator; | |
class Registrar implements RegistrarContract { | |
/** | |
* The validation factory implementation. | |
* | |
* @var ValidationFactory | |
*/ | |
protected $validator; | |
/** | |
* The user repository implementation. | |
* | |
* @var UserRepository | |
*/ | |
protected $users; | |
/** | |
* The hasher implementation. | |
* | |
* @var HasherContract | |
*/ | |
protected $hasher; | |
/** | |
* Create a new registrar instance. | |
* | |
* @param ValidationFactory $validator | |
* @param UserRepository $users | |
* @param HasherContract $hasher | |
*/ | |
public function __construct(ValidationFactory $validator, UserRepository $users, HasherContract $hasher) | |
{ | |
$this->validator = $validator; | |
$this->users = $users; | |
$this->hasher = $hasher; | |
} | |
/** | |
* Get a validator for an incoming registration request. | |
* | |
* @param array $data | |
* @return Validator | |
*/ | |
public function validator(array $data) | |
{ | |
return $this->validator->make($data, [ | |
'name' => 'required|max:255', | |
'email' => 'required|email|max:255|unique:users', | |
'password' => 'required|confirmed|min:6', | |
]); | |
} | |
/** | |
* Create a new user instance after a valid registration. | |
* | |
* @param array $data | |
* @return User | |
*/ | |
public function create(array $data) | |
{ | |
return $this->users->create([ | |
'name' => $data['name'], | |
'email' => $data['email'], | |
'password' => $this->hasher->make($data['password']), | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment