src/Service/DBLogService.php line 37

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: uwethiess
  5.  * Date: 22.06.17
  6.  * Time: 12:29
  7.  */
  8. namespace App\Service;
  9. use App\Entity\Coupon;
  10. use App\Entity\DBLog;
  11. use App\Entity\Order;
  12. use App\Entity\User;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Doctrine\ORM\EntityRepository;
  16. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  17. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  18. use Symfony\Component\Serializer\Serializer;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. class DBLogService
  21. {
  22.     /** @var EntityManager $_em */
  23.     protected $_em;
  24.     
  25.     /** @var EntityRepository $_repository */
  26.     protected $_repository;
  27.     /** @var Serializer $_serializer */
  28.     protected $_serializer;
  29.     
  30.     public function __construct(EntityManagerInterface $em)
  31.     {
  32.         $this->_em $em;
  33.         $this->_repository $this->_em->getRepository(DBLog::class);
  34.         $objectNormalizer = new ObjectNormalizer(
  35.             null,
  36.             null,
  37.             null,
  38.             null,
  39.             null,
  40.             null,
  41.             [
  42.                 ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER=>function ($object) {
  43.                     return (string)$object;
  44.                 }
  45.             ]
  46.         );
  47. //        $objectNormalizer->setCircularReferenceHandler(function ($object) {
  48. //            return $object->__toString();
  49. //        });
  50.         $normalizers = [$objectNormalizer];
  51.         $encoders = [new JsonEncoder()];
  52.         $this->_serializer = new Serializer($normalizers$encoders);
  53.     }
  54.     
  55.     public function info($class$type$dataOrder $order=nullUser $user=null) {
  56.         $this->writelog('info'$class$type$data$order$user);
  57.     }
  58.     
  59.     public function warning($class$type$dataOrder $order=nullUser $user=null) {
  60.         $this->writelog('warning'$class$type$data$order$user);
  61.     }
  62.     
  63.     public function error($class$type$dataOrder $order=nullUser $user=null) {
  64.         $this->writelog('error'$class$type$data$order$user);
  65.     }
  66.     public function debug($class$type$dataOrder $order=nullUser $user=null) {
  67.         $this->writelog('debug'$class$type$data$order$user);
  68.     }
  69.     public function writelog($level$class$type$dataOrder $order=nullUser $user=null) {
  70.         $log = new DBLog();
  71.         try {
  72.             $jsonData $this->_serializer->serialize($data'json');
  73.         } catch (\Exception $e) {
  74.             $data = [
  75.                 'level' => $level,
  76.                 'class' => $class,
  77.                 'type' => $type,
  78.                 'exception' => $e
  79.             ];
  80.             if ($user instanceof User) {
  81.                 $data['user_id'] = $user->getId();
  82.             }
  83.             if ($order instanceof Order) {
  84.                 $data['order_id'] = $order->getId();
  85.             }
  86.             $this->error'Exception''LoggerSerializeException'$data);
  87.             $jsonData null;
  88.         }
  89.         if ($order) {
  90.             $order $this->_em->merge($order);
  91.         }
  92.         $log->setTs(new \DateTime());
  93.         $log->setLevel($level);
  94.         $log->setClass($class);
  95.         $log->setType($type);
  96.         $log->setData($jsonData);
  97.         $log->setOrder($order);
  98.         $log->setUser($user);
  99.         try {
  100.             $this->_em->persist($log);
  101.             $this->_em->flush();
  102.         } catch (\Exception $e) {
  103.             $data = [
  104.                 'level' => $level,
  105.                 'class' => $class,
  106.                 'type' => $type,
  107.                 'exception' => $e
  108.             ];
  109.             if ($user instanceof User) {
  110.                 $data['user_id'] = $user->getId();
  111.             }
  112.             if ($order instanceof Order) {
  113.                 $data['order_id'] = $order->getId();
  114.             }
  115.             $this->error'Exception''LoggingException'$data);
  116.         }
  117.         unset($log);
  118.     }
  119. }