LogConsumer.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace WorkflowBundle\Services;
  3. use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. class LogConsumer implements ConsumerInterface
  7. {
  8. /**
  9. * @var ContainerInterface
  10. */
  11. protected $serviceContainer;
  12. /**
  13. * @param ContainerInterface $serviceContainer
  14. */
  15. public function __construct(ContainerInterface $serviceContainer)
  16. {
  17. $this->serviceContainer = $serviceContainer;
  18. }
  19. /**
  20. * $msg will be an instance of `PhpAmqpLib\Message\AMQPMessage`
  21. * with the $msg->body being the data sent over RabbitMQ.
  22. *
  23. * @param AMQPMessage $msg
  24. */
  25. public function execute(AMQPMessage $msg)
  26. {
  27. $fields = [];
  28. $logClass = 'LogBundle\Entity\Log';
  29. try {
  30. $msgBody = json_decode($msg->body, true);
  31. $json_error = json_last_error();
  32. if (!$json_error) {
  33. // se hace un date format previo
  34. $msgBody['datetime'] = date('Y-m-d H:i:s', strtotime($msgBody['datetime']['date']));
  35. $fields['message'] = $this->serviceContainer->get('monolog_line_formatter')->format($msgBody);
  36. if (isset($msgBody['context']['deviceId']) && isset($msgBody['context']['deviceType'])) {
  37. $logClass = 'LogBundle\Entity\DeviceLog';
  38. $fields['deviceId'] = $msgBody['context']['deviceId'];
  39. $fields['deviceType'] = $msgBody['context']['deviceType'];
  40. }
  41. } else {
  42. $fields['message'] = "Error json: {$json_error}";
  43. }
  44. } catch (\Exception $ex) {
  45. $fields['message'] = $ex->getMessage();
  46. }
  47. $log = $this->createLog($fields, $logClass);
  48. if (is_null($log)) {
  49. var_dump('Error: log no creado');
  50. } else {
  51. var_dump(sprintf("ID: %s Message: %s", $log->getId(), $log->getMessage()));
  52. }
  53. return;
  54. }
  55. /**
  56. * Crea una entidad Log y la persiste
  57. *
  58. * @param array $fields
  59. * @param string $logClass
  60. *
  61. * @return \WorkflowBundle\Services\LogBundle\Entity\Log
  62. */
  63. private function createLog($fields, $logClass = 'LogBundle\Entity\Log')
  64. {
  65. if (class_exists($logClass)) {
  66. $em = $this->serviceContainer->get('doctrine.orm.entity_manager');
  67. $log = new $logClass();
  68. $log->setMessage($fields['message']);
  69. if (isset($fields['deviceId']) && isset($fields['deviceType'])) {
  70. $device = $em->getRepository('LicenseBundle:Device')->findOneBy([
  71. 'deviceId' => $fields['deviceId'],
  72. 'deviceType' => $fields['deviceType'],
  73. ]);
  74. $log->setDevice($device);
  75. }
  76. $validator = $this->serviceContainer->get('validator');
  77. if ($validator->validate($log)->count() == 0) {
  78. $em->persist($log);
  79. $em->flush($log);
  80. return $log;
  81. }
  82. }
  83. return null;
  84. }
  85. }