DeviceListener.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * @param Webservice $webservice
  19. * @param string $devicePostUrl
  20. */
  21. public function __construct(Webservice $webservice, $devicePostUrl)
  22. {
  23. $this->webservice = $webservice;
  24. $this->devicePostUrl = $devicePostUrl;
  25. }
  26. /**
  27. * @param LifecycleEventArgs $args
  28. */
  29. public function postPersist(LifecycleEventArgs $args)
  30. {
  31. $entity = $args->getEntity();
  32. if ($entity instanceof DeviceInterface) {
  33. $data = array(
  34. 'deviceType' => get_class($entity),
  35. 'deviceId' => $entity->getId(),
  36. 'ip' => $entity->getIp(),
  37. );
  38. $result = $this->webservice->makeGetRequest($this->devicePostUrl, HttpRequestInterface::METHOD_POST, $data);
  39. }
  40. }
  41. }