【发布时间】:2018-02-14 23:46:49
【问题描述】:
以下代码是否会被视为“良好”做法?
它是包的 RPC 端点的控制器。 这个想法是轻松覆盖/扩展包含该包的特定项目的验证或授权。
你能说这些是保护条款或方法吗? 拥有一个仅用于检查某些内容并在出现问题时抛出异常的方法是一个好主意吗?
代码对我来说看起来很干净,但我想就此获得一些建议:)
public function doSomethingWithCustomer() {
try {
$this->validate();
$this->authorize();
$customer = $this->resolveCustomer();
$customer->doSomething();
} catch (HttpException $e) {
$this->logger->error($e->getMessage());
return $this->errorResponse($e->getStatusCode(), $e->getMessage());
}
}
protected function validate()
{
// Validate input
if (!$valid) {
throw new BadRequestHttpException('Invalid email address');
}
}
protected function authorize()
{
// Do some authorization checking
if ($notAuthorized) {
throw new AccessDeniedHttpException('Not authorized');
}
}
protected function resolveCustomer()
{
$customer = Customer::load(1);
if (is_null($customer) {
throw new NotFoundHttpException('Customer not found');
}
return $customer;
}
【问题讨论】:
-
这种事情在整个 JDK 中都可以看到,例如在
Socket和SocketImpl之间,或者在许多方法中,在完成所有明显的检查后最终委托给 JNI。 -
基本上这是例外的目的。在出现问题时短路正常流量。虽然我会将授权检查移动到不同的类并在必要时覆盖它。
标签: design-patterns exception-handling guard-clause