Created
October 27, 2015 19:29
-
-
Save jlem/f4647077470e06227a50 to your computer and use it in GitHub Desktop.
Medium Laravel Article - EloquentMemberRepository.php
This file contains hidden or 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 EloquentMemberRepository implements MemberRepositoryInterface | |
{ | |
/** | |
* @var Member | |
*/ | |
protected $model; | |
public function __construct(Member $model) | |
{ | |
$this->model = $model | |
} | |
/** | |
* @return MemberInterface | |
*/ | |
public function find($id) | |
{ | |
return $this->model->find($id); | |
} | |
/** | |
* @return Illuminate\Support\Collection | |
*/ | |
public function findTopPosters($count = 10) | |
{ | |
return $this->model | |
->orderBy($this->model::ATTR_POST_COUNT, 'DESC') | |
->take($count) | |
->get(); | |
} | |
public function save(MemberInterface $member) | |
{ | |
$member->save(); | |
/* | |
Caveat here - our MemberInterface doesn't explicitly define | |
a save() method, we are only type hinting it to ensure some consistency | |
between different implementations of the repository interface. | |
This Eloquent-specific repository implementation very much expects that | |
What you are passing back into it can save itself - so in practice | |
You will not be returning and saving POPOs through Eloquent repositories. | |
Despite the coupling of Eloquent models with Eloquent repositories, the purpose | |
of the architecture is to insulate the rest of your application from changes | |
in your data / persistence model. | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment