vendor/oi/bug/src/Listener/CatchExceptionListener.php line 73

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: eph
  5.  * Date: 27/10/16
  6.  * Time: 15:30
  7.  */
  8. namespace Oi\BugBundle\Listener;
  9. use Doctrine\Bundle\DoctrineBundle\Registry;
  10. use Doctrine\ORM\EntityManager;
  11. use Oi\BugBundle\Entity\Bug;
  12. use Oi\BugBundle\Entity\BugCookie;
  13. use Oi\BugBundle\Entity\BugTrace;
  14. use Oi\BugBundle\Entity\LastUpdate;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Twig\Environment;
  19. class CatchExceptionListener
  20. {
  21.     /**
  22.      * @var Registry
  23.      */
  24.     private $doctrine;
  25.     /**
  26.      * @var ExceptionEvent
  27.      */
  28.     private $event null;
  29.     /**
  30.      * @var Request
  31.      */
  32.     private $request null;
  33.     /**
  34.      * @var TokenStorageInterface
  35.      */
  36.     private $tokenStorage null;
  37.     /**
  38.      * @var EntityManager
  39.      */
  40.     private $em null;
  41.     private $user;
  42.     private $options;
  43.     public function __construct(
  44.         Registry $doctrine,
  45.         TokenStorageInterface $tokenStorage,
  46.         $options
  47.     )
  48.     {
  49.         $this->doctrine $doctrine;
  50.         $this->tokenStorage $tokenStorage;
  51.         $this->em $doctrine->getManager();
  52.         $this->options $options;
  53.         if ($this->tokenStorage) {
  54.             if (null !== $token $this->tokenStorage->getToken()) {
  55.                 if (is_object($user $token->getUser())) {
  56.                     $this->user $user;
  57.                 }
  58.             }
  59.         }
  60.     }
  61.     /**
  62.      * @param ExceptionEvent $event
  63.      */
  64.     public function onKernelException(ExceptionEvent $event)
  65.     {
  66.         $this->event $event;
  67.         $this->request $event->getRequest();
  68.         $exception $event->getThrowable();
  69.         if (!strpos($exception->getMessage(), 'GET /uploads')) {
  70.             $cookies $this->request->cookies->all();
  71.             $exceptionType 'Exception';
  72.             if (!is_null($exception->getPrevious())) {
  73.                 $exceptionType get_class($exception->getPrevious());
  74.             }
  75.             if ($exceptionType == 'Oi\BugBundle\Listener\CatchExceptionListener') {
  76.                 $exceptionType get_class($exception);
  77.             }
  78.             $message $exception->getMessage();
  79.             $route $this->request->get('_route');
  80.             $routeParams $this->request->get('_route_params', []);
  81.             $url $this->request->getUri();
  82.             $baseUrl $this->request->getBaseUrl();
  83.             $query $this->request->getQueryString();
  84.             $userAgent $this->request->server->get('HTTP_USER_AGENT');
  85.             $host $this->request->getHost();
  86.             $ip $this->request->getClientIp();
  87.             $username $this->user $this->user->getUsername() : "Anonymous";
  88.             $statusCode $this->getStatusCode($exceptionType);
  89.             if (isset($this->options['filter']) && isset($this->options['filter']['status'])) {
  90.                 switch ($statusCode) {
  91.                     case 400:
  92.                         if (!$this->options['filter']['status']['400']) {
  93.                             return false;
  94.                         }
  95.                         break;
  96.                     case 401:
  97.                         if (!$this->options['filter']['status']['401']) {
  98.                             return false;
  99.                         }
  100.                         break;
  101.                     case 403:
  102.                         if (!$this->options['filter']['status']['403']) {
  103.                             return false;
  104.                         }
  105.                         break;
  106.                     case 404:
  107.                         if (!$this->options['filter']['status']['404']) {
  108.                             return false;
  109.                         }
  110.                         break;
  111.                     case 405:
  112.                         if (!$this->options['filter']['status']['405']) {
  113.                             return false;
  114.                         }
  115.                         break;
  116.                     case 406:
  117.                         if (!$this->options['filter']['status']['406']) {
  118.                             return false;
  119.                         }
  120.                         break;
  121.                     case 407:
  122.                         if (!$this->options['filter']['status']['407']) {
  123.                             return false;
  124.                         }
  125.                         break;
  126.                     case 500:
  127.                         if (!$this->options['filter']['status']['500']) {
  128.                             return false;
  129.                         }
  130.                         break;
  131.                     default:
  132.                         if (!$this->options['filter']['status']['others']) {
  133.                             return false;
  134.                         }
  135.                         break;
  136.                 }
  137.             }
  138.             if (isset($this->options['database']['enable']) && $this->options['database']['enable']) {
  139.                 if ($this->em->isOpen()) {
  140.                     $this->em->close();
  141.                     //If some transactions are currently active, rollbacking them all before starting.
  142.                     $conn $this->em->getConnection();
  143.                     while ($conn->getTransactionNestingLevel() > 0) {
  144.                         $conn->rollBack();
  145.                     }
  146.                 }
  147.                 $this->em $this->doctrine->resetManager();
  148.                 $this->em->beginTransaction();
  149.                 try {
  150.                     $last $this->em->getRepository(LastUpdate::class)->findOneBy([], ['id' => 'DESC']);
  151.                     $bug = new Bug();
  152.                     $bug->setExceptionType($exceptionType);
  153.                     $bug->setMessage($message);
  154.                     $bug->setRoute($route);
  155.                     $bug->setRouteParams($routeParams);
  156.                     $bug->setUrl($url);
  157.                     $bug->setBaseUrl($baseUrl);
  158.                     $bug->setQuery($query);
  159.                     $bug->setUserAgent($userAgent);
  160.                     $bug->setHost($host);
  161.                     $bug->setIpAddress($ip);
  162.                     $bug->setUsername($username);
  163.                     $bug->setStatusCode($statusCode);
  164.                     $bug->setUpdate($last);
  165.                     $this->em->persist($bug);
  166.                     $this->em->flush();
  167.                     foreach ($exception->getTrace() as $trace) {
  168.                         $bugTrace = new BugTrace();
  169.                         $bugTrace->setBug($bug);
  170.                         $fx '';
  171.                         if (isset($trace['file'])) {
  172.                             $bugTrace->setFile($trace['file']);
  173.                         }
  174.                         if (isset($trace['class'])) {
  175.                             $fx .= $trace['class'];
  176.                         }
  177.                         if (isset($trace['type'])) {
  178.                             $fx .= $trace['type'];
  179.                         }
  180.                         if (isset($trace['function'])) {
  181.                             $fx .= $trace['function'];
  182.                         }
  183.                         $bugTrace->setFunction($fx);
  184.                         if (isset($trace['line'])) {
  185.                             $bugTrace->setLine($trace['line']);
  186.                         }
  187.                         if (isset($trace['line'])) {
  188.                             $bugTrace->setLine($trace['line']);
  189.                         }
  190.                         $this->em->persist($bugTrace);
  191.                     }
  192.                     $this->em->flush();
  193.                     foreach ($cookies as $key => $value) {
  194.                         $bugCookie = new BugCookie();
  195.                         $bugCookie->setBug($bug);
  196.                         $bugCookie->setKey($key);
  197.                         $bugCookie->setValue($value);
  198.                         $this->em->persist($bugCookie);
  199.                     }
  200.                     $this->em->flush();
  201.                     $this->em->commit();
  202.                 } catch (\Exception $e) {
  203.                     $this->em->rollback();
  204.                 }
  205.             }
  206.         }
  207.     }
  208.     private function getStatusCode($exceptionType)
  209.     {
  210.         switch ($exceptionType) {
  211.             case 'Symfony\Component\Routing\Exception\ResourceNotFoundException':
  212.                 return 404;
  213.             case 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException':
  214.             case 'Symfony\Component\Debug\Exception\FatalThrowableError':
  215.             default:
  216.                 return 500;
  217.         }
  218.     }
  219. }