DeviceListener.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. */
  30. public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl, $devicePutUrl)
  31. {
  32. $this->webservice = $webservice;
  33. $this->devicePostUrl = $devicePostUrl;
  34. $this->deviceDeletePostUrl = $deviceDeletePostUrl;
  35. $this->devicePutUrl = $devicePutUrl;
  36. }
  37. /**
  38. * @param LifecycleEventArgs $args
  39. */
  40. public function postPersist(LifecycleEventArgs $args)
  41. {
  42. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  43. }
  44. /**
  45. * @param LifecycleEventArgs $args
  46. */
  47. public function preRemove(LifecycleEventArgs $args)
  48. {
  49. $entity = $args->getEntity();
  50. if ($entity instanceof DeviceInterface) {
  51. if($deviceId = $this->getRemoteDeviceId($entity)) {
  52. $data = array('id' => $deviceId);
  53. return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
  54. }
  55. }
  56. }
  57. /**
  58. * @param LifecycleEventArgs $args
  59. */
  60. public function postUpdate(LifecycleEventArgs $args)
  61. {
  62. $entity = $args->getEntity();
  63. if ($entity instanceof DeviceInterface) {
  64. if($deviceId = $this->getRemoteDeviceId($entity)) {
  65. $url = "{$this->devicePutUrl}{$deviceId}";
  66. $this->send($args, $url, HttpRequestInterface::METHOD_PUT);
  67. } else {
  68. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  69. }
  70. }
  71. }
  72. /**
  73. * @param LifecycleEventArgs $args
  74. * @param string $url
  75. * @param string $method
  76. */
  77. private function send(LifecycleEventArgs $args, $url, $method)
  78. {
  79. $entity = $args->getEntity();
  80. if ($entity instanceof DeviceInterface) {
  81. $data = $entity->getDeviceData();
  82. return $this->webservice->makeGetRequest($url, $method, $data);
  83. }
  84. }
  85. /**
  86. * @param object $entity
  87. */
  88. private function getRemoteDeviceId($entity)
  89. {
  90. $deviceId = $entity->getId();
  91. $deviceType = get_class($entity);
  92. $tenancyId = $entity->getTenancyId();
  93. $filters = array('deviceId'=>$deviceId,'deviceType'=>$deviceType,'tenancyId'=>$tenancyId);
  94. $data = $this->webservice->getData("device_post_url",$filters);
  95. file_put_contents("/var/flowdat/error.log",json_encode($data));
  96. $deviceId = null;
  97. if(isset($data[0]))
  98. $deviceId = $data[0]['id'];
  99. return $deviceId;
  100. }
  101. }