Created
October 26, 2015 03:49
-
-
Save rizqidjamaluddin/819528492291552b75e9 to your computer and use it in GitHub Desktop.
Base set of classes for more complex features - NOT a "pattern" to be followed like you will be cursed for not adhering to, if you use an unnecessary factory I will hunt you down with a super soaker
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 | |
/** | |
* @property int $id | |
* @property string $email | |
* @property string $password | |
* @property Carbon $created_at | |
*/ | |
class User extends Model { | |
} |
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 | |
class UserController { | |
// I'm not going to lay out all the injections, just assume every other class here except the model | |
function __construct(){} | |
function store () { | |
$email = Input::get('email'); | |
$password = Input::get('password'); | |
if ($this->creationValidator->validate($email, $password)->any()) { | |
return $this->respondWithValidationErrorsHere(); | |
} | |
$user = $this->userFactory->makeFromNew($email, $password); | |
$this->userRepository->persist($user); | |
return $this->respondWithSuccessfulUserCreationHere($user); | |
} | |
} |
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 | |
class UserCreationValidator { | |
function __construct(UserRepository $userRepository) { | |
$this->repository = $userRepository; | |
} | |
// I like to have agnostic validator classes that CAN use laravel's validators if needed - or any other validators. | |
// The one in use here is Respect\Validation. | |
function validate($email, $password) { | |
$bag = new MessageBag; | |
if (!v::stringType()->length(5, 500)->validate($password)) { | |
$bag->add("password", "Password needs to be between 5 and 500 characters.") | |
} | |
if (!v::email()->validate($email)) { | |
$bag->add("email", "Please enter a valid email.") | |
} | |
if ($this->repository->emailExists($email)) { | |
$bag->add("email", "Email already used."); | |
} | |
return $bag; | |
} | |
} |
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 | |
class UserFactory { | |
/** | |
* Let's imagine we DI, say, a BcryptHasher into this. | |
*/ | |
function __construct (Hasher $hasher) { | |
$this->hasher = $hasher; | |
} | |
public function makeFromNew($email, $password) { | |
$i = new User; | |
$i->email = $email; | |
$i->password = $this->hasher->hash($password); | |
return $i; | |
} | |
// factories can contain whatever various ways a model might be created | |
public function copyExistingUser(User $user) {} | |
} |
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 | |
class UserRepository { | |
function __construct (User $model) { | |
$this->model = $model; | |
} | |
// yeah, yeah, eloquent repositories lol | |
public function persist(User $user) { | |
$user->save(); | |
} | |
public function emailExists($email) { | |
return $this->model->where('email', $email)->exists(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment