DeviceListener.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  68. }
  69. }
  70. /**
  71. * @param LifecycleEventArgs $args
  72. * @param string $url
  73. * @param string $method
  74. */
  75. private function send(LifecycleEventArgs $args, $url, $method)
  76. {
  77. $entity = $args->getEntity();
  78. if ($entity instanceof DeviceInterface) {
  79. $data = $entity->getDeviceData();
  80. return $this->webservice->makeGetRequest($url, $method, $data);
  81. }
  82. }
  83. /**
  84. * @param object $entity
  85. */
  86. private function getRemoteDeviceId($entity)
  87. {
  88. $deviceId = $entity->getId();
  89. $deviceType = get_class($entity);
  90. $tenancyId = $entity->getTenancyId();
  91. $filters = array('deviceId'=>$deviceId,'deviceType'=>$deviceType,'tenancyId'=>$tenancyId);
  92. $data = $this->webservice->getData("device_post_url",$filters);
  93. $deviceId = null;
  94. if(isset($data[0]))
  95. $deviceId = $data[0]['id'];
  96. return $deviceId;
  97. }
  98. }