Looks like a pretty default abstraction to me.
How do you think? Am I too paranoid?
Pretty hard to judge what you want to hear. What would be "too paranoid" and for what?
I want to abstract Eloquent from my controllers completely. This is what I'm doing:
abstract class EloquentRepository implements RepositoryInterface {
protected $query;
public function __construct()
{
// child class must be named as "UserRepository"
$child_name = str_replace('Repository', '', class_basename($this));
$this->query = app($child_name)->newQuery();
}
public function getAll()
{
// makeEntityCollection maps each Eloquent record to an Entity
return $this->makeEntityCollection($this->query->all());
}
public function getById($id)
{
return $this->makeEntity($this->query->findOrFail($id));
}
}
Example of retrieving User's posts
class EloquentPostRepository extends BaseRepository {
public function getPostsOfUser($user_id)
{
$collection = $this->query->with('user')->whereUserId($user_id)->get();
return $this->makeEntityCollection($collection);
}
}
class UserController {
// Show
public function posts($user_id)
{
// injectedRepository
$posts = $this->postRepository->getPostsOfUser($user_id);
return view('user.posts', compact('posts'));
}
}
Entities are PHP Plain Objects.
How do you think? Am I too paranoid?
Well, I realize this was a good example of "premature" abstraction.
Please or to participate in this conversation.