DeviceListener.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace DeviceBundle\EventListener;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use DeviceBundle\Interfaces\DeviceInterface;
  5. use WebserviceBundle\Services\Webservice;
  6. use Buzz\Message\RequestInterface as HttpRequestInterface;
  7. class DeviceListener
  8. {
  9. /**
  10. * @var Webservice
  11. */
  12. private $webservice;
  13. /**
  14. * @var string
  15. */
  16. private $devicePostUrl;
  17. /**
  18. * @var string
  19. */
  20. private $deviceDeletePostUrl;
  21. /**
  22. * @var string
  23. */
  24. private $devicePutUrl;
  25. /**
  26. * @param Webservice $webservice
  27. * @param string $devicePostUrl
  28. * @param string $deviceDeletePostUrl
  29. * @param string $devicePutUrl
  30. */
  31. public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl, $devicePutUrl)
  32. {
  33. $this->webservice = $webservice;
  34. $this->devicePostUrl = $devicePostUrl;
  35. $this->deviceDeletePostUrl = $deviceDeletePostUrl;
  36. $this->devicePutUrl = $devicePutUrl;
  37. $this->enabled = true;
  38. }
  39. /**
  40. * @param LifecycleEventArgs $args
  41. */
  42. public function postPersist(LifecycleEventArgs $args)
  43. {
  44. if (!$this->enabled) return;
  45. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  46. }
  47. /**
  48. * @param LifecycleEventArgs $args
  49. * @return mixed
  50. */
  51. public function preRemove(LifecycleEventArgs $args)
  52. {
  53. if (!$this->enabled) return;
  54. $entity = $args->getEntity();
  55. if ($entity instanceof DeviceInterface) {
  56. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  57. $data = array('id' => $deviceId);
  58. return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
  59. }
  60. }
  61. }
  62. /**
  63. * @param LifecycleEventArgs $args
  64. */
  65. public function postUpdate(LifecycleEventArgs $args)
  66. {
  67. if (!$this->enabled) return;
  68. $entity = $args->getEntity();
  69. if ($entity instanceof DeviceInterface) {
  70. if ($deviceId = $this->getRemoteDeviceId($entity)) {
  71. $url = "{$this->devicePutUrl}{$deviceId}";
  72. $this->send($args, $url, HttpRequestInterface::METHOD_PUT);
  73. } else {
  74. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  75. }
  76. }
  77. }
  78. /**
  79. * @param LifecycleEventArgs $args
  80. * @param string $url
  81. * @param string $method
  82. * @return mixed
  83. */
  84. private function send(LifecycleEventArgs $args, $url, $method)
  85. {
  86. if (!$this->enabled) return;
  87. $entity = $args->getEntity();
  88. if ($entity instanceof DeviceInterface) {
  89. $data = $entity->getDeviceData();
  90. $data = $this->addLocationData($entity, $data);
  91. return $this->webservice->makeGetRequest($url, $method, $data);
  92. }
  93. }
  94. /**
  95. * Agrega la ubicación de $entity si implementa LocationInterface
  96. *
  97. * @param Entity $entity
  98. * @param array $data
  99. *
  100. * @return array
  101. */
  102. private function addLocationData($entity, $data)
  103. {
  104. if (!$this->enabled) return;
  105. $locationInterface = 'MapBundle\Entity\Interfaces\LocationInterface';
  106. if (interface_exists($locationInterface) && is_a($entity, $locationInterface)) {
  107. $extraData = json_decode($data['extraData'], true);
  108. $extraData['location'] = $entity->getLocation() ? $entity->getLocation()->getData() : array();
  109. $data['extraData'] = json_encode($extraData);
  110. }
  111. return $data;
  112. }
  113. /**
  114. * @param object $entity
  115. * @return mixed
  116. */
  117. private function getRemoteDeviceId($entity)
  118. {
  119. if (!$this->enabled) return;
  120. $deviceId = $entity->getId();
  121. $deviceType = get_class($entity);
  122. $tenancyId = $entity->getTenancyId();
  123. $filters = array('deviceId' => $deviceId, 'deviceType' => $deviceType, 'tenancyId' => $tenancyId);
  124. $data = $this->webservice->getData("device_post_url", $filters);
  125. // file_put_contents("/var/flowdat/error.log",json_encode($data));
  126. $deviceId = null;
  127. if (isset($data[0]))
  128. $deviceId = $data[0]['id'];
  129. return $deviceId;
  130. }
  131. function remoteCheck($enabled = true)
  132. {
  133. $this->enabled = $enabled;
  134. }
  135. /**
  136. * Se utiliza para las pruebas de phpunit
  137. * @param Webservice $webservice
  138. */
  139. public function setWebservice($webservice)
  140. {
  141. $this->webservice = $webservice;
  142. }
  143. }