5 ms·
I also prefer this approach. The excellent book 'Domain Driven Design - Tackling Complexity in the Heart of Software' by Eric Evans has helped me a lot. I'd wri
by jorgenhorstink 14y ago
I also prefer this approach. The excellent book 'Domain Driven Design - Tackling Complexity in the Heart of Software' by Eric Evans has helped me a lot. I'd write something like:
<?php
class AuthController extends Controller {
public function __construct(Request $request) {
$this->service = AuthService::getInstance();
}
// reflection calls a Module + Action attribute to {module}Controller
public function login(Request $request) {
if ($request->getRequestMethod() === WebRequest::POST) {
if ($this->service->login($request->getAttribute('username'), $request->getAttribute('response')) {
die('{ "success" : true }');
} else {
die('{ "success" : false }');
}
}
}
}
class AuthService { // Application Service -> it just defines a clear API, not a Domain Driven Design service
public function __construct(Session $session) {
$this->session = $session;
}
public function login($username, $response) {
try {
$user = User::getRepository()->getByUsername($username);
return $user->isValidChallengeResponse($this->session->getAttribute('challenge', 'auth'), $response);
} catch (UserDoesNotExistException $e) { }
return false;
}
}
class User {
protected static $repository;
public static function setRepository(IUserRepository $repository) {
self::$repository = $repository;
}
public function getRepository() {
return self::$repository;
}
// yeah yeah, it is arguably if this belongs as a Domain method of the user…
public function isValidChallengeResponse($challenge, $response) {
// and yes, this is very weak challenge response…
return md5($challenge . $this->getPassword()) === $response;
}
}
?>
If anyone can tell me what's wrong with this type of MVC, except it isn't using an actual View in this limited example, I'd love to hear it. Controllers should be thin, models and services should be thick.