/src/ServerGrove/SGLiveChatBundle/Controller/BaseController.php

https://github.com/casoetan/ServerGroveLiveChat · PHP · 102 lines · 60 code · 13 blank · 29 comment · 5 complexity · 5e831762d1e02dc4c885bc40c7a9f4b8 MD5 · raw file

  1. <?php
  2. namespace ServerGrove\SGLiveChatBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Cookie;
  7. use Symfony\Component\HttpFoundation\Session as SessionStorage;
  8. use Doctrine\ODM\MongoDB\DocumentManager;
  9. /**
  10. * Chat's base controller
  11. *
  12. * @author Ismael Ambrosi<ismael@servergrove.com>
  13. */
  14. abstract class BaseController extends Controller
  15. {
  16. private $request, $response, $session, $dm;
  17. /**
  18. * @return Symfony\Component\HttpFoundation\Request
  19. */
  20. public function getRequest()
  21. {
  22. if (is_null($this->request)) {
  23. $this->request = $this->get('request');
  24. }
  25. return $this->request;
  26. }
  27. /**
  28. * @return Symfony\Component\HttpFoundation\Response
  29. */
  30. public function getResponse()
  31. {
  32. if (is_null($this->response)) {
  33. $this->response = new Response();
  34. }
  35. return $this->response;
  36. }
  37. /**
  38. * @return Symfony\Component\HttpFoundation\Session
  39. */
  40. public function getSessionStorage()
  41. {
  42. if (is_null($this->session)) {
  43. $this->session = $this->getRequest()->getSession();
  44. }
  45. return $this->session;
  46. }
  47. /**
  48. * @return Doctrine\ODM\MongoDB\DocumentManager
  49. */
  50. public function getDocumentManager()
  51. {
  52. if (is_null($this->dm)) {
  53. $this->dm = $this->get('doctrine.odm.mongodb.document_manager');
  54. }
  55. return $this->dm;
  56. }
  57. /**
  58. * @return Symfony\Component\HttpFoundation\Response
  59. */
  60. public function renderTemplate($view, array $parameters = array())
  61. {
  62. return $this->render($view, $parameters, $this->getResponse());
  63. }
  64. /**
  65. * @return Symfony\Component\HttpFoundation\Response
  66. */
  67. public function redirect($url, $status = 302)
  68. {
  69. $this->getResponse()->setRedirect($url, $status);
  70. return $this->getResponse();
  71. }
  72. /**
  73. * @return ServerGrove\SGLiveChatBundle\Document\Operator
  74. */
  75. protected function getOperator()
  76. {
  77. if (!$this->getSessionStorage()->has('_operator')) {
  78. return null;
  79. }
  80. return $this->getDocumentManager()->find('SGLiveChatBundle:Operator', $this->getSessionStorage()->get('_operator'));
  81. }
  82. /**
  83. * @return Symfony\Bundle\ZendBundle\Logger\Logger
  84. */
  85. protected function getLogger()
  86. {
  87. return $this->container->get('logger');
  88. }
  89. }