Answer the question
In order to leave comments, you need to log in
Exceptions or validation?
Hey Guru, I need your help! Studying the principle "TTUK - Fat Stupid Ugly Controllers" I decided to make all controllers using services. Here a problem arose.
Example: suppose the login process in theory should look like this
in the controller
$auth_service = new AuthService();
try {
$auth_service->auth($email, $password);
} catch (ServiceException $e) {
switch ($e->getCode()) {
case ServiceException::NOT_FOUND_EMAIL:
return;
case ServiceException::NOT_CORRECT_PASSWORD:
return;
default:
throw $e;
}
}
public function auth(string $email, string $password)
{
if ($this->isEmailNotExists($email)) {
throw new ServiceException('Email not exists', ServiceException::NOT_FOUND_EMAIL);
}
$profile = Profile::firstFromEmail($email);
if ($this->isPasswordNotCorrect($profile, $password)) {
throw new ServiceException('Password not correct', ServiceException::NOT_CORRECT_PASSWORD);
}
/* логика с сессиями */
}
Answer the question
In order to leave comments, you need to log in
However, it is not entirely clear whether it would be right to make exceptions in the controller or still first ask the service about the existence of soap, for example?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question