Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 20th, 2012  |  syntax: None  |  size: 0.95 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Is this correct use of Exception handling in PHP / Symfony2
  2. class ExampleService{
  3.     // ...
  4.     public function getValueByUser($user)
  5.     {
  6.         $result = $this->em->getRepository('SomeBundle:SomeEntity')->getValue($user);
  7.         if (!$result instanceof EntitySomeEntity) {
  8.             throw new ExceptionInvalidArgumentException("no value found for that user");
  9.         }
  10.         return $result;
  11.     }
  12. }
  13.        
  14. // ...
  15. $ExampleService = $this->get('example_serivce');
  16.  
  17. $value = $ExampleService->getValueByUser($user);
  18.        
  19. class UserService
  20. {
  21.     public function find($id)
  22.     {
  23.         return $this->em->getRepository('UserBundle:User')->find($id);
  24.     }
  25. }
  26.        
  27. class UserController
  28. {
  29.     public function viewAction($id)
  30.     {
  31.         $user = $this->get('user.service')->find($id);
  32.         if (!$user) {
  33.             throw $this->createNotFoundException(
  34.                 $this->get('translator')->trans('user.not_found')
  35.             );
  36.         }
  37.         // ...
  38.     }
  39. }