DeviceListener.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * @param Webservice $webservice
  23. * @param string $devicePostUrl
  24. * @param string $deviceDeletePostUrl
  25. */
  26. public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl)
  27. {
  28. $this->webservice = $webservice;
  29. $this->devicePostUrl = $devicePostUrl;
  30. $this->deviceDeletePostUrl = $deviceDeletePostUrl;
  31. }
  32. /**
  33. * @param LifecycleEventArgs $args
  34. */
  35. public function postPersist(LifecycleEventArgs $args)
  36. {
  37. $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
  38. }
  39. /**
  40. * @param LifecycleEventArgs $args
  41. */
  42. public function preRemove(LifecycleEventArgs $args)
  43. {
  44. $this->send($args, $this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE);
  45. }
  46. /**
  47. * @param LifecycleEventArgs $arg
  48. * @param string $url
  49. * @param string $method
  50. */
  51. private function send(LifecycleEventArgs $args, $url, $method)
  52. {
  53. $entity = $args->getEntity();
  54. if ($entity instanceof DeviceInterface) {
  55. $data = array(
  56. 'deviceType' => get_class($entity),
  57. 'deviceId' => $entity->getId(),
  58. 'ip' => $entity->getIp(),
  59. );
  60. return $this->webservice->makeGetRequest($url, $method, $data);
  61. }
  62. }
  63. }