patternphplaravelMinor
Laravel - Repository and Entity patterns
Viewed 0 times
laravelpatternsrepositoryandentity
Problem
I've spent quite a bit of time studying the repository and entity patterns and this is what I came up with. I would appreciate it if you could post suggestions and critique.
User entity:repository = $repository;
$this->loginValidator = $loginValidator;
}
public function login(array $input)
{
$result = $this->validate($this->loginValidator, $input);
if(!$result instanceof MessageBag)
{
try
{
$user = $this->repository->authenticate(array(
'email' => $input['email'],
'password' => $input['password']
), false);
$this->save($user);
$user->type = $user->getGroups()[0]->id;
$result = new stdClass;
$result->errors = false;
$result->status_code = 23;
$result->user = $user->toArray();
return $result;
}
catch (Exception $e)
{
return ExceptionHandler::returnException($e);
}
}
else
return $this->loginValidator->returnErrors();
}
}User Repository:model = $model;
}
public function find($id)
{
return $this->model->findUserById($id);
}
public function save(SentryUser $model)
{
return $model->save();
}
public function getAccessToken()
{
return $this->model->getUser()->access_token;
}
}
?>User controller:user = $user;
}
public function postLogin()
{
return ResponseSender::send($this->user->login(Input::all()));
}
}
?>Solution
Maybe I'm just thinking of a different approach, but I normally would've reversed the dependency between UserEntity and UserRepository. I would expect the UserEntity to represent a singular record in the database and that I would use the UserRepository to retrieve one.
In this case, I would inject the UserRepository into the controller and call UserRepository.Login(userId, password) that would actually perform the lookup against the DB and return a UserEntity representing the logged in user.
Also, I would suggest defining interfaces for your injected classes so that it's easier to provide future implementations
In this case, I would inject the UserRepository into the controller and call UserRepository.Login(userId, password) that would actually perform the lookup against the DB and return a UserEntity representing the logged in user.
Also, I would suggest defining interfaces for your injected classes so that it's easier to provide future implementations
Context
StackExchange Code Review Q#54919, answer score: 5
Revisions (0)
No revisions yet.