Webservice.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace WebserviceBundle\Services;
  3. use Buzz\Message\Request as HttpRequest;
  4. use Buzz\Message\RequestInterface as HttpRequestInterface;
  5. use Buzz\Message\Response as HttpResponse;
  6. class Webservice
  7. {
  8. protected $serviceContainer;
  9. protected $securityTokenStorage;
  10. protected $httpClient;
  11. public function __construct($serviceContainer, $securityTokenStorage, $httpClient)
  12. {
  13. $this->serviceContainer = $serviceContainer;
  14. $this->securityTokenStorage = $securityTokenStorage;
  15. $this->httpClient = $httpClient;
  16. }
  17. /**
  18. * @param string $webservice
  19. * @param array $params
  20. * @return array
  21. */
  22. public function getChoices($webservice, $params = array())
  23. {
  24. $choices = array();
  25. $results = $this->getArray($webservice, $params);
  26. foreach ($results as $row) {
  27. if (isset($row['name']) && isset($row['id'])) {
  28. $choices[$row['name']] = $row['id'];
  29. }
  30. }
  31. return $choices;
  32. }
  33. /**
  34. * @param string $webservice
  35. * @param array $params
  36. * @return array
  37. */
  38. public function getArray($webservice, $params = array())
  39. {
  40. $results = array();
  41. if ($this->serviceContainer->hasParameter($webservice)) {
  42. $url = $this->serviceContainer->getParameter($webservice);
  43. $url .= '?filters[qb-criteria]';
  44. foreach ($params as $param => $value) {
  45. $url .= "&filters[{$param}]=$value";
  46. }
  47. try {
  48. $results = json_decode($this->makeGetRequest($url), true);
  49. } catch (\Exception $ex) {
  50. }
  51. }
  52. return $results;
  53. }
  54. /**
  55. * @param string $url
  56. * @return HttpResponse
  57. */
  58. public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET)
  59. {
  60. $response = '';
  61. if ($token = $this->securityTokenStorage->getToken()) {
  62. $request = new HttpRequest($method, $url);
  63. $response = new HttpResponse();
  64. if (method_exists($token, 'getAccessToken')) {
  65. $request->setHeaders(array('Authorization: Bearer ' . $token->getAccessToken()));
  66. }
  67. $this->httpClient->send($request, $response);
  68. $response = $response->getContent();
  69. }
  70. return $response;
  71. }
  72. /**
  73. * @param string $webservice
  74. * @param array $filters
  75. * @param array $order_by
  76. * @param integer $limit
  77. * @param integer $offset
  78. * @return array
  79. */
  80. public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  81. {
  82. if ($this->serviceContainer->hasParameter($webservice)) {
  83. $url = $this->serviceContainer->getParameter($webservice) . "?";
  84. if($filters) {
  85. $url .= http_build_query(array('filters' => $filters));
  86. }
  87. if($order_by) {
  88. $url .= http_build_query(array('order_by' => $order_by));
  89. }
  90. if($limit) {
  91. $url .= "&limit={$limit}";
  92. }
  93. if($offset) {
  94. $url .= "&offset={$offset}";
  95. }
  96. $data = array();
  97. try {
  98. $data = json_decode($this->makeGetRequest($url), true);
  99. } catch (\Exception $ex) {
  100. }
  101. return $data;
  102. }
  103. return array();
  104. }
  105. /**
  106. * @param string $webservice
  107. * @param int $id
  108. * @return string
  109. */
  110. public function getById($webservice, $id)
  111. {
  112. $result = $this->getArray($webservice, array(
  113. 'id' => $id
  114. ));
  115. return isset($result[0]) && isset($result[0]['id']) && isset($result[0]['name'])
  116. ? "{$result[0]['id']} - {$result[0]['name']}"
  117. : $id;
  118. }
  119. /**
  120. * @param string $webservice
  121. * @param array $filters
  122. * @param array $order_by
  123. * @param integer $limit
  124. * @param integer $offset
  125. * @return array
  126. */
  127. public function putData($webservice, $id, $data)
  128. {
  129. if ($this->serviceContainer->hasParameter($webservice)) {
  130. $url = str_replace(".json", "/{$id}", $this->serviceContainer->getParameter($webservice));
  131. $data_string = json_encode($data);
  132. $ch = curl_init($url);
  133. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  134. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  135. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($data_string)));
  136. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  137. try {
  138. $return = curl_exec($ch);
  139. } catch (\Exception $ex) {
  140. return array("error"=>"Connection Error.");
  141. }
  142. if($return) {
  143. return json_decode($return, true);
  144. } else {
  145. return array("error"=>"Transaction Error.");
  146. }
  147. }
  148. return array("error"=>"Webservice({$webservice}) not found.");
  149. }
  150. }