class SiteController extends \Illuminate\Routing\Controller
{
protected $userService;
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
public function showUserProfile(Request $request)
{
$user = $this->userService->getUser($request->id);
return view('user.profile', compact('user'));
}
}
class UserService
{
protected $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function getUser($id)
{
$user = $this->userRepository->getUserById($id);
$this->userRepository->logSession($user);
return $user;
}
}
class UserRepository
{
protected $userModel, $logModel;
public function __construct(User $user, Log $log)
{
$this->userModel = $user;
$this->logModel = $log;
}
public function getUserById($id)
{
return $this->userModel->findOrFail($id);
}
public function logSession($user)
{
$this->logModel->user = $user->id;
$this->logModel->save();
}
}