123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace DeviceBundle\EventListener;
- use Doctrine\ORM\Event\LifecycleEventArgs;
- use DeviceBundle\Interfaces\DeviceInterface;
- use WebserviceBundle\Services\Webservice;
- use Buzz\Message\RequestInterface as HttpRequestInterface;
- class DeviceListener
- {
- /**
- * @var Webservice
- */
- private $webservice;
- /**
- * @var string
- */
- private $devicePostUrl;
- /**
- * @var string
- */
- private $deviceDeletePostUrl;
-
- /**
- * @var string
- */
- private $devicePutUrl;
- /**
- * @param Webservice $webservice
- * @param string $devicePostUrl
- * @param string $deviceDeletePostUrl
- */
- public function __construct(Webservice $webservice, $devicePostUrl, $deviceDeletePostUrl, $devicePutUrl)
- {
- $this->webservice = $webservice;
- $this->devicePostUrl = $devicePostUrl;
- $this->deviceDeletePostUrl = $deviceDeletePostUrl;
- $this->devicePutUrl = $devicePutUrl;
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function postPersist(LifecycleEventArgs $args)
- {
- $this->send($args, $this->devicePostUrl, HttpRequestInterface::METHOD_POST);
- }
- /**
- * @param LifecycleEventArgs $args
- */
- public function preRemove(LifecycleEventArgs $args)
- {
- $entity = $args->getEntity();
- if ($entity instanceof DeviceInterface) {
- if($deviceId = $this->getRemoteDeviceId($entity)) {
- $data = array('id' => $deviceId);
- return $this->webservice->makeGetRequest($this->deviceDeletePostUrl, HttpRequestInterface::METHOD_DELETE, $data);
- }
- }
- }
-
- /**
- * @param LifecycleEventArgs $args
- */
- public function postUpdate(LifecycleEventArgs $args)
- {
- $entity = $args->getEntity();
- if ($entity instanceof DeviceInterface) {
- if($deviceId = $this->getRemoteDeviceId($entity)) {
- $url = "{$this->devicePutUrl}{$deviceId}";
- $this->send($args, $url, HttpRequestInterface::METHOD_PUT);
- }
- }
- }
- /**
- * @param LifecycleEventArgs $args
- * @param string $url
- * @param string $method
- */
- private function send(LifecycleEventArgs $args, $url, $method)
- {
- $entity = $args->getEntity();
- if ($entity instanceof DeviceInterface) {
- $data = $entity->getDeviceData();
- return $this->webservice->makeGetRequest($url, $method, $data);
- }
- }
- /**
- * @param object $entity
- */
- private function getRemoteDeviceId($entity)
- {
- $deviceId = $entity->getId();
- $deviceType = get_class($entity);
- $tenancyId = $entity->getTenancyId();
-
- $filters = array('deviceId'=>$deviceId,'deviceType'=>$deviceType,'tenancyId'=>$tenancyId);
- $data = $this->webservice->getData("device_post_url",$filters);
- $deviceId = null;
- if(isset($data[0]))
- $deviceId = $data[0]['id'];
-
- return $deviceId;
- }
- }
|