DeviceLogExtension.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace DeviceBundle\Twig;
  3. use WebserviceBundle\Services\Webservice;
  4. class DeviceLogExtension extends \Twig_Extension
  5. {
  6. /**
  7. * @var Webservice
  8. */
  9. protected $webservice;
  10. /**
  11. * @var string
  12. */
  13. protected $deviceGetUrl;
  14. /**
  15. * @var string
  16. */
  17. protected $deviceLogGetUrl;
  18. /**
  19. * @param Webservice $webservice
  20. * @param string $deviceGetUrl
  21. * @param string $deviceLogGetUrl
  22. */
  23. public function __construct(Webservice $webservice, $deviceGetUrl, $deviceLogGetUrl)
  24. {
  25. $this->webservice = $webservice;
  26. $this->deviceGetUrl = $deviceGetUrl;
  27. $this->deviceLogGetUrl = $deviceLogGetUrl;
  28. }
  29. /**
  30. * @return array
  31. */
  32. public function getFunctions()
  33. {
  34. return array(
  35. new \Twig_SimpleFunction('device_log', array($this, 'getDeviceLog')),
  36. );
  37. }
  38. /**
  39. * @param Entity $object
  40. * @param int $limit
  41. * @param int $offset
  42. *
  43. * @return array
  44. */
  45. public function getDeviceLog($object, $limit = 50, $offset = 0)
  46. {
  47. $filters = array(
  48. 'deviceType' => get_class($object),
  49. 'deviceId' => $object->getId(),
  50. 'disableTenancy' => true
  51. );
  52. $device = $this->webservice->getData($this->deviceGetUrl, $filters);
  53. $log = '';
  54. if (count($device) && isset($device[0])) {
  55. $filters = array(
  56. 'device' => $device[0]['id'],
  57. );
  58. $deviceLogs = $this->webservice->getData($this->deviceLogGetUrl, $filters, array(), $limit, $offset);
  59. foreach ($deviceLogs as $deviceLog) {
  60. unset($deviceLog['device']);
  61. $created = new \DateTime($deviceLog['created']);
  62. $log .= $created->format('d/m/Y H:i') . ' - ' . $deviceLog['message'] . PHP_EOL;
  63. }
  64. }
  65. return $log;
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getName()
  71. {
  72. return 'device_log_extension';
  73. }
  74. }