Skip to content

Instantly share code, notes, and snippets.

@roquie
Created August 8, 2018 12:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save roquie/8b1170f345e62a46480ab16d073642cf to your computer and use it in GitHub Desktop.
<?php
// file1
// bind UserRepositoryInterface to EloquentUserRepository object
class EloquentUserRepository extends UserRepositoryInterface
{
// ...
public function all()
{
return $this->model->all();
}
public function allForAgent()
{
return $this->model->where('type', 'agent')->all();
}
public function allForOtherRole()
{
// .....
}
}
// file2
class UserService extends UserServiceInterface
{
protected $auth;
protected $repo;
public function __construct(Authentificable $auth, UserRepositoryInterface $repo)
{
$this->auth = $auth;
$this->repo = $repo;
}
public function getUsers()
{
if ($this->auth->inRole('admin')) {
return $this->repo->all();
} elseif ($this->auth->inRole('agent')) {
return $this->repo->allForAgent();
} elseif (/* other checks */) {
return /* other methods */;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment