sirajul91

Passing repository from controller to service to repository

Mar 14th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class UserController extends Controller {
  3.     public function __constructor(UserService $service, UserTransformer $transformer){
  4.         $this->service = $service;
  5.         $this->transformer = $transformer;
  6.     }
  7.     public function getPostsByUser(Request $request, NewRepository $newRepository, $userId){
  8.         return $this->transformer->transformData($this->service->getUserPosts($userId, $newRepository));
  9.     }
  10. }
  11.  
  12. class UserService extends Service {
  13.     public function __construct(UserRepository $repository){
  14.         $this->repository = $repository;
  15.     }
  16.     public function getUserPosts($userId, NewRepository $newRepository){
  17.         return $this->repository->getUserPostsFromRepository($userId, $newRepository);
  18.     }
  19. }
  20.  
  21. class UserRepository extends Repository {
  22.     public function __construct(User $user){
  23.         $this->model = $user;
  24.     }
  25.  
  26.     public function getUserPostsFromRepository($id, $newRepository){
  27.         return $newRepository->anotherMethod($this->model->with('posts')->find($id)->posts);
  28.     }
  29. }
Add Comment
Please, Sign In to add comment