
Untitled
By: a guest on
May 20th, 2012 | syntax:
None | size: 0.95 KB | hits: 24 | expires: Never
Is this correct use of Exception handling in PHP / Symfony2
class ExampleService{
// ...
public function getValueByUser($user)
{
$result = $this->em->getRepository('SomeBundle:SomeEntity')->getValue($user);
if (!$result instanceof EntitySomeEntity) {
throw new ExceptionInvalidArgumentException("no value found for that user");
}
return $result;
}
}
// ...
$ExampleService = $this->get('example_serivce');
$value = $ExampleService->getValueByUser($user);
class UserService
{
public function find($id)
{
return $this->em->getRepository('UserBundle:User')->find($id);
}
}
class UserController
{
public function viewAction($id)
{
$user = $this->get('user.service')->find($id);
if (!$user) {
throw $this->createNotFoundException(
$this->get('translator')->trans('user.not_found')
);
}
// ...
}
}