Webservice.php 5.6 KB

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